adk-python/contributing/samples/workflow_agent_seq/agent.py
Hangfei Lin a4adb739c0 Create a developer folder and add samples.
PiperOrigin-RevId: 755885332
2025-05-07 09:26:19 -07:00

95 lines
3.0 KiB
Python

# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.sequential_agent import SequentialAgent
# --- 1. Define Sub-Agents for Each Pipeline Stage ---
# Code Writer Agent
# Takes the initial specification (from user query) and writes code.
code_writer_agent = LlmAgent(
name="code_writer_agent",
model="gemini-1.5-flash-001",
instruction="""You are a Code Writer AI.
Based on the user's request, write the initial Python code.
Output *only* the raw code block.
""",
description="Writes initial code based on a specification.",
# Stores its output (the generated code) into the session state
# under the key 'generated_code'.
output_key="generated_code",
)
# Code Reviewer Agent
# Takes the code generated by the previous agent (read from state) and provides feedback.
code_reviewer_agent = LlmAgent(
name="code_reviewer_agent",
model="gemini-2.0-flash-001",
instruction="""You are a Code Reviewer AI.
Review the below Python code.
```
{generated_code}
```
Provide constructive feedback on potential errors, style issues, or improvements.
Focus on clarity and correctness.
Output only the review comments.
""",
description="Reviews code and provides feedback.",
# Stores its output (the review comments) into the session state
# under the key 'review_comments'.
output_key="review_comments",
)
# Code Refactorer Agent
# Takes the original code and the review comments (read from state) and refactors the code.
code_refactorer_agent = LlmAgent(
name="code_refactorer_agent",
model="gemini-2.0-flash-001",
instruction="""You are a Code Refactorer AI.
Below is the original Python code:
```
{generated_code}
```
Below are the review comments:
{review_comments}
Refactor the code based on the provided feedback.
Output *only* the final, refactored code block.
""",
description="Refactors code based on review comments.",
# Stores its output (the refactored code) into the session state
# under the key 'refactored_code'.
output_key="refactored_code",
)
# --- 2. Create the SequentialAgent ---
# This agent orchestrates the pipeline by running the sub_agents in order.
code_pipeline_agent = SequentialAgent(
name="code_pipeline_agent",
sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent],
# The agents will run in the order provided: Writer -> Reviewer -> Refactorer
)
root_agent = code_pipeline_agent