Tool Implementation Example
What's Included
Tool Functions
Weather Tool Implementation
async def get_weather(location, session):
"""Get weather information using Open-Meteo API."""
if not location or not isinstance(location, str):
raise ValueError("Invalid location")
# Geocoding request to resolve location
geocode_url = "https://geocoding-api.open-meteo.com/v1/search"
geocode_params = {
"name": location,
"count": 1,
"language": "en",
"format": "json",
}
async with session.get(geocode_url, params=geocode_params, timeout=20) as response:
geo_data = await response.json()
results = geo_data.get("results", [])
if not results:
raise ValueError(f"No results found for location: {location}")
first = results[0]
latitude = first["latitude"]
longitude = first["longitude"]
resolved = {
"name": first["name"],
"country": first["country"],
"admin1": first.get("admin1"),
"latitude": latitude,
"longitude": longitude,
"timezone": first.get("timezone"),
}
# Weather forecast request
forecast_url = "https://api.open-meteo.com/v1/forecast"
forecast_params = {
"latitude": latitude,
"longitude": longitude,
"current": "temperature_2m,apparent_temperature,relative_humidity_2m,weather_code,wind_speed_10m",
"wind_speed_unit": "kmh",
"timezone": "auto",
}
async with session.get(forecast_url, params=forecast_params, timeout=20) as response:
forecast_data = await response.json()
current = forecast_data.get("current")
units = forecast_data.get("current_units")
if not current:
raise ValueError("Weather data unavailable")
return {
"locationQueried": location,
"resolvedLocation": resolved,
"current": current,
"units": units,
"source": "open-meteo.com",
}Time Tool Implementation
Web Search Tool Implementation
Tool Schema Definition
System Prompt
Response Parsing
Tool Execution Framework
Customizing Tools for Your Application
Last updated