|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.adk.tutorials; |
| 17 | + |
| 18 | +import com.google.adk.agents.BaseAgent; |
| 19 | +import com.google.adk.agents.LlmAgent; |
| 20 | +import com.google.adk.tools.Annotations.Schema; |
| 21 | +import com.google.adk.tools.FunctionTool; |
| 22 | +import java.text.Normalizer; |
| 23 | +import java.time.ZoneId; |
| 24 | +import java.time.ZonedDateTime; |
| 25 | +import java.time.format.DateTimeFormatter; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +public class CityTimeWeather { |
| 29 | + |
| 30 | + private static String USER_ID = "student"; |
| 31 | + private static String NAME = "multi_tool_agent"; |
| 32 | + |
| 33 | + public static final BaseAgent ROOT_AGENT = initAgent(); |
| 34 | + |
| 35 | + public static BaseAgent initAgent() { |
| 36 | + return LlmAgent.builder() |
| 37 | + .name(NAME) |
| 38 | + .model("gemini-2.0-flash-lite") |
| 39 | + .description("Agent to answer questions about the time and weather in a city.") |
| 40 | + .instruction( |
| 41 | + "You are a helpful agent who can answer user questions about the time and weather" |
| 42 | + + " in a city.") |
| 43 | + .tools( |
| 44 | + FunctionTool.create(CityTimeWeather.class, "getCurrentTime"), |
| 45 | + FunctionTool.create(CityTimeWeather.class, "getWeather")) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + public static Map<String, String> getCurrentTime( |
| 50 | + @Schema( |
| 51 | + name = "city", |
| 52 | + description = "The name of the city for which to retrieve the current time") |
| 53 | + String city) { |
| 54 | + String normalizedCity = |
| 55 | + Normalizer.normalize(city, Normalizer.Form.NFD) |
| 56 | + .trim() |
| 57 | + .toLowerCase() |
| 58 | + .replaceAll("(\\p{IsM}+|\\p{IsP}+)", "") |
| 59 | + .replaceAll("\\s+", "_"); |
| 60 | + |
| 61 | + return ZoneId.getAvailableZoneIds().stream() |
| 62 | + .filter(zid -> zid.toLowerCase().endsWith("/" + normalizedCity)) |
| 63 | + .findFirst() |
| 64 | + .map( |
| 65 | + zid -> |
| 66 | + Map.of( |
| 67 | + "status", |
| 68 | + "success", |
| 69 | + "report", |
| 70 | + "The current time in " |
| 71 | + + city |
| 72 | + + " is " |
| 73 | + + ZonedDateTime.now(ZoneId.of(zid)) |
| 74 | + .format(DateTimeFormatter.ofPattern("HH:mm")) |
| 75 | + + ".")) |
| 76 | + .orElse( |
| 77 | + Map.of( |
| 78 | + "status", |
| 79 | + "error", |
| 80 | + "report", |
| 81 | + "Sorry, I don't have timezone information for " + city + ".")); |
| 82 | + } |
| 83 | + |
| 84 | + public static Map<String, String> getWeather( |
| 85 | + @Schema( |
| 86 | + name = "city", |
| 87 | + description = "The name of the city for which to retrieve the weather report") |
| 88 | + String city) { |
| 89 | + if (city.toLowerCase().equals("new york")) { |
| 90 | + return Map.of( |
| 91 | + "status", |
| 92 | + "success", |
| 93 | + "report", |
| 94 | + "The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees" |
| 95 | + + " Fahrenheit)."); |
| 96 | + |
| 97 | + } else { |
| 98 | + return Map.of( |
| 99 | + "status", "error", "report", "Weather information for " + city + " is not available."); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments