|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Full example code for the basic weather agent |
| 16 | +# --- Full example code demonstrating LlmAgent with Tools --- |
| 17 | +import os |
| 18 | + |
| 19 | +from google.adk.agents import LlmAgent |
| 20 | +from google.adk.runners import Runner |
| 21 | +from google.adk.sessions import InMemorySessionService |
| 22 | +from google.adk.models.lite_llm import LiteLlm |
| 23 | +from google.genai import types |
| 24 | +from litellm.rerank_api.main import httpx |
| 25 | +from pydantic import BaseModel, Field |
| 26 | + |
| 27 | +# --- 1. Define Schemas --- |
| 28 | + |
| 29 | +# Input schema used by both agents |
| 30 | +class CityInput(BaseModel): |
| 31 | + city: str = Field(description="The city to get information about.") |
| 32 | + |
| 33 | +# --- 2. Define the Tool (Only for the first agent) --- |
| 34 | +def get_weather(city: str) -> str: |
| 35 | + """Retrieves the weather condition of a given city.""" |
| 36 | + print(f"\n-- Tool Call: get_weather(city='{city}') --") |
| 37 | + city_weather = { |
| 38 | + "paris": "The weather in Paris is sunny with a temperature of 25 degrees Celsius (77 degrees Fahrenheit).", |
| 39 | + "ottawa": "In Ottawa, it's currently cloudy with a temperature of 18 degrees Celsius (64 degrees Fahrenheit) and a chance of rain.", |
| 40 | + "tokyo": "Tokyo sees humid conditions with a high of 28 degrees Celsius (82 degrees Fahrenheit) and possible rainfall." |
| 41 | + } |
| 42 | + result = city_weather.get(city.strip().lower(), f"Sorry, I don't have weather information in {city}.") |
| 43 | + print(f"-- Tool Result: '{result}' --") |
| 44 | + return result |
| 45 | + |
| 46 | +# --- 3. Configure Agent --- |
| 47 | +# Connect to the deployed model by using LiteLlm |
| 48 | +api_base_url = os.getenv("LLM_BASE_URL", "http://vllm-llama3-service:8000/v1") |
| 49 | +model_name_at_endpoint = os.getenv("MODEL_NAME", "hosted_vllm/meta-llama/Llama-3.1-8B-Instruct") |
| 50 | +model = LiteLlm( |
| 51 | + model=model_name_at_endpoint, |
| 52 | + api_base=api_base_url, |
| 53 | +) |
| 54 | + |
| 55 | +# Uses a tool and output_key |
| 56 | +weather_agent = LlmAgent( |
| 57 | + model=model, |
| 58 | + name="weather_agent_tool", |
| 59 | + description="Retrieves weather in a city using a specific tool.", |
| 60 | + instruction="""You are a helpful agent that provides weather report in a city using a tool. |
| 61 | +The user will provide a city name in a JSON format like {"city": "city_name"}. |
| 62 | +1. Extract the city name. |
| 63 | +2. Use the `get_weather` tool to find the weather. Don't use other tools! |
| 64 | +3. Answer on user request based on the weather |
| 65 | +""", |
| 66 | + tools=[get_weather], |
| 67 | + input_schema=CityInput, |
| 68 | + output_key="city_weather_tool_result", # Store final text response |
| 69 | +) |
| 70 | + |
| 71 | +root_agent = weather_agent |
0 commit comments