cjb97 commited on
Commit
28fd9eb
·
1 Parent(s): 972fb5d

Fix weather tool implementation and improve API response handling

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -9,15 +9,17 @@ import os # Added for accessing environment variables
9
  from typing import Any, Optional
10
  import re
11
 
12
- @tool
13
  def get_weather(location: str) -> str:
14
  """Get the current weather for a specified location.
15
  Args:
16
  location: A string representing a city (e.g., 'New York', 'Paris').
 
 
17
  """
18
  # Validate that location contains only allowed characters: letters, digits, spaces, and hyphens
19
  if not re.fullmatch(r'[A-Za-z0-9\s-]+', location):
20
- return "Error: Location contains invalid characters. Only letters, digits, spaces, and hyphens are allowed. I cannot disambiguate between cities with the same name."
21
  try:
22
  # Get the API key from environment variables
23
  api_key = os.getenv('OPENWEATHERMAP_API_KEY')
@@ -41,17 +43,17 @@ def get_weather(location: str) -> str:
41
  weather_data = weather_response.json()
42
 
43
  if weather_response.status_code == 200:
44
- current = weather_data.get('current', {})
45
- temp = current.get('temp', 'N/A')
46
- weather_desc = current.get('weather', [{}])[0].get('description', 'N/A')
47
- humidity = current.get('humidity', 'N/A')
48
- wind_speed = current.get('wind_speed', 'N/A')
49
- return f"The current weather in {location} is {temp}°C with {weather_desc}. Humidity: {humidity}%, Wind speed: {wind_speed} m/s."
50
  else:
51
  return f"Error fetching weather for {location}: {weather_data.get('message', 'Unknown error')}"
52
  except Exception as e:
53
- return f"Error fetching weather for {location}: {str(e)}"
54
 
 
55
  final_answer = FinalAnswerTool()
56
  duck_duck_go_search = DuckDuckGoSearchTool()
57
 
@@ -75,9 +77,9 @@ with open("prompts.yaml", 'r') as stream:
75
  agent = CodeAgent(
76
  model=model,
77
  tools=[
 
78
  final_answer,
79
  duck_duck_go_search,
80
- get_weather
81
  ],
82
  max_steps=6,
83
  verbosity_level=1,
 
9
  from typing import Any, Optional
10
  import re
11
 
12
+ @tool("get_weather") # Add the tool name explicitly
13
  def get_weather(location: str) -> str:
14
  """Get the current weather for a specified location.
15
  Args:
16
  location: A string representing a city (e.g., 'New York', 'Paris').
17
+ Returns:
18
+ str: A string containing the current weather information.
19
  """
20
  # Validate that location contains only allowed characters: letters, digits, spaces, and hyphens
21
  if not re.fullmatch(r'[A-Za-z0-9\s-]+', location):
22
+ return "Error: Location contains invalid characters. Only letters, digits, spaces, and hyphens are allowed."
23
  try:
24
  # Get the API key from environment variables
25
  api_key = os.getenv('OPENWEATHERMAP_API_KEY')
 
43
  weather_data = weather_response.json()
44
 
45
  if weather_response.status_code == 200:
46
+ temp = weather_data.get('main', {}).get('temp', 'N/A')
47
+ weather_desc = weather_data.get('weather', [{}])[0].get('description', 'N/A')
48
+ humidity = weather_data.get('main', {}).get('humidity', 'N/A')
49
+ wind_speed = weather_data.get('wind', {}).get('speed', 'N/A')
50
+ return f"The current weather in {location} is {temp}°F with {weather_desc}. Humidity: {humidity}%, Wind speed: {wind_speed} mph."
 
51
  else:
52
  return f"Error fetching weather for {location}: {weather_data.get('message', 'Unknown error')}"
53
  except Exception as e:
54
+ return f"Error fetching weather for {location}: {str(e)}"
55
 
56
+ # Initialize tools
57
  final_answer = FinalAnswerTool()
58
  duck_duck_go_search = DuckDuckGoSearchTool()
59
 
 
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
  ],
84
  max_steps=6,
85
  verbosity_level=1,