smolagents-weather-codeagent / tools /get_current_weather.py
VLAIResearchLab's picture
Upload agent
6b5d8c4 verified
from smolagents import Tool
from typing import Any, Optional
class SimpleTool(Tool):
name = "get_current_weather"
description = "Retrieves the current weather description for a specific geographical location."
inputs = {"lat":{"type":"number","description":"The latitude of the location."},"lon":{"type":"number","description":"The longitude of the location."}}
output_type = "string"
def forward(self, lat: float, lon: float) -> str:
"""
Retrieves the current weather description for a specific geographical location.
Args:
lat: The latitude of the location.
lon: The longitude of the location.
Returns:
str: The current weather description for the location, or None if the weather information could not
be retrieved.
"""
import requests
API_KEY = "d8376952ee1e3b3e591cec518a7d41cb"
api_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API_KEY}"
try:
response = requests.get(api_url)
response.raise_for_status()
data = response.json()
return data["weather"][0]["description"]
except (requests.RequestException, ValueError, KeyError) as e:
print(f"Error retrieving weather information: {e}")
return None