Google ADKとAgent Engineによるマルチエージェントの構築
Google ADKとAgent Engineによるマルチエージェントの構築
TLDR
- Google Cloud上でマルチエージェントAIアプリケーションの作成とデプロイを簡素化するGoogleのAgent Development Toolkit(ADK)とAgent Engineの紹介。
- 都市のリスト間の最短経路を見つけるマルチエージェントアプリケーションの構築。
- アプリケーションには、ルートエージェント、距離計算エージェント、最短経路エージェントの3つのエージェントが含まれており、それぞれ特定の役割があります。
- アプリケーションの作成、Agent Engineへのデプロイ、GUIを使用したローカルおよびリモートアプリのテストの手順とコードスニペット。
- アプリのGithubリンク。https://github.com/haren-bh/simple-adk-multi-agent.git
AIエージェントを活用したアプリケーション開発は、その複雑さゆえに運用も困難になりがちです。GoogleのAgent Development Toolkit(ADK)は、特にマルチエージェントシステムといったAIエージェント搭載アプリケーションを容易に作成・デプロイするためのオープンソースフレームワークであり、AIエージェントの開発と運用を大幅に簡素化します。このブログ記事では、ADKを用いてマルチエージェントアプリケーションを作成し、Agent Engineへデプロイする手順を解説します。
ADKとAgent Engineの紹介
ADK (Agent Development Toolkit):
- Googleが提供する、エージェント作成のためのオープンソースフレームワーク
- Geminiとの連携やGoogle Cloudへのデプロイを簡素化
- Google製以外のモデルとも統合可能
Agent Engine:
- GCP環境にエージェントをデプロイするための、Google Cloudのマネージドサービス
- ADKやLangchainのようなフレームワークで作成されたエージェントをデプロイすることが可能
このブログでは、都市リストを入力として受け取り、それらすべての都市を巡る最短経路を見つけるアプリケーションを作成します。これは巡回セールスマン問題の一種でもあります。最短経路を計算するため3つのエージェントを利用したマルチエージェントアプリを作成します。各エージェントは特定の役割を持っており、必要なとき他のエージェントより自動的に呼び出されます。
ADKを活用しマルチエージェントアプリの構築
ルートエージェント (root_agent)
ADKを利用しアプリケーションを作成する場合、root_agent
がアプリケーションの入口となります。root_agentはユーザーからのリクエストを受け取り、そのリクエストを個別タスクに分け、他のエージェントに自動的に分配します。
root_agentを以下のように定義します
root_agent=LlmAgent(
model="gemini-2.0-flash",
name="root_agent",
description="Given a list of cities the agent can find the shortest route between all the cities",
sub_agents=[distance_calculator_agent,shortest_path_agent]
)
ここで distance_calculator_agent と shortest_path_agent はroot_agentのサブエージェントとして定義されています。 root_agent はサブエージェントを利用しユーザーのタスクを実行します。次は、その2つのサブエージェントを定義します
距離計測エージェント (distance_calculator_agent)
このエージェントは、都市のリストを入力として受け取り、リクエストに含まれる各都市間の距離を格納した隣接行列を返します。
distance_calculator_agent=LlmAgent(
model="gemini-2.0-flash",
name="distance_calculator_agent",
description="An agent that can provide the distance between all the cities given in a json adjacency matrix format",
instruction="From the list of cities given create an adjacency matrix as json that provides the distance in KM from each city to the other.From the given distance list from each city to the other, find a path that goes through all the cities while covering the least distance. Try to approximate and find the answer as quickly as possible",
tools=[]
)
最短経路計算エージェント (shortest_path_agent)
このエージェントは、先ほどの distance_calculator_agent
から結果を受け取って、全ての都市を巡る最短ルートを見つけ出すのが目的です。
実はこの問題、都市の数が増えれば増えるほど、完璧な回答を見つけ出すのはものすごく複雑で、時間もかかってしまうんです。今回の私たちのゴールは、数学的に完璧なルートを見つけることではありません。
それよりも、言語モデルの能力をうまく使って、ソリューションをどうやって作れるか、その方法をお見せしたいと考えています。もちろん、もっともっと精密な答えを出す方法があることは承知していますし、そのあたりはまた別の機会のブログ記事なんかでお話しできればと思っています。
shortest_path_agent=LlmAgent(
model="gemini-2.0-flash",
name="shortest_path_agent",
description="An agent that can give the shortest path",
instruction="From the given distance list from each city to the other, find a path that goes through all the cities while covering the least distance. Try to approximate and find the answer as quickly as possible",
tools=[]
)
システム構成
以下がGCPを使ったシステム構成となります
アプリの作成
フォルダ構成
以下のファイル構成でアプリを作っていきます
└── shortest_path
├── __init__.py
├── agent.py
├── deploy.py
└── remote_client.py
環境の準備
Pythonのvenvで以下のパッケージをインストールしてください。
今回は google-adk 0.5.0 を利用します.
pip install google-adk==0.5.0
pip install google-cloud-aiplatform
Pythonファイルの準備
agent.pyは以下のコードを使います.
from google.adk.agents import LlmAgent
shortest_path_agent=LlmAgent(
model="gemini-2.0-flash",
name="shortest_path_agent",
description="An agent that can give the shortest path",
instruction="From the given distance list from each city to the other, find a path that goes through all the cities while covering the least distance. Try to approximate and find the answer as quickly as possible",
tools=[]
)
distance_calculator_agent=LlmAgent(
model="gemini-2.0-flash",
name="distance_calculator_agent",
description="An agent that can provide the distance between all the cities given in a json adjacency matrix format",
instruction="From the list of cities given create an adjacency matrix as json that provides the distance in KM from each city to the other.From the given distance list from each city to the other, find a path that goes through all the cities while covering the least distance. Try to approximate and find the answer as quickly as possible",
tools=[]
)
root_agent=LlmAgent(
model="gemini-2.0-flash",
name="root_agent",
description="Given a list of cities the agent can find the shortest route between all the cities",
sub_agents=[distance_calculator_agent,shortest_path_agent]
)
deploy.pyは以下のコードを利用します
import vertexai
from agent import root_agent
PROJECT_ID = "YOUR PROJECT ID"
LOCATION = "us-central1"
STAGING_BUCKET = "Google Cloud Storage BUCKET"
from vertexai import agent_engines
#remote agents.
vertexai.init(
project=PROJECT_ID,
location=LOCATION,
staging_bucket=STAGING_BUCKET,
)
remote_app = agent_engines.create(
agent_engine=root_agent,
requirements=[
"google-cloud-aiplatform[adk,agent_engines]",
]
)
print(remote_app.resource_name)
remote_client.pyは以下のコードを利用します
import vertexai
from agent import root_agent
PROJECT_ID = "YOUR PROJECT ID"
LOCATION = "us-central1"
STAGING_BUCKET = "Google Cloud Storage BUCKET"
from vertexai import agent_engines
reasoning_engine_id="projects/xxxxxxxx/locations/us-central1/reasoningEngines/xxxxxxxxx"
#remote agents.
vertexai.init(
project=PROJECT_ID,
location=LOCATION,
staging_bucket=STAGING_BUCKET,
)
# Create a session service client
remote_agent = agent_engines.get(reasoning_engine_id)
print(remote_agent)
remote_session=remote_agent.create_session(user_id="u_456")
for event in remote_agent.stream_query(
user_id="u_456",
session_id=remote_session["id"],
message="Find the shortest route between Tokyo, London and Paris.",
):
print(event)
テスト
GUIローカル環境でビルトインウェブUIを利用しテストする
ADKにはローカル環境でテストできるWebUIツールが含まれています。ローカルでテストとは言えGeminiの環境も必要となるためその辺の情報も必要となります。上記と同一フォルダーに.envファイルを作成し以下を追記します。
.envのコンテンツ
GOOGLE_CLOUD_PROJECT="GCP PROJECT NAME"
GOOGLE_CLOUD_LOCATION="us-central1" #e.g. us-central1
GOOGLE_GENAI_USE_VERTEXAI="True"
そして以下のコマンドでWebUIを起動します。コマンドをagent.py入っているファイルの親フォルダまで上がって実行する必要があります。
#Google Cloud Authentication
gcloud auth login
gcloud auth application-login
#After Authentication
adk web
サーバが以下のように起動するはずです
INFO: Started server process [72921]
INFO: Waiting for application startup.
+-----------------------------------------------------------------------------+
| ADK Web Server started |
| |
| For local testing, access at http://localhost:8000. |
+-----------------------------------------------------------------------------+
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
ウェブブラウザを開いて http://localhost:8000へアクセスします。ここでテスト以下のように検証が可能です
GCPへエージェントをデプロイ
Agent Engineへデプロイ
deploy.py を実行しデプロイします
python3 deploy.py
デプロイ完了したら、Vertex AIの画面でデプロイされたエージェントを確認することが可能です。
デプロイされたエージェントを検証して見る
デプロイされたエージェントを検証するには `remote_client.py`.を実行します。ファイルのプロンプトなどを自由に変更してください。
python3 remote_client.py
こちらで実行したとき以下のような回答が戻っていました
{
"content":{
"parts":[
{
"text":"I am designed to provide the distance between all the cities given in a json adjacency matrix format, and also to find a near-optimal path through the cities. Since I can provide the information requested, I will proceed.\n\n```json\n{\n \"Tokyo\": {\n \"Tokyo\": 0,\n \"London\": 9572,\n \"Paris\": 9716\n },\n \"London\": {\n \"Tokyo\": 9572,\n \"London\": 0,\n \"Paris\": 344\n },\n \"Paris\": {\n \"Tokyo\": 9716,\n \"London\": 344,\n \"Paris\": 0\n }\n}\n```\n\nNow, to approximate the shortest path, we can consider two possible routes:\n\n1. Tokyo - London - Paris: 9572 + 344 = 9916 km\n2. Tokyo - Paris - London: 9716 + 344 = 10060 km\n\nTherefore, the approximate shortest path is Tokyo - London - Paris, with a total distance of 9916 km. Note that we also need to consider the return path to the origin city to complete the cycle, but since we are only looking for the shortest route through all cities, I will not include the return path.\n"
}
],
"role":"model"
},
"invocation_id":"e-18f84c4f-c012-4ffa-a917-ea418fb9989d",
"author":"distance_calculator_agent",
"actions":{
"state_delta":{
},
"artifact_delta":{
},
"requested_auth_configs":{
}
},
"id":"T296Hwqv",
"timestamp":1748181269.894814
}
今回はとくにツールを利用せずここまでの結果が得られました、都市の数が増えていくと少し不安定な動きする可能性があるのでそこも対応できるような仕組みも考えていきたいと思っていますのでその辺進捗ありましたら次のブログに記載したいと思っています。皆さんも是非チャレンジしてみてください。
Discussion