|
15 | 15 | from google.adk.agents.llm_agent import LlmAgent
|
16 | 16 | from google.adk.agents.sequential_agent import SequentialAgent
|
17 | 17 |
|
| 18 | +# Part of agent.py --> Follow https://google.github.io/adk-docs/get-started/quickstart/ to learn the setup |
| 19 | + |
18 | 20 | # --- 1. Define Sub-Agents for Each Pipeline Stage ---
|
19 | 21 |
|
20 | 22 | # Code Writer Agent
|
21 | 23 | # Takes the initial specification (from user query) and writes code.
|
22 | 24 | code_writer_agent = LlmAgent(
|
23 |
| - name="code_writer_agent", |
24 |
| - model="gemini-1.5-flash-001", |
25 |
| - instruction="""You are a Code Writer AI. |
26 |
| - Based on the user's request, write the initial Python code. |
27 |
| - Output *only* the raw code block. |
28 |
| - """, |
29 |
| - description="Writes initial code based on a specification.", |
30 |
| - # Stores its output (the generated code) into the session state |
31 |
| - # under the key 'generated_code'. |
32 |
| - output_key="generated_code", |
| 25 | + name="CodeWriterAgent", |
| 26 | + model="gemini-1.5-flash", |
| 27 | + # Change 3: Improved instruction |
| 28 | + instruction="""You are a Python Code Generator. |
| 29 | +Based *only* on the user's request, write Python code that fulfills the requirement. |
| 30 | +Output *only* the complete Python code block, enclosed in triple backticks (```python ... ```). |
| 31 | +Do not add any other text before or after the code block. |
| 32 | +""", |
| 33 | + description="Writes initial Python code based on a specification.", |
| 34 | + output_key="generated_code", # Stores output in state['generated_code'] |
33 | 35 | )
|
34 | 36 |
|
35 | 37 | # Code Reviewer Agent
|
36 | 38 | # Takes the code generated by the previous agent (read from state) and provides feedback.
|
37 | 39 | code_reviewer_agent = LlmAgent(
|
38 |
| - name="code_reviewer_agent", |
39 |
| - model="gemini-2.0-flash-001", |
40 |
| - instruction="""You are a Code Reviewer AI. |
41 |
| -
|
42 |
| -Review the below Python code. |
43 |
| -
|
44 |
| -``` |
45 |
| -{generated_code} |
46 |
| -``` |
47 |
| -
|
48 |
| -Provide constructive feedback on potential errors, style issues, or improvements. |
49 |
| -Focus on clarity and correctness. |
50 |
| -Output only the review comments. |
51 |
| -
|
52 |
| - """, |
| 40 | + name="CodeReviewerAgent", |
| 41 | + model="gemini-2.0-flash", |
| 42 | + # Change 3: Improved instruction, correctly using state key injection |
| 43 | + instruction="""You are an expert Python Code Reviewer. |
| 44 | + Your task is to provide constructive feedback on the provided code. |
| 45 | +
|
| 46 | + **Code to Review:** |
| 47 | + ```python |
| 48 | + {generated_code} |
| 49 | + ``` |
| 50 | +
|
| 51 | +**Review Criteria:** |
| 52 | +1. **Correctness:** Does the code work as intended? Are there logic errors? |
| 53 | +2. **Readability:** Is the code clear and easy to understand? Follows PEP 8 style guidelines? |
| 54 | +3. **Efficiency:** Is the code reasonably efficient? Any obvious performance bottlenecks? |
| 55 | +4. **Edge Cases:** Does the code handle potential edge cases or invalid inputs gracefully? |
| 56 | +5. **Best Practices:** Does the code follow common Python best practices? |
| 57 | +
|
| 58 | +**Output:** |
| 59 | +Provide your feedback as a concise, bulleted list. Focus on the most important points for improvement. |
| 60 | +If the code is excellent and requires no changes, simply state: "No major issues found." |
| 61 | +Output *only* the review comments or the "No major issues" statement. |
| 62 | +""", |
53 | 63 | description="Reviews code and provides feedback.",
|
54 |
| - # Stores its output (the review comments) into the session state |
55 |
| - # under the key 'review_comments'. |
56 |
| - output_key="review_comments", |
| 64 | + output_key="review_comments", # Stores output in state['review_comments'] |
57 | 65 | )
|
58 | 66 |
|
| 67 | + |
59 | 68 | # Code Refactorer Agent
|
60 | 69 | # Takes the original code and the review comments (read from state) and refactors the code.
|
61 | 70 | code_refactorer_agent = LlmAgent(
|
62 |
| - name="code_refactorer_agent", |
63 |
| - model="gemini-2.0-flash-001", |
64 |
| - instruction="""You are a Code Refactorer AI. |
65 |
| -
|
66 |
| -Below is the original Python code: |
67 |
| -
|
68 |
| -``` |
69 |
| -{generated_code} |
70 |
| -``` |
71 |
| -
|
72 |
| -Below are the review comments: |
73 |
| -
|
74 |
| -{review_comments} |
75 |
| -
|
76 |
| -Refactor the code based on the provided feedback. |
77 |
| -
|
78 |
| -Output *only* the final, refactored code block. |
79 |
| - """, |
| 71 | + name="CodeRefactorerAgent", |
| 72 | + model="gemini-2.0-flash", |
| 73 | + # Change 3: Improved instruction, correctly using state key injection |
| 74 | + instruction="""You are a Python Code Refactoring AI. |
| 75 | +Your goal is to improve the given Python code based on the provided review comments. |
| 76 | +
|
| 77 | + **Original Code:** |
| 78 | + ```python |
| 79 | + {generated_code} |
| 80 | + ``` |
| 81 | +
|
| 82 | + **Review Comments:** |
| 83 | + {review_comments} |
| 84 | +
|
| 85 | +**Task:** |
| 86 | +Carefully apply the suggestions from the review comments to refactor the original code. |
| 87 | +If the review comments state "No major issues found," return the original code unchanged. |
| 88 | +Ensure the final code is complete, functional, and includes necessary imports and docstrings. |
| 89 | +
|
| 90 | +**Output:** |
| 91 | +Output *only* the final, refactored Python code block, enclosed in triple backticks (```python ... ```). |
| 92 | +Do not add any other text before or after the code block. |
| 93 | +""", |
80 | 94 | description="Refactors code based on review comments.",
|
81 |
| - # Stores its output (the refactored code) into the session state |
82 |
| - # under the key 'refactored_code'. |
83 |
| - output_key="refactored_code", |
| 95 | + output_key="refactored_code", # Stores output in state['refactored_code'] |
84 | 96 | )
|
85 | 97 |
|
| 98 | + |
86 | 99 | # --- 2. Create the SequentialAgent ---
|
87 | 100 | # This agent orchestrates the pipeline by running the sub_agents in order.
|
88 | 101 | code_pipeline_agent = SequentialAgent(
|
89 |
| - name="code_pipeline_agent", |
| 102 | + name="CodePipelineAgent", |
90 | 103 | sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent],
|
| 104 | + description=( |
| 105 | + "Executes a sequence of code writing, reviewing, and refactoring." |
| 106 | + ), |
91 | 107 | # The agents will run in the order provided: Writer -> Reviewer -> Refactorer
|
92 | 108 | )
|
93 | 109 |
|
| 110 | +# For ADK tools compatibility, the root agent must be named `root_agent` |
94 | 111 | root_agent = code_pipeline_agent
|
0 commit comments