目標:掌握使用 YAML 檔案進行宣告式代理人設定,定義代理人、工具和行為,而無需撰寫 Python 程式碼。
您將學到:
- 使用
root_agent.yaml建立代理人設定 - 了解
AgentConfig和LlmAgentConfig架構 - 在 YAML 中設定工具、模型和指令
- 設定檔中的多代理人系統
- 載入和驗證設定
tutorial20/
├── root_agent.yaml # 代理人設定
├── run_agent.py # 執行腳本
├── tools/ # 工具實作
│ ├── __init__.py
│ └── customer_tools.py
├── tests/ # 綜合測試
│ ├── test_agent.py
│ ├── test_tools.py
│ ├── test_imports.py
│ └── test_structure.py
├── pyproject.toml # 套件設定
├── requirements.txt # 依賴項
└── Makefile # 建置指令
sequenceDiagram
participant User
participant Runner
participant Agent
participant SessionService
User->>Runner: 執行 main()
Runner->>Agent: config_agent_utils.from_config('root_agent.yaml')
activate Agent
Agent-->>Runner: 代理人實例
deactivate Agent
Runner->>SessionService: 建立 InMemorySessionService
Runner->>SessionService: create_session()
loop 對於每個查詢
User->>Runner: 發送查詢 (run_async)
Runner->>Agent: 處理訊息
activate Agent
Agent->>Agent: 思考/調用工具
Agent-->>Runner: 回應事件
deactivate Agent
Runner-->>User: 顯示結果
end
make setupmake validate-configmake dev在瀏覽器中開啟 http://localhost:8000 並從下拉選單中選擇 'customer_support'。
在 ADK 網頁 UI 中嘗試這些提示:
- "我是客戶 CUST-001,我想查詢我的訂單 ORD-001"
- "我需要協助解決登入錯誤"
- "我想要訂單 ORD-002 的退款 $75"
root_agent.yaml 定義了一個客戶支援系統,包含:
- 單一代理人:具有完整工具的客戶支援代理人
- 工具:11 個用於客戶服務操作的功能
make test測試涵蓋:
- YAML 設定載入
- 工具功能實作
- 代理人建立和驗證
- 專案結構驗證
使用測試查詢直接執行代理人:
python run_agent.pyname: agent_name
model: gemini-2.0-flash
description: "代理人用途"
instruction: |
代理人的
多行指令
generate_content_config:
temperature: 0.7
max_output_tokens: 2048
tools:
- type: function
name: tool_name
description: "工具描述"
sub_agents:
- name: sub_agent_1
model: gemini-2.0-flash
# ... 子代理人設定工具是 Python 函數,回傳結構化的字典:
def tool_name(param: Type) -> Dict[str, Any]:
return {
'status': 'success',
'report': '人類可讀的訊息',
'data': { ... } # 工具特定資料
}export GOOGLE_API_KEY=您的_api_key在此獲取免費金鑰:https://aistudio.google.com/app/apikey
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
export GOOGLE_CLOUD_PROJECT=您的_project_id# 驗證 YAML 語法
python -c "import yaml; yaml.safe_load(open('root_agent.yaml'))"
# 測試代理人載入
python -c "from google.adk.agents import config_agent_utils; config_agent_utils.from_config('root_agent.yaml')"# 測試工具匯入
python -c "from tools.customer_tools import check_customer_status; print('OK')"確保您擁有:
GOOGLE_API_KEY環境變數,或GOOGLE_APPLICATION_CREDENTIALS和GOOGLE_CLOUD_PROJECT變數
編輯 root_agent.yaml 以:
- 更改模型參數
- 新增工具
- 修改代理人指令
- 新增子代理人
- 在
tools/customer_tools.py中實作函數 - 在
tools/__init__.py匯出中新增 - 在
root_agent.yaml中引用
建立多個 YAML 檔案:
config/dev/root_agent.yamlconfig/prod/root_agent.yaml
載入方式:config_agent_utils.from_config('config/dev/root_agent.yaml')
- 宣告式設定:在 YAML 中定義代理人,而非程式碼
- 工具函數:在 YAML 中按名稱引用的 Python 函數
- 多代理人系統:協調器 + 專門的子代理人
- 設定驗證:部署前測試設定
- 環境管理:開發/預備/生產環境的分離設定
- 教學 21:學習多模態與影像生成
- 教學 22:掌握模型選擇與最佳化
- 教學 23:探索生產部署
- 核心概念:透過 YAML 檔案進行宣告式代理人設定 (Declarative Agent Configuration),將代理人定義與程式碼實作分離。
- 關鍵技術:YAML,
AgentConfig,LlmAgentConfig, ADK Configuration Utils。 - 重要結論:宣告式設定提高了代理人定義的可讀性和維護性,支援多環境配置,並簡化了多代理人系統的建構。
- 行動項目:
- 學習 YAML 設定語法。
- 實作並註冊工具函數。
- 建立與驗證
root_agent.yaml。