ADK + Cloud Run を動かす
Google Cloud Next '25 に参加してます。
そのうち会社のほうで参加レポートを出します。
こちらは ADK(Agent Development Kit、Android ではない) のメモ書きのようなものです
2025/04/11 時点だと python でしか ADK はリリースされていないようです。
Cloud Run で動かす
Cloud Run で動かす方法自体は https://google.github.io/adk-docs/deploy/cloud-run/ に記載されていますのでほぼこちらを参考にお願いします。
ディレクトリやファイルは以下のとおりです
.
├── Dockerfile
├── README.md
├── agents
│ ├── code_agent
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ └── agent.py
│ ├── get_weather_agent
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ └── agent.py
│ ├── google_search_agent
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ └── agent.py
│ └── multi_agent
│ └── agent.py
├── pyproject.toml
├── server.py
└── uv.lock
例えば code_agent
は以下のとおりです
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.agents.llm_agent import LlmAgent
# --- Constants ---
GEMINI_MODEL = "gemini-2.0-flash"
# --- 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="CodeWriterAgent",
model=GEMINI_MODEL,
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="CodeReviewerAgent",
model=GEMINI_MODEL,
instruction="""You are a Code Reviewer AI.
Review the Python code provided in the session state under the key '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="CodeRefactorerAgent",
model=GEMINI_MODEL,
instruction="""You are a Code Refactorer AI.
Take the original Python code provided in the session state key 'generated_code'
and the review comments found in the session state key 'review_comments'.
Refactor the original code to address the feedback and improve its quality.
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.
root_agent = SequentialAgent(
name="CodePipelineAgent",
sub_agents=[code_writer_agent, code_reviewer_agent, code_refactorer_agent]
# The agents will run in the order provided: Writer -> Reviewer -> Refactorer
)
Agent として 展開しておけば web ページでみれるようです。root_agent で公開するというのが必要というのが若干くせがある気がする。
今回使っているのが sequential_agent ですが parallel_agent や loop_agent というのもあります。これらはユースケースによって使い分ける形ですね。
今回 Agent Engine ではなく docker で動かす際には、uvicorn + fastAPI で動かします。ほぼサンプルのとおりです。
import os
import uvicorn
from fastapi import FastAPI
from google.adk.cli.fast_api import get_fast_api_app
APP_DIR = os.path.dirname(os.path.abspath(__file__))
SERVE_WEB_INTERFACE = True
AGENT_DIR = os.path.join(APP_DIR, "agents")
app: FastAPI = get_fast_api_app(
agent_dir=AGENT_DIR,
web=SERVE_WEB_INTERFACE,
)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
SERVE_WEB_INTERFACE を True にすると web 画面が表示されます
docker image を作成し、以下のように push してしまいます。
gcloud beta run deploy adk-agent \
--region $GOOGLE_CLOUD_LOCATION \
--project $GOOGLE_CLOUD_PROJECT \
--image asia-northeast1-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/samples/adk-python \
--iap \
--set-env-vars="GOOGLE_CLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT,GOOGLE_CLOUD_LOCATION=$GOOGLE_CLOUD_LOCATION,GOOGLE_GENAI_USE_VERTEXAI=$GOOGLE_GENAI_USE_VERTEXAI"
iap をつけているので組織内のアカウントでアクセスするようにしてください。
アクセスができると以下のように見え、agent が指定でき、利用できるはずです。
使った感想
簡単に作れました。想像以上に楽でした。
A2A の実装も配布してくれててもいいのになーと思いましたが。
次はその A2A protocol での接続をやってみたいと思います(IAP どうなるんだろうという気持ち)
Discussion