ADK Go 入門 04 - 讓 Agent 擁有工具能力(Go)
Agent 光靠 LLM 只能產生文字,透過 Tool 才能真正執行動作:查資料庫、呼叫 API、計算數值。本篇說明如何在 Go 中定義工具並掛載到 Agent 上。
| 概念 |
說明 |
適用情境 |
tool.Tool |
單一可呼叫函式 |
自訂邏輯、內建工具(如 GoogleSearch) |
tool.Toolset |
多個 Tool 的集合 |
外部系統(MCP 伺服器、API 組) |
ADK Go SDK 使用 functiontool.New 建立工具,透過泛型自動推斷 schema,handler 接收強型別的 struct 參數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import ( "google.golang.org/adk/tool" "google.golang.org/adk/tool/functiontool" )
type WeatherArgs struct { City string `json:"city" jsonschema:"description=城市名稱"` }
type WeatherResult struct { Weather string `json:"weather"` City string `json:"city"` }
weatherTool, err := functiontool.New[WeatherArgs, WeatherResult]( functiontool.Config{ Name: "get_weather", Description: "查詢指定城市的天氣", }, func(ctx tool.Context, args WeatherArgs) (WeatherResult, error) { return WeatherResult{Weather: "晴天", City: args.City}, nil }, )
|
將工具掛載到 Agent:
1 2 3 4 5
| myAgent, _ := llmagent.New(llmagent.Config{ Name: "weather_agent", Model: llm, Tools: []tool.Tool{weatherTool}, })
|
當 Toolset 提供數十個工具時,某個 Agent 可能只需要其中幾個。使用 tool.FilterToolset 限制範圍:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
filteredToolset := tool.FilterToolset( fullToolset, tool.StringPredicate([]string{ "get_product", "list_products", "update_product", }), )
productAgent, _ := llmagent.New(llmagent.Config{ Name: "product_agent", Model: llm, Toolsets: []tool.Toolset{filteredToolset}, })
|
⚠️ 最小權限原則:每個 Agent 只給它實際需要的工具。工具過多會讓 LLM 選錯工具,也增加安全風險。
Agent 可以把另一個 Agent 包裝成 Tool,讓被呼叫的 Agent 以工具身份執行後回傳結果(而非完全移交控制權):
1 2 3 4 5 6 7 8 9 10
| import "google.golang.org/adk/tool/agenttool"
visionTool := agenttool.New(visionAgent, nil)
parentAgent, _ := llmagent.New(llmagent.Config{ Name: "parent_agent", Model: llm, Tools: []tool.Tool{visionTool}, })
|
這和 transfer_to_agent(完全移交)不同,適合「呼叫後取得結果,繼續由自己處理」的場景。