|
| 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 com.google.adk.web.AdkWebServer; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +public class LiveAudioSingleAgent { |
| 26 | + |
| 27 | + public static final BaseAgent WEATHER_AGENT = |
| 28 | + LlmAgent.builder() |
| 29 | + .name("weather_agent") |
| 30 | + .model("gemini-2.0-flash-live-001") |
| 31 | + .description("A helpful weather assistant that provides weather information.") |
| 32 | + .instruction( |
| 33 | + "You are a friendly weather assistant. When users ask about weather, " |
| 34 | + + "you MUST call the getWeather tool with the location name. " |
| 35 | + + "Extract the location from the user's question. " |
| 36 | + + "ALWAYS use the getWeather tool to get accurate information - never make up weather data. " |
| 37 | + + "After getting the tool result, provide a friendly and descriptive response. " |
| 38 | + + "For general conversation or greetings, respond naturally and helpfully. " |
| 39 | + + "Do NOT use code execution for anything.") |
| 40 | + .tools(FunctionTool.create(LiveAudioSingleAgent.class, "getWeather")) |
| 41 | + .build(); |
| 42 | + |
| 43 | + public static Map<String, String> getWeather( |
| 44 | + @Schema(name = "location", description = "The location for which to retrieve weather") |
| 45 | + String location) { |
| 46 | + |
| 47 | + Map<String, Map<String, String>> weatherData = |
| 48 | + Map.of( |
| 49 | + "new york", |
| 50 | + Map.of( |
| 51 | + "status", |
| 52 | + "success", |
| 53 | + "temperature", |
| 54 | + "72°F (22°C)", |
| 55 | + "condition", |
| 56 | + "Partly cloudy", |
| 57 | + "report", |
| 58 | + "The weather in New York is partly cloudy with a temperature of 72°F (22°C)."), |
| 59 | + "london", |
| 60 | + Map.of( |
| 61 | + "status", |
| 62 | + "success", |
| 63 | + "temperature", |
| 64 | + "59°F (15°C)", |
| 65 | + "condition", |
| 66 | + "Rainy", |
| 67 | + "report", |
| 68 | + "The weather in London is rainy with a temperature of 59°F (15°C)."), |
| 69 | + "tokyo", |
| 70 | + Map.of( |
| 71 | + "status", |
| 72 | + "success", |
| 73 | + "temperature", |
| 74 | + "68°F (20°C)", |
| 75 | + "condition", |
| 76 | + "Clear", |
| 77 | + "report", |
| 78 | + "The weather in Tokyo is clear with a temperature of 68°F (20°C)."), |
| 79 | + "sydney", |
| 80 | + Map.of( |
| 81 | + "status", |
| 82 | + "success", |
| 83 | + "temperature", |
| 84 | + "77°F (25°C)", |
| 85 | + "condition", |
| 86 | + "Sunny", |
| 87 | + "report", |
| 88 | + "The weather in Sydney is sunny with a temperature of 77°F (25°C).")); |
| 89 | + |
| 90 | + String normalizedLocation = location.toLowerCase().trim(); |
| 91 | + |
| 92 | + return weatherData.getOrDefault( |
| 93 | + normalizedLocation, |
| 94 | + Map.of( |
| 95 | + "status", |
| 96 | + "error", |
| 97 | + "report", |
| 98 | + String.format( |
| 99 | + "Weather information for '%s' is not available. Try New York, London, Tokyo, or" |
| 100 | + + " Sydney.", |
| 101 | + location))); |
| 102 | + } |
| 103 | + |
| 104 | + public static void main(String[] args) { |
| 105 | + AdkWebServer.start(WEATHER_AGENT); |
| 106 | + } |
| 107 | +} |
0 commit comments