Weather Tool with multiple arguments
Have started looking into google's A2A SDK. While going through the getting started guide as provided by google, was stuck with function call, with a function having multiple arguments, Function call example as provided by google was helpful, but it was not clear how to pass the multiple arguments while calling the tool. Spent two days googling for the answer without luck.
Option 1 - Calling Weather Tool using a simple query
First option that worked was just pass a single query as argument with the query containing parameters potentially matching to the function arguments. The tool/function that was used for testing is:
# @title Define the get_weather Tool
def get_weather(city: str, country: str, unit: str) -> dict:
"""Retrieves the current weather report for a specified city.
Args:
city (str): The name of the city (e.g., "New York", "London", "Tokyo").
country (str): The name of the county (e.g., "England", "Great Britan", "USA", "United States", "Japan"
unit (str): The temperature unit, either 'Celsius' or 'Fahrenheit'.
Returns:
dict: A dictionary containing the weather information.
Includes a 'status' key ('success' or 'error').
If 'success', includes a 'report' key with weather details.
If 'error', includes an 'error_message' key.
"""
print(f"--- Tool: get_weather called for city: {city} ---") # Log tool execution
print(f"country: {country}")
print(f"unit: {unit}")
city_normalized = city.lower().replace(" ", "") # Basic normalization
# Mock weather data
mock_weather_db = {
"newyork": {"status": "success", "report": "The weather in New York is sunny with a temperature of 25°C."},
"london": {"status": "success", "report": "It's cloudy in London with a temperature of 15°C."},
"tokyo": {"status": "success", "report": "Tokyo is experiencing light rain and a temperature of 18°C."},
}
if city_normalized in mock_weather_db:
return mock_weather_db[city_normalized]
else:
return {"status": "error", "error_message": f"Sorry, I don't have weather information for '{city}'."}
Code used while calling the agent is:
Query used
The output from the execution was:
>>> User Query: What is the weather like in London Britan? Temperature must be in Fahrenheit
--- Tool: get_weather called for city: London ---
country: Great Britan
unit: Fahrenheit
<<< Agent Response: I am sorry. I cannot fulfill this request. The weather report I received is in Celsius, but you asked for Fahrenheit. I am unable to convert units.
Based on the results it is my understanding that the agent able extract required parameters to call the function from the query provided. Since the implementation was independent of the unit, even though the parameter was not used anywhere in the function other than printing, it is not clear why the agent is looking for a temperature converter.
Option 2 - Function Call
After stumbling across python api, specially
from google.genai import types
contents = types.Part.from_function_call(
name='get_weather_by_location',
args={'location': 'Boston'}
)
I modified content generation using the following code:
part = types.Part.from_function_call(
name='get_weather',
args={'city': 'London', 'unit': 'Fahrenheit'}
)
Output from the code execution was:
>>> User Query: What is the weather like in London Britan? Temperature must be in Fahrenheit
--- Tool: get_weather called for city: London ---
country: United Kingdom
unit: Fahrenheit
<<< Agent Response: OK. The weather in London, United Kingdom is cloudy with a temperature of 15°C.
which is inline with what was expected by the execution of a function. No mention of temperature converter tool in the output.
Question
It is not clear why the difference in the results is different in the two cases?
No comments:
Post a Comment