Skip to content

Commit 561233f

Browse files
committed
feat: Initial tutorials/city-time-weather
1 parent 806e985 commit 561233f

File tree

5 files changed

+159
-8
lines changed

5 files changed

+159
-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>
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.
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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)