From 6dfc33712668b5fbdf89d0bdc8764509fd4359b4 Mon Sep 17 00:00:00 2001 From: Rishi Pradeepkumar Date: Wed, 8 Apr 2026 17:02:34 +0530 Subject: [PATCH] Fix Go multi-tool tutorial snippet to include two tools --- .../get-started/multi_tool_agent/main.go | 72 +++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/examples/go/snippets/get-started/multi_tool_agent/main.go b/examples/go/snippets/get-started/multi_tool_agent/main.go index 516aaa9f7..e237d98ca 100644 --- a/examples/go/snippets/get-started/multi_tool_agent/main.go +++ b/examples/go/snippets/get-started/multi_tool_agent/main.go @@ -19,6 +19,8 @@ import ( "context" "log" "os" + "strings" + "time" "google.golang.org/genai" @@ -28,9 +30,13 @@ import ( "google.golang.org/adk/cmd/launcher/full" "google.golang.org/adk/model/gemini" "google.golang.org/adk/tool" - "google.golang.org/adk/tool/geminitool" + "google.golang.org/adk/tool/functiontool" ) +type CityArgs struct { + City string `json:"city"` +} + func main() { ctx := context.Background() @@ -43,14 +49,70 @@ func main() { log.Fatalf("Failed to create model: %v", err) } + weatherTool, err := functiontool.New[CityArgs, map[string]any]( + functiontool.Config{ + Name: "get_weather", + Description: "Retrieves the current weather report for a specified city.", + }, + func(ctx tool.Context, args CityArgs) (map[string]any, error) { + if strings.EqualFold(args.City, "new york") { + return map[string]any{ + "status": "success", + "report": "The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees Fahrenheit).", + }, nil + } + return map[string]any{ + "status": "error", + "error_message": "Weather information for '" + args.City + "' is not available.", + }, nil + }, + ) + if err != nil { + log.Fatalf("Failed to create get_weather tool: %v", err) + } + + currentTimeTool, err := functiontool.New[CityArgs, map[string]any]( + functiontool.Config{ + Name: "get_current_time", + Description: "Returns the current time in a specified city.", + }, + func(ctx tool.Context, args CityArgs) (map[string]any, error) { + var tzIdentifier string + if strings.EqualFold(args.City, "new york") { + tzIdentifier = "America/New_York" + } else { + return map[string]any{ + "status": "error", + "error_message": "Sorry, I don't have timezone information for " + args.City + ".", + }, nil + } + + tz, err := time.LoadLocation(tzIdentifier) + if err != nil { + return nil, err + } + + now := time.Now().In(tz) + report := "The current time in " + args.City + " is " + now.Format("2006-01-02 15:04:05 MST-0700") + return map[string]any{ + "status": "success", + "report": report, + }, nil + }, + ) + if err != nil { + log.Fatalf("Failed to create get_current_time tool: %v", err) + } + // 2. Define the agent. a, err := llmagent.New(llmagent.Config{ - Name: "multi_tool_agent", + Name: "weather_time_agent", Model: model, - Description: "An agent that can answer questions using Google Search.", - Instruction: "You are a helpful assistant. Use the available tools to answer questions.", + Description: "Agent to answer questions about the time and weather in a city.", + Instruction: "You are a helpful agent who can answer user questions about the time and weather in a city.", Tools: []tool.Tool{ - geminitool.GoogleSearch{}, + weatherTool, + currentTimeTool, }, }) if err != nil {