zakk249 commited on
Commit
4d378f1
·
verified ·
1 Parent(s): 8003cfd

Create get_weather.py

Browse files
Files changed (1) hide show
  1. tools/get_weather.py +47 -0
tools/get_weather.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from smolagents.tools import Tool
3
+ import os
4
+
5
+ class GetWeatherTool(Tool):
6
+ name = "get_weather"
7
+ description = "Fetches current weather and local time for a given city."
8
+
9
+ inputs = {
10
+ "city": {
11
+ "type": "string",
12
+ "description": "A valid city name (e.g., 'Kigali', 'London')"
13
+ }
14
+ }
15
+
16
+ output_type = "string"
17
+
18
+ def forward(self, city: str) -> str:
19
+ url = "https://api.weatherapi.com/v1/current.json"
20
+
21
+ API_KEY = "6e2a665c311f491da3f53514262004"
22
+
23
+ params = {
24
+ "key": API_KEY,
25
+ "q": city,
26
+ "aqi": "no"
27
+ }
28
+
29
+ try:
30
+ response = requests.get(url, params=params, timeout=5)
31
+ response.raise_for_status()
32
+ data = response.json()
33
+
34
+ return (
35
+ f"Weather in {data['location']['name']}, {data['location']['country']}:\n"
36
+ f"- Temperature: {data['current']['temp_c']}°C\n"
37
+ f"- Feels like: {data['current']['feelslike_c']}°C\n"
38
+ f"- Condition: {data['current']['condition']['text']}\n"
39
+ f"- Humidity: {data['current']['humidity']}%\n"
40
+ f"- Local time: {data['location']['localtime']}"
41
+ )
42
+
43
+ except Exception as e:
44
+ return f"Error fetching weather for city '{city}': {str(e)}"
45
+
46
+ def __init__(self, *args, **kwargs):
47
+ self.is_initialized = False