Spaces:
Sleeping
Sleeping
Add additional tools and update CodeAgent in app.py
Browse filesUpdated the tools used by the agent in app.py to expand its capabilities. In detail:
- Added the DuckDuckGoSearchTool to perform web searches.
- Loaded the image_generation_tool from the Hub to enable image generation.
- Retained the mandatory FinalAnswerTool.
- Kept the dummy tool my_custom_tool for users to customize as needed.
- Included the get_current_time_in_timezone tool to fetch the local time for a given timezone.
- Updated the "tools" parameter in the CodeAgent creation to include all the new tools.
These changes facilitate experimentation and customization of the agent, enabling it to perform tasks like web searches, image generation, and time checking, among others.
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
@@ -7,25 +7,26 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
# Below is an example of a tool
|
11 |
@tool
|
12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
13 |
-
|
14 |
-
|
15 |
Args:
|
16 |
arg1: the first argument
|
17 |
arg2: the second argument
|
18 |
"""
|
19 |
-
return "What magic will you build
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
24 |
Args:
|
25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
"""
|
27 |
try:
|
28 |
-
# Create timezone object
|
29 |
tz = pytz.timezone(timezone)
|
30 |
# Get current time in that timezone
|
31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
@@ -33,29 +34,38 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
-
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
# If the agent does not answer, the model
|
40 |
-
#
|
|
|
41 |
|
42 |
model = HfApiModel(
|
43 |
-
max_tokens=2096,
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
|
|
|
|
|
|
53 |
with open("prompts.yaml", 'r') as stream:
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
|
|
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +75,4 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
GradioUI(agent).launch()
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
# Below is an example of a dummy tool. Impress us with your creativity!
|
11 |
@tool
|
12 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
13 |
+
"""A tool that does nothing yet.
|
14 |
+
|
15 |
Args:
|
16 |
arg1: the first argument
|
17 |
arg2: the second argument
|
18 |
"""
|
19 |
+
return "What magic will you build?"
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
24 |
+
|
25 |
Args:
|
26 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
27 |
"""
|
28 |
try:
|
29 |
+
# Create a timezone object
|
30 |
tz = pytz.timezone(timezone)
|
31 |
# Get current time in that timezone
|
32 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
34 |
except Exception as e:
|
35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
36 |
|
|
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
+
# If the agent does not answer, the model might be overloaded.
|
40 |
+
# Please use another model or the following Hugging Face endpoint that also contains Qwen2.5-Coder:
|
41 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
42 |
|
43 |
model = HfApiModel(
|
44 |
+
max_tokens=2096,
|
45 |
+
temperature=0.5,
|
46 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
47 |
+
custom_role_conversions=None,
|
48 |
)
|
49 |
|
50 |
+
# Load the image generation tool from the Hub
|
|
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
|
53 |
+
# Create an instance of DuckDuckGoSearchTool for web searches
|
54 |
+
duckduckgo_tool = DuckDuckGoSearchTool()
|
55 |
+
|
56 |
with open("prompts.yaml", 'r') as stream:
|
57 |
prompt_templates = yaml.safe_load(stream)
|
58 |
|
59 |
+
# Create our CodeAgent, adding all available tools
|
60 |
agent = CodeAgent(
|
61 |
model=model,
|
62 |
+
tools=[
|
63 |
+
final_answer, # Mandatory tool
|
64 |
+
my_custom_tool, # Dummy tool for customization
|
65 |
+
get_current_time_in_timezone, # Tool to get the current time for a given timezone
|
66 |
+
image_generation_tool, # Tool for image generation
|
67 |
+
duckduckgo_tool # Tool for web searches
|
68 |
+
],
|
69 |
max_steps=6,
|
70 |
verbosity_level=1,
|
71 |
grammar=None,
|
|
|
75 |
prompt_templates=prompt_templates
|
76 |
)
|
77 |
|
|
|
78 |
GradioUI(agent).launch()
|