cjb97 commited on
Commit
7dc53ba
·
1 Parent(s): 28fd9eb

Fix weather tool implementation and imports

Browse files
Files changed (2) hide show
  1. app.py +2 -1
  2. tools/weather.py +49 -0
app.py CHANGED
@@ -4,6 +4,7 @@ import datetime
4
  import requests
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
8
  import os # Added for accessing environment variables
9
  from typing import Any, Optional
@@ -77,7 +78,7 @@ with open("prompts.yaml", 'r') as stream:
77
  agent = CodeAgent(
78
  model=model,
79
  tools=[
80
- get_weather, # Pass the function directly, not an instance
81
  final_answer,
82
  duck_duck_go_search,
83
  ],
 
4
  import requests
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from tools.weather import get_weather
8
  from Gradio_UI import GradioUI
9
  import os # Added for accessing environment variables
10
  from typing import Any, Optional
 
78
  agent = CodeAgent(
79
  model=model,
80
  tools=[
81
+ get_weather, # Pass the function directly
82
  final_answer,
83
  duck_duck_go_search,
84
  ],
tools/weather.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.tools import tool
2
+ import requests
3
+ import os
4
+ import re
5
+ from typing import Optional
6
+
7
+ @tool
8
+ def get_weather(location: str) -> str:
9
+ """Get the current weather for a specified location.
10
+ Args:
11
+ location: A string representing a city (e.g., 'New York', 'Paris').
12
+ Returns:
13
+ str: A string containing the current weather information.
14
+ """
15
+ # Validate that location contains only allowed characters: letters, digits, spaces, and hyphens
16
+ if not re.fullmatch(r'[A-Za-z0-9\s-]+', location):
17
+ return "Error: Location contains invalid characters. Only letters, digits, spaces, and hyphens are allowed."
18
+ try:
19
+ # Get the API key from environment variables
20
+ api_key = os.getenv('OPENWEATHERMAP_API_KEY')
21
+ if not api_key:
22
+ return "Error: OPENWEATHERMAP_API_KEY not set in environment variables."
23
+
24
+ # First, geocode the location to get latitude and longitude
25
+ geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&limit=1&appid={api_key}"
26
+ geo_response = requests.get(geo_url)
27
+ geo_data = geo_response.json()
28
+ if not geo_data:
29
+ return f"Error: Could not geocode location '{location}'."
30
+ lat = geo_data[0].get('lat')
31
+ lon = geo_data[0].get('lon')
32
+ if lat is None or lon is None:
33
+ return f"Error: Latitude or longitude not found for location '{location}'."
34
+
35
+ # Call the OpenWeatherMap weather API 2.5 endpoint
36
+ weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units=imperial"
37
+ weather_response = requests.get(weather_url)
38
+ weather_data = weather_response.json()
39
+
40
+ if weather_response.status_code == 200:
41
+ temp = weather_data.get('main', {}).get('temp', 'N/A')
42
+ weather_desc = weather_data.get('weather', [{}])[0].get('description', 'N/A')
43
+ humidity = weather_data.get('main', {}).get('humidity', 'N/A')
44
+ wind_speed = weather_data.get('wind', {}).get('speed', 'N/A')
45
+ return f"The current weather in {location} is {temp}°F with {weather_desc}. Humidity: {humidity}%, Wind speed: {wind_speed} mph."
46
+ else:
47
+ return f"Error fetching weather for {location}: {weather_data.get('message', 'Unknown error')}"
48
+ except Exception as e:
49
+ return f"Error fetching weather for {location}: {str(e)}"