Spaces:
Sleeping
Sleeping
fixing errors and adding tools
Browse filesadding tools to the list and fixing errors in the get_weather tool
app.py
CHANGED
|
@@ -9,19 +9,49 @@ from Gradio_UI import GradioUI
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
"""A tool that retrieves the current weather for a city
|
|
|
|
| 14 |
Args:
|
| 15 |
city: the name of the city
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
try:
|
|
|
|
| 18 |
DuckDuckGoSearchTool.setup()
|
| 19 |
-
web_search_tool = DuckDuckGoSearchTool(max_results=
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
-
return f"Error fetching information for city '{city}': {str(e)}"
|
| 25 |
|
| 26 |
|
| 27 |
@tool
|
|
@@ -61,7 +91,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 61 |
|
| 62 |
agent = CodeAgent(
|
| 63 |
model=model,
|
| 64 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 65 |
max_steps=6,
|
| 66 |
verbosity_level=1,
|
| 67 |
grammar=None,
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def get_weather(city: str) -> str: # Fixed comment syntax and function name
|
| 13 |
"""A tool that retrieves the current weather for a city
|
| 14 |
+
|
| 15 |
Args:
|
| 16 |
city: the name of the city
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: Weather information for the specified city, or error message
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
+
# Initialize the search tool
|
| 23 |
DuckDuckGoSearchTool.setup()
|
| 24 |
+
web_search_tool = DuckDuckGoSearchTool(max_results=3, rate_limit=2.0) # Get more results for better info
|
| 25 |
+
|
| 26 |
+
# Search for weather information
|
| 27 |
+
search_query = f"current weather {city} temperature conditions"
|
| 28 |
+
results = web_search_tool(search_query)
|
| 29 |
+
|
| 30 |
+
# Extract and format weather information
|
| 31 |
+
if results and len(results) > 0:
|
| 32 |
+
# Assuming results is a list of search result objects with 'snippet' or 'body' attributes
|
| 33 |
+
weather_info = ""
|
| 34 |
+
for result in results[:2]: # Use top 2 results
|
| 35 |
+
if hasattr(result, 'snippet'):
|
| 36 |
+
weather_info += result.snippet + " "
|
| 37 |
+
elif hasattr(result, 'body'):
|
| 38 |
+
weather_info += result.body + " "
|
| 39 |
+
elif isinstance(result, dict):
|
| 40 |
+
weather_info += result.get('snippet', result.get('body', str(result))) + " "
|
| 41 |
+
|
| 42 |
+
if weather_info.strip():
|
| 43 |
+
return f"Weather in {city}: {weather_info.strip()}"
|
| 44 |
+
else:
|
| 45 |
+
return f"Weather information found for {city}, but couldn't extract details. Raw results: {str(results)}"
|
| 46 |
+
else:
|
| 47 |
+
return f"No weather information found for city '{city}'"
|
| 48 |
+
|
| 49 |
+
except ImportError:
|
| 50 |
+
return f"Error: DuckDuckGoSearchTool not available. Please install required dependencies."
|
| 51 |
+
except AttributeError as e:
|
| 52 |
+
return f"Error: Problem with search tool configuration: {str(e)}"
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"Error fetching weather information for city '{city}': {str(e)}"
|
| 55 |
|
| 56 |
|
| 57 |
@tool
|
|
|
|
| 91 |
|
| 92 |
agent = CodeAgent(
|
| 93 |
model=model,
|
| 94 |
+
tools=[final_answer, get_weather, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
| 95 |
max_steps=6,
|
| 96 |
verbosity_level=1,
|
| 97 |
grammar=None,
|