Commit
·
bcbe917
1
Parent(s):
08d8184
Format code, add .gitignore, and add re import to visit_webpage
Browse files- .gitignore +2 -0
- app.py +26 -11
- tools/final_answer.py +4 -1
- tools/visit_webpage.py +5 -1
- tools/web_search.py +8 -2
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.gradio
|
app.py
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -8,17 +14,21 @@ from tools.visit_webpage import VisitWebpageTool
|
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
|
|
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
@tool
|
| 13 |
-
def my_cutom_tool(
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
Args:
|
| 17 |
arg1: the first argument
|
| 18 |
arg2: the second argument
|
| 19 |
"""
|
| 20 |
return "What magic will you build ?"
|
| 21 |
|
|
|
|
| 22 |
@tool
|
| 23 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 24 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -35,11 +45,10 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
|
| 38 |
-
|
| 39 |
model = HfApiModel(
|
| 40 |
max_tokens=2096,
|
| 41 |
temperature=0.5,
|
| 42 |
-
model_id=
|
| 43 |
custom_role_conversions=None,
|
| 44 |
)
|
| 45 |
|
|
@@ -50,19 +59,25 @@ duckduckgo_search = DuckDuckGoSearchTool()
|
|
| 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",
|
| 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,
|
| 62 |
planning_interval=None,
|
| 63 |
name=None,
|
| 64 |
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import (
|
| 2 |
+
CodeAgent,
|
| 3 |
+
DuckDuckGoSearchTool,
|
| 4 |
+
HfApiModel,
|
| 5 |
+
load_tool,
|
| 6 |
+
tool,
|
| 7 |
+
)
|
| 8 |
import datetime
|
| 9 |
import requests
|
| 10 |
import pytz
|
|
|
|
| 14 |
|
| 15 |
from Gradio_UI import GradioUI
|
| 16 |
|
| 17 |
+
|
| 18 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 19 |
@tool
|
| 20 |
+
def my_cutom_tool(
|
| 21 |
+
arg1: str, arg2: int
|
| 22 |
+
) -> str: # it's import to specify the return type
|
| 23 |
+
# Keep this format for the description / args / args description but feel free to modify the tool
|
| 24 |
+
"""A tool that does nothing yet
|
| 25 |
Args:
|
| 26 |
arg1: the first argument
|
| 27 |
arg2: the second argument
|
| 28 |
"""
|
| 29 |
return "What magic will you build ?"
|
| 30 |
|
| 31 |
+
|
| 32 |
@tool
|
| 33 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 34 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 45 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 46 |
|
| 47 |
|
|
|
|
| 48 |
model = HfApiModel(
|
| 49 |
max_tokens=2096,
|
| 50 |
temperature=0.5,
|
| 51 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 52 |
custom_role_conversions=None,
|
| 53 |
)
|
| 54 |
|
|
|
|
| 59 |
# Import tool from Hub
|
| 60 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 61 |
|
| 62 |
+
with open("prompts.yaml", "r") as stream:
|
| 63 |
prompt_templates = yaml.safe_load(stream)
|
| 64 |
+
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
+
tools=[
|
| 68 |
+
final_answer,
|
| 69 |
+
get_current_time_in_timezone,
|
| 70 |
+
image_generation_tool,
|
| 71 |
+
visit_webpage,
|
| 72 |
+
duckduckgo_search,
|
| 73 |
+
],
|
| 74 |
max_steps=6,
|
| 75 |
verbosity_level=1,
|
| 76 |
grammar=None,
|
| 77 |
planning_interval=None,
|
| 78 |
name=None,
|
| 79 |
description=None,
|
| 80 |
+
prompt_templates=prompt_templates,
|
| 81 |
)
|
| 82 |
|
| 83 |
+
GradioUI(agent).launch()
|
tools/final_answer.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
|
|
|
|
| 4 |
class FinalAnswerTool(Tool):
|
| 5 |
name = "final_answer"
|
| 6 |
description = "Provides a final answer to the given problem."
|
| 7 |
-
inputs = {
|
|
|
|
|
|
|
| 8 |
output_type = "any"
|
| 9 |
|
| 10 |
def forward(self, answer: Any) -> Any:
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
|
| 4 |
+
|
| 5 |
class FinalAnswerTool(Tool):
|
| 6 |
name = "final_answer"
|
| 7 |
description = "Provides a final answer to the given problem."
|
| 8 |
+
inputs = {
|
| 9 |
+
"answer": {"type": "any", "description": "The final answer to the problem"}
|
| 10 |
+
}
|
| 11 |
output_type = "any"
|
| 12 |
|
| 13 |
def forward(self, answer: Any) -> Any:
|
tools/visit_webpage.py
CHANGED
|
@@ -3,11 +3,15 @@ from smolagents.tools import Tool
|
|
| 3 |
import requests
|
| 4 |
import markdownify
|
| 5 |
import smolagents
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class VisitWebpageTool(Tool):
|
| 8 |
name = "visit_webpage"
|
| 9 |
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
| 10 |
-
inputs = {
|
|
|
|
|
|
|
| 11 |
output_type = "string"
|
| 12 |
|
| 13 |
def forward(self, url: str) -> str:
|
|
|
|
| 3 |
import requests
|
| 4 |
import markdownify
|
| 5 |
import smolagents
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
|
| 9 |
class VisitWebpageTool(Tool):
|
| 10 |
name = "visit_webpage"
|
| 11 |
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
| 12 |
+
inputs = {
|
| 13 |
+
"url": {"type": "string", "description": "The url of the webpage to visit."}
|
| 14 |
+
}
|
| 15 |
output_type = "string"
|
| 16 |
|
| 17 |
def forward(self, url: str) -> str:
|
tools/web_search.py
CHANGED
|
@@ -2,10 +2,13 @@ from typing import Any, Optional
|
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
import duckduckgo_search
|
| 4 |
|
|
|
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
| 6 |
name = "web_search"
|
| 7 |
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 8 |
-
inputs = {
|
|
|
|
|
|
|
| 9 |
output_type = "string"
|
| 10 |
|
| 11 |
def __init__(self, max_results=10, **kwargs):
|
|
@@ -23,5 +26,8 @@ class DuckDuckGoSearchTool(Tool):
|
|
| 23 |
results = self.ddgs.text(query, max_results=self.max_results)
|
| 24 |
if len(results) == 0:
|
| 25 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 26 |
-
postprocessed_results = [
|
|
|
|
|
|
|
|
|
|
| 27 |
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
|
|
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
import duckduckgo_search
|
| 4 |
|
| 5 |
+
|
| 6 |
class DuckDuckGoSearchTool(Tool):
|
| 7 |
name = "web_search"
|
| 8 |
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 9 |
+
inputs = {
|
| 10 |
+
"query": {"type": "string", "description": "The search query to perform."}
|
| 11 |
+
}
|
| 12 |
output_type = "string"
|
| 13 |
|
| 14 |
def __init__(self, max_results=10, **kwargs):
|
|
|
|
| 26 |
results = self.ddgs.text(query, max_results=self.max_results)
|
| 27 |
if len(results) == 0:
|
| 28 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 29 |
+
postprocessed_results = [
|
| 30 |
+
f"[{result['title']}]({result['href']})\n{result['body']}"
|
| 31 |
+
for result in results
|
| 32 |
+
]
|
| 33 |
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|