Skip to content

Commit 40ec7bc

Browse files
Merge pull request #371 from vorburger:tutorials-city-time-weather
PiperOrigin-RevId: 798869959
2 parents 72e219c + 6ce41ef commit 40ec7bc

File tree

5 files changed

+152
-8
lines changed

5 files changed

+152
-8
lines changed

dev/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<dependency>
5555
<groupId>com.google.adk</groupId>
5656
<artifactId>google-adk</artifactId>
57-
<version>0.2.1-SNAPSHOT</version><!-- {x-version-update:google-adk:current} -->
57+
<version>${project.version}</version>
5858
</dependency>
5959
<dependency>
6060
<groupId>org.springframework.boot</groupId>

pom.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<module>dev</module>
3232
<module>maven_plugin</module>
3333
<module>contrib/langchain4j</module>
34+
<module>tutorials/city-time-weather</module>
3435
</modules>
3536

3637
<properties>
@@ -181,13 +182,13 @@
181182
</plugin>
182183
<plugin>
183184
<groupId>org.sonatype.central</groupId>
184-
<artifactId>central-publishing-maven-plugin</artifactId>
185-
<version>0.8.0</version>
186-
<extensions>true</extensions>
187-
<configuration>
188-
<publishingServerId>central</publishingServerId>
189-
</configuration>
190-
</plugin>
185+
<artifactId>central-publishing-maven-plugin</artifactId>
186+
<version>0.8.0</version>
187+
<extensions>true</extensions>
188+
<configuration>
189+
<publishingServerId>central</publishingServerId>
190+
</configuration>
191+
</plugin>
191192
</plugins>
192193
</pluginManagement>
193194
<plugins>

tutorials/city-time-weather/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# City Time Weather
2+
3+
```shell
4+
GEMINI_API_KEY={YOUR-KEY} ../../mvnw exec:java
5+
```
6+
7+
See https://google.github.io/adk-docs/get-started/quickstart/#java for more information.

tutorials/city-time-weather/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2025 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<parent>
23+
<groupId>com.google.adk</groupId>
24+
<artifactId>google-adk-parent</artifactId>
25+
<version>0.2.1-SNAPSHOT</version><!-- {x-version-update:google-adk:current} -->
26+
</parent>
27+
28+
<artifactId>google-adk-tutorials-city-time-weather</artifactId>
29+
30+
<properties>
31+
<exec.mainClass>com.google.adk.web.AdkWebServer</exec.mainClass>
32+
</properties>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>com.google.adk</groupId>
37+
<artifactId>google-adk-dev</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
40+
</dependencies>
41+
</project>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
public static final BaseAgent ROOT_AGENT =
31+
LlmAgent.builder()
32+
.name("multi_tool_agent")
33+
.model("gemini-2.0-flash-lite")
34+
.description("Agent to answer questions about the time and weather in a city.")
35+
.instruction(
36+
"You are a helpful agent who can answer user questions about the time and weather in a city.")
37+
.tools(
38+
FunctionTool.create(CityTimeWeather.class, "getCurrentTime"),
39+
FunctionTool.create(CityTimeWeather.class, "getWeather"))
40+
.build();
41+
42+
public static Map<String, String> getCurrentTime(
43+
@Schema(
44+
name = "city",
45+
description = "The name of the city for which to retrieve the current time")
46+
String city) {
47+
String normalizedCity =
48+
Normalizer.normalize(city, Normalizer.Form.NFD)
49+
.trim()
50+
.toLowerCase()
51+
.replaceAll("(\\p{IsM}+|\\p{IsP}+)", "")
52+
.replaceAll("\\s+", "_");
53+
54+
return ZoneId.getAvailableZoneIds().stream()
55+
.filter(zid -> zid.toLowerCase().endsWith("/" + normalizedCity))
56+
.findFirst()
57+
.map(
58+
zid ->
59+
Map.of(
60+
"status",
61+
"success",
62+
"report",
63+
"The current time in "
64+
+ city
65+
+ " is "
66+
+ ZonedDateTime.now(ZoneId.of(zid))
67+
.format(DateTimeFormatter.ofPattern("HH:mm"))
68+
+ "."))
69+
.orElse(
70+
Map.of(
71+
"status",
72+
"error",
73+
"report",
74+
"Sorry, I don't have timezone information for " + city + "."));
75+
}
76+
77+
public static Map<String, String> getWeather(
78+
@Schema(
79+
name = "city",
80+
description = "The name of the city for which to retrieve the weather report")
81+
String city) {
82+
if (city.equalsIgnoreCase("new york")) {
83+
return Map.of(
84+
"status",
85+
"success",
86+
"report",
87+
"The weather in New York is sunny with a temperature of 25 degrees Celsius (77 degrees"
88+
+ " Fahrenheit).");
89+
90+
} else {
91+
return Map.of(
92+
"status", "error", "report", "Weather information for " + city + " is not available.");
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)