Spaces:
Sleeping
Sleeping
Add weather, math solver, and joke generator tools to agent
Browse files- Implemented `get_weather` tool to fetch current weather for a city
- Added `solve_math` tool to evaluate basic mathematical expressions
- Integrated `get_joke` tool to fetch random jokes from an API
- Updated `CodeAgent` to include the new tools
- Ensured proper exception handling and API response formatting
app.py
CHANGED
@@ -1,22 +1,48 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
-
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
|
17 |
-
arg2: the second argument
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -36,22 +62,23 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
38 |
model = HfApiModel(
|
39 |
-
max_tokens=2096,
|
40 |
-
temperature=0.5,
|
41 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
42 |
-
custom_role_conversions=None,
|
43 |
)
|
44 |
|
45 |
-
|
46 |
-
# Import tool from Hub
|
47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
48 |
|
|
|
49 |
with open("prompts.yaml", 'r') as stream:
|
50 |
prompt_templates = yaml.safe_load(stream)
|
51 |
-
|
|
|
52 |
agent = CodeAgent(
|
53 |
model=model,
|
54 |
-
tools=[final_answer
|
55 |
max_steps=6,
|
56 |
verbosity_level=1,
|
57 |
grammar=None,
|
@@ -61,5 +88,5 @@ agent = CodeAgent(
|
|
61 |
prompt_templates=prompt_templates
|
62 |
)
|
63 |
|
64 |
-
|
65 |
-
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
7 |
from Gradio_UI import GradioUI
|
8 |
|
9 |
+
# Custom Tools
|
10 |
+
|
11 |
+
@tool
|
12 |
+
def get_weather(city: str) -> str:
|
13 |
+
"""Fetches the current weather for a given city.
|
14 |
+
Args:
|
15 |
+
city: The name of the city.
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
response = requests.get(f"https://wttr.in/{city}?format=%C+%t")
|
19 |
+
return f"The current weather in {city} is: {response.text}"
|
20 |
+
except Exception as e:
|
21 |
+
return f"Error fetching weather for {city}: {str(e)}"
|
22 |
+
|
23 |
+
|
24 |
@tool
|
25 |
+
def solve_math(expression: str) -> str:
|
26 |
+
"""Solves basic math expressions.
|
|
|
27 |
Args:
|
28 |
+
expression: A math expression in string format (e.g., "2+2*3").
|
|
|
29 |
"""
|
30 |
+
try:
|
31 |
+
result = eval(expression, {"__builtins__": None}, {})
|
32 |
+
return f"The result is {result}"
|
33 |
+
except Exception as e:
|
34 |
+
return f"Error solving math expression '{expression}': {str(e)}"
|
35 |
+
|
36 |
+
|
37 |
+
@tool
|
38 |
+
def get_joke() -> str:
|
39 |
+
"""Fetches a random joke."""
|
40 |
+
try:
|
41 |
+
response = requests.get("https://official-joke-api.appspot.com/random_joke").json()
|
42 |
+
return f"{response['setup']} {response['punchline']}"
|
43 |
+
except Exception as e:
|
44 |
+
return f"Error fetching joke: {str(e)}"
|
45 |
+
|
46 |
|
47 |
@tool
|
48 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
62 |
|
63 |
final_answer = FinalAnswerTool()
|
64 |
model = HfApiModel(
|
65 |
+
max_tokens=2096,
|
66 |
+
temperature=0.5,
|
67 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Update this if needed
|
68 |
+
custom_role_conversions=None,
|
69 |
)
|
70 |
|
71 |
+
# Import tool from Hugging Face Hub
|
|
|
72 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
73 |
|
74 |
+
# Load prompt templates
|
75 |
with open("prompts.yaml", 'r') as stream:
|
76 |
prompt_templates = yaml.safe_load(stream)
|
77 |
+
|
78 |
+
# Initialize agent with custom tools
|
79 |
agent = CodeAgent(
|
80 |
model=model,
|
81 |
+
tools=[final_answer, get_weather, solve_math, get_joke, get_current_time_in_timezone], # Added new tools
|
82 |
max_steps=6,
|
83 |
verbosity_level=1,
|
84 |
grammar=None,
|
|
|
88 |
prompt_templates=prompt_templates
|
89 |
)
|
90 |
|
91 |
+
# Launch UI
|
92 |
+
GradioUI(agent).launch()
|