ADKのSequential agentsを使って実装するマルチエージェント

記事タイトルとURLをコピーする

G-gen の堂原です。本記事では Google Cloud の Agent Development Kit の機能である、複数の AI エージェントを連携させる Sequential agents の概念と実装方法について、天気予報エージェントの開発を通して解説します。

概要

Agent Development Kit(ADK)とは

Agent Development Kit(以下、ADK)は、Google Cloud が提供する、AI エージェントを構築するための Python ライブラリです。ADK を使用することで、エージェントへの関数処理組み込みや外部ツールの利用、マルチエージェントの実装が行えます。

ADK の基本的な使い方については、以下の記事で紹介していますのでご参照ください。

blog.g-gen.co.jp

Sequential agents

ADK の Sequential agents は、複数の AI エージェントを順番に実行し、前のエージェントの出力を次のエージェントの入力として渡すことで、複雑なタスクを実行するためのワークフローを構築する機能です。

これにより、タスクをより小さく管理しやすいステップに分割し、各ステップを個別のエージェントに担当させることができます。なお、このように複数のエージェントが連携してタスクを行うエージェントを、マルチエージェントと呼びます。

処理の概要

当記事では、指定された都市の明日の天気予報を回答する AI エージェントを開発します。

Google の Gemini アプリに「東京の明日の天気を教えて」と質問した場合の思考プロセスを参考に、エージェントの処理フローを設計します。

The user wants to know the weather in Tokyo tomorrow.
Since the current date is May 1, 2025, "tomorrow" refers to May 2, 2025.
I need to search for the weather forecast for Tokyo on May 2, 2025.

処理の流れは以下のとおりです。

  1. ユーザから指定された都市の現在の日付を求める
  2. 現在の日付から明日の日付を求める
  3. 指定された都市の名前と明日の日付をもとに、Google 検索を用いて天気予報を求める

AI エージェントの実装

ユーザの都市の現在日付を取得

ユーザから指定された都市の現在の日付を求める AI エージェント「current_date_agent」を作成します。

from datetime import datetime, timedelta, timezone
from google.adk.agents import LlmAgent
  
  
def get_current_date(time_difference: int) -> str:
    """Return the current date expressed in the format %m月%d日.

    Args:
        time_difference (int): Time difference from Coordinated Universal Time (UTC)

    Returns:
        str: Current time expressed in the format %m月%d日
    """

    tz = timezone(timedelta(hours=time_difference))
    now = datetime.now(tz)

    return now.strftime("%m月%d日")
  
  
current_date_agent = LlmAgent(
    name="current_date_agent",
    model="gemini-2.0-flash",
    description="Agent to answer questions about the current date.",
    instruction=(
        "You are a helpful agent that answers questions about the current date."
        "First, guess the time difference from Coordinated Universal Time (UTC) for the city specified by the user."
        "Then, retrieve the current date based on the time difference using the `get_current_date` tool."
        "Be sure to include the city name and the current date in the `%m月%d日` format in your answer."
    ),
    tools=[get_current_date],
    output_key="current_date"
)

get_current_date 関数は UTC との時差を引数として受け取り、 %m月%d日 形式で現在日付を返します。

LlmAgentinstruction で AI エージェントの振る舞いを定義し、tools に利用可能なツールとして get_current_date 関数を指定しています。

output_key="current_date" とすることで、このエージェントの出力が current_date というキーで後続のエージェントに渡されるようになります。

現在日付から明日の日付を求める

current_date_agent の処理結果を受けて、明日の日付を計算する AI エージェント tomorrows_date_agent を作成します。

from pydantic import BaseModel, Field
from google.adk.agents import LlmAgent
  
  
class DateOutput(BaseModel):
    city: str = Field(description="A city name")
    date: str = Field(description="A date in the format `%m月%d日`")
  
  
tomorrows_date_agent = LlmAgent(
    name="tomorrows_date_agent",
    model="gemini-2.0-flash",
    instruction=(
        "You are a helpful agent that answers tomorrow's date for a specified city."
        "You are given the city name and the current date as input."
        "Be sure to include the city name and tomorrow's date in the `%m月%d日` format in your answer."
        
        "**the city name and the current date**"
        "{current_date}"
    ),
    output_schema=DateOutput,
    output_key="tomorrow_date"
)

instruction 内の {current_date} には、前の AI エージェント current_date_agent の出力が埋め込まれます。

このエージェントでは、Pydantic を用いて出力データのスキーマ(DateOutput)を定義し、LlmAgentoutput_schema に指定しています。これにより、エージェントの出力フォーマットを JSON 形式で固定化できます。

注意点として、output_schema を設定した場合、同一の LlmAgent において、tools を同時に使用することはできません。 この場合以下のようなエラーが発生します。

pydantic_core._pydantic_core.ValidationError: 1 validation error for LlmAgent
  Value error, Invalid config for agent current_date_agent: if output_schema is set, tools must be empty [type=value_error, input_value={'name': 'current_date_ag...ut_key': 'current_date'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/value_error

Google 検索で天気予報を取得

都市名と明日の日付を基に Google 検索を行い、天気予報を取得する AI エージェント tomorrows_weather_agent を作成します。

from pydantic import BaseModel, Field
from google.adk.tools import google_search
from google.adk.agents import LlmAgent
 
class DateOutput(BaseModel):
    city: str = Field(description="A city name")
    date: str = Field(description="A date in the format `%m月%d日`")
 
tomorrows_weather_agent = LlmAgent(
    name="tomorrows_weather_agent",
    model="gemini-2.0-flash",
    instruction=(
        "You are a helpful agent that answers tomorrow's weather for a specified city."
        "You are given the city name and the tomorrow's date as input."
        "Be sure to include the city name and tomorrow's date in the `%m月%d日` format and tomorrow's weather in your answer."
 
        "**the city name and the tomorrow's date**"
        "{tomorrow_date}"
    ),
    input_schema=DateOutput,
    tools=[google_search]
)

instruction 内の {tomorrow_date} には、前のエージェントの出力が埋め込まれます。

input_schema=DateOutput とすることで、この AI エージェントに渡される入力データのフォーマットを DateOutput スキーマに指定しています。

toolsgoogle_search を指定することで、Google 検索によるグラウンディングを行うことができます。

Sequential agents による連結

最後に、これらの AI エージェントを Sequential agents を用いて連結し、一つのワークフローとして実行できるようにします。

from google.adk.agents import SequentialAgent
 
root_agent = SequentialAgent(
    name="tomorrows_date_agent",
    sub_agents=[current_date_agent, tomorrows_date_agent, tomorrows_weather_agent]
)

SequentialAgentsub_agents に、実行したいエージェントをリストとして渡します。リストの順番通りにエージェントが実行され、前のエージェントの出力が次のエージェントの入力として自動的に渡されます。

動作検証

ADK では、ローカルで作成した AI エージェントを簡単に検証できる開発用 UI が用意されています。この UI を立ち上げるまでの流れは、以下のドキュメントで紹介されています。

この開発用 UI を用いて、作成した天気予報エージェントに「東京の明日の天気を教えて」や「大阪の明日の天気を教えて」といったクエリを投げて動作検証を行います。

上部:Gemini アプリの回答 / 下部:作成した AI エージェントの回答

作成した AI エージェントの回答と、Gemini アプリでの回答が共に同じ予報を返していることが確認できました。これらの値は、実際に各天気予報サイトで発表されている予報とも合致していました。

堂原 竜希(記事一覧)

クラウドソリューション部クラウドエクスプローラ課。2023年4月より、G-genにジョイン。

Google Cloud Partner Top Engineer 2023, 2024, 2025に選出 (2024年はRookie of the year、2025年はFellowにも選出)。休みの日はだいたいゲームをしているか、時々自転車で遠出をしています。