經過前面幾天的練習,我們已經完成了四個獨立的 Sub-Agent:
這篇我們要把這四支 Agent 全部包裝進一個 Master Agent(Root Agent),讓它能根據使用者的輸入,自動判斷需求並選擇合適的 Sub-Agent 來執行。
整個專案的最終目錄如下:
ExampleProject
└─ TravelAgent
├─ __init__.py
├─ HotelContrastAgent.py
├─ HotelSearchAgent.py
├─ TransportAgent.py
├─ ItineraryAgent.py
├─ prompt.py
├─ .env
└─ MasterAgent.py
__init__.py
為了讓整個模組在匯入時能自動載入 root_agent
,我們只需要在 __init__.py
中加入:
from .MasterAgent import root_agent
這樣當導入 TravelAgent
時,就會自動載入我們的 Master Agent。
.env
別忘了在專案根目錄放上 API Key 設定:
GOOGLE_GENAI_USE_VERTEXAI=FALSE
GOOGLE_API_KEY=YOUR_API_KEY
MasterAgent.py
from google.adk.agents import Agent
import os
from dotenv import load_dotenv
from .HotelContrastAgent import hotel_compare_agent
from .HotelSearchAgent import hotel_search_agent
from .TransportAgent import transport_agent
from .ItineraryAgent import itinerary_agent
from . import prompt
load_dotenv()
root_agent = Agent(
name="root_agent",
model="gemini-2.5-flash",
description=prompt.ROOT_AGENT_DESCRIPTION,
instruction=prompt.ROOT_AGENT_INSTRUCTION,
sub_agents=[
hotel_compare_agent,
hotel_search_agent,
itinerary_agent,
transport_agent
],
)
description
ROOT_AGENT_DESCRIPTION = """
This is a root coordination agent that integrates multiple sub-agents, including hotel search, hotel comparison, transport queries, and itinerary planning.
"""
這裡簡單的說明了,Master Agent 是整個系統的中樞,負責協調與整合所有子代理。
instruction
ROOT_AGENT_INSTRUCTION = """
You are a root agent responsible for coordinating sub-agents and integrating their responses. You can use the following sub-agents:
1. **Hotel Search Agent**
- **Purpose:** Searches hotels by city, returning up to 3 results.
- **Parameters:**
- `city` (str): The target city for hotel search.
- **Example Input:**
```json
{ "city": "Tokyo" }
```
2. **Hotel Compare Agent**
- **Purpose:** Compares two hotels based on price or rating, recommending the best one.
- **Parameters:**
- `hotel1` (str): The first hotel name.
- `hotel2` (str): The second hotel name.
- `criterion` (str): Comparison basis, either `"cheapest"` or `"best_rating"`.
- **Example Input:**
```json
{ "hotel1": "Hotel Sakura", "hotel2": "Hotel Fuji", "criterion": "cheapest" }
```
3. **Transport Agent**
- **Purpose:** Provides available transport options between an origin and destination.
- **Parameters:**
- `start` (str): Starting location.
- `end` (str): Destination location.
- **Example Input:**
```json
{ "start": "Taipei", "end": "Kaohsiung" }
```
4. **Itinerary Agent**
- **Purpose:** Generates travel itineraries based on city and number of days.
- **Parameters:**
- `city` (str): The target city for the itinerary.
- `days` (int): Number of days to plan.
- **Example Input:**
```json
{ "city": "Seoul", "days": 3 }
```
Your responsibilities:
- Analyze the user’s input and decide which sub-agent(s) should handle the request.
- Integrate outputs into a single coherent, helpful response.
- Return an error if the request cannot be handled by any sub-agent.
Dispatch guidelines:
- Hotel-related queries → Hotel Search or Hotel Compare Agent
- Transport-related queries → Transport Agent
- Itinerary-related queries → Itinerary Agent
Always respond in a natural, helpful, and user-friendly manner.
"""
這樣,我們的 Master Agent 就能自動根據使用者問題選擇對應的 Sub-Agent。
整合完成後,我們就可以啟動測試:
adk web --port 5000
記得先切換到最外層資料夾(
ExampleProject
)
接著開啟瀏覽器,就能測試每個 Sub-Agent 是否都能被 Master Agent 正確調用。