File size: 1,365 Bytes
6b5d8c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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