From 6ce41ef318e64212c226884323c425d46a98894e Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Mon, 18 Aug 2025 21:26:59 +0200 Subject: [PATCH] feat: Initial tutorials/city-time-weather --- dev/pom.xml | 2 +- pom.xml | 15 +-- tutorials/city-time-weather/README.md | 7 ++ tutorials/city-time-weather/pom.xml | 41 ++++++++ .../google/adk/tutorials/CityTimeWeather.java | 95 +++++++++++++++++++ 5 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 tutorials/city-time-weather/README.md create mode 100644 tutorials/city-time-weather/pom.xml create mode 100644 tutorials/city-time-weather/src/main/java/com/google/adk/tutorials/CityTimeWeather.java diff --git a/dev/pom.xml b/dev/pom.xml index b6e16b45..c3207e39 100644 --- a/dev/pom.xml +++ b/dev/pom.xml @@ -54,7 +54,7 @@ com.google.adk google-adk - 0.2.1-SNAPSHOT + ${project.version} org.springframework.boot diff --git a/pom.xml b/pom.xml index 7801d476..147c7ce8 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ dev maven_plugin contrib/langchain4j + tutorials/city-time-weather @@ -181,13 +182,13 @@ org.sonatype.central - central-publishing-maven-plugin - 0.8.0 - true - - central - - + central-publishing-maven-plugin + 0.8.0 + true + + central + + diff --git a/tutorials/city-time-weather/README.md b/tutorials/city-time-weather/README.md new file mode 100644 index 00000000..10360a1a --- /dev/null +++ b/tutorials/city-time-weather/README.md @@ -0,0 +1,7 @@ +# City Time Weather + +```shell +GEMINI_API_KEY={YOUR-KEY} ../../mvnw exec:java +``` + +See https://google.github.io/adk-docs/get-started/quickstart/#java for more information. diff --git a/tutorials/city-time-weather/pom.xml b/tutorials/city-time-weather/pom.xml new file mode 100644 index 00000000..de2afb14 --- /dev/null +++ b/tutorials/city-time-weather/pom.xml @@ -0,0 +1,41 @@ + + + + 4.0.0 + + + com.google.adk + google-adk-parent + 0.2.1-SNAPSHOT + + + google-adk-tutorials-city-time-weather + + + com.google.adk.web.AdkWebServer + + + + + com.google.adk + google-adk-dev + ${project.version} + + + diff --git a/tutorials/city-time-weather/src/main/java/com/google/adk/tutorials/CityTimeWeather.java b/tutorials/city-time-weather/src/main/java/com/google/adk/tutorials/CityTimeWeather.java new file mode 100644 index 00000000..eb089a80 --- /dev/null +++ b/tutorials/city-time-weather/src/main/java/com/google/adk/tutorials/CityTimeWeather.java @@ -0,0 +1,95 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.adk.tutorials; + +import com.google.adk.agents.BaseAgent; +import com.google.adk.agents.LlmAgent; +import com.google.adk.tools.Annotations.Schema; +import com.google.adk.tools.FunctionTool; +import java.text.Normalizer; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Map; + +public class CityTimeWeather { + + public static final BaseAgent ROOT_AGENT = + LlmAgent.builder() + .name("multi_tool_agent") + .model("gemini-2.0-flash-lite") + .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( + FunctionTool.create(CityTimeWeather.class, "getCurrentTime"), + FunctionTool.create(CityTimeWeather.class, "getWeather")) + .build(); + + public static Map getCurrentTime( + @Schema( + name = "city", + description = "The name of the city for which to retrieve the current time") + String city) { + String normalizedCity = + Normalizer.normalize(city, Normalizer.Form.NFD) + .trim() + .toLowerCase() + .replaceAll("(\\p{IsM}+|\\p{IsP}+)", "") + .replaceAll("\\s+", "_"); + + return ZoneId.getAvailableZoneIds().stream() + .filter(zid -> zid.toLowerCase().endsWith("/" + normalizedCity)) + .findFirst() + .map( + zid -> + Map.of( + "status", + "success", + "report", + "The current time in " + + city + + " is " + + ZonedDateTime.now(ZoneId.of(zid)) + .format(DateTimeFormatter.ofPattern("HH:mm")) + + ".")) + .orElse( + Map.of( + "status", + "error", + "report", + "Sorry, I don't have timezone information for " + city + ".")); + } + + public static Map getWeather( + @Schema( + name = "city", + description = "The name of the city for which to retrieve the weather report") + String city) { + if (city.equalsIgnoreCase("new york")) { + return Map.of( + "status", + "success", + "report", + "The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees" + + " Fahrenheit)."); + + } else { + return Map.of( + "status", "error", "report", "Weather information for " + city + " is not available."); + } + } +}