Spaces:
Sleeping
Sleeping
add web_search tool
Browse files- app.py +25 -16
- tools/web_search.py +16 -4
app.py
CHANGED
|
@@ -1,23 +1,28 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
-
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from
|
| 7 |
|
| 8 |
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 my_custom_tool(
|
| 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.
|
|
@@ -35,35 +40,39 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 35 |
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
|
|
|
| 38 |
|
| 39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id=
|
| 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",
|
| 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 |
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
|
|
|
| 1 |
import datetime
|
| 2 |
+
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 6 |
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
+
from tools.final_answer import FinalAnswerTool
|
| 9 |
+
from tools.web_search import DuckDuckGoSearchTool
|
| 10 |
+
|
| 11 |
|
| 12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 13 |
@tool
|
| 14 |
+
def my_custom_tool(
|
| 15 |
+
arg1: str, arg2: int
|
| 16 |
+
) -> str: # it's import to specify the return type
|
| 17 |
+
# Keep this format for the description / args / args description but feel free to modify the tool
|
| 18 |
+
"""A tool that does nothing yet
|
| 19 |
Args:
|
| 20 |
arg1: the first argument
|
| 21 |
arg2: the second argument
|
| 22 |
"""
|
| 23 |
return "What magic will you build ?"
|
| 24 |
|
| 25 |
+
|
| 26 |
@tool
|
| 27 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 28 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
final_answer = FinalAnswerTool()
|
| 43 |
+
web_search = DuckDuckGoSearchTool()
|
| 44 |
|
| 45 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 46 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 47 |
|
| 48 |
model = HfApiModel(
|
| 49 |
+
max_tokens=2096,
|
| 50 |
+
temperature=0.5,
|
| 51 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # it is possible that this model may be overloaded
|
| 52 |
+
custom_role_conversions=None,
|
| 53 |
)
|
| 54 |
|
| 55 |
|
| 56 |
# Import tool from Hub
|
| 57 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 58 |
|
| 59 |
+
with open("prompts.yaml", "r") as stream:
|
| 60 |
prompt_templates = yaml.safe_load(stream)
|
| 61 |
+
|
| 62 |
agent = CodeAgent(
|
| 63 |
model=model,
|
| 64 |
+
tools=[
|
| 65 |
+
final_answer,
|
| 66 |
+
web_search,
|
| 67 |
+
],
|
| 68 |
max_steps=6,
|
| 69 |
verbosity_level=1,
|
| 70 |
grammar=None,
|
| 71 |
planning_interval=None,
|
| 72 |
name=None,
|
| 73 |
description=None,
|
| 74 |
+
prompt_templates=prompt_templates,
|
| 75 |
)
|
| 76 |
|
| 77 |
|
| 78 |
+
GradioUI(agent).launch()
|
tools/web_search.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
-
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
|
| 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 +24,16 @@ 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from smolagents.tools import Tool
|
| 2 |
+
|
| 3 |
|
| 4 |
class DuckDuckGoSearchTool(Tool):
|
| 5 |
name = "web_search"
|
| 6 |
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 7 |
+
inputs = {
|
| 8 |
+
"query": {"type": "string", "description": "The search query to perform."}
|
| 9 |
+
}
|
| 10 |
output_type = "string"
|
| 11 |
|
| 12 |
def __init__(self, max_results=10, **kwargs):
|
|
|
|
| 24 |
results = self.ddgs.text(query, max_results=self.max_results)
|
| 25 |
if len(results) == 0:
|
| 26 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 27 |
+
postprocessed_results = [
|
| 28 |
+
f"[{result['title']}]({result['href']})\n{result['body']}"
|
| 29 |
+
for result in results
|
| 30 |
+
]
|
| 31 |
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
ddgs_tool = DuckDuckGoSearchTool()
|
| 36 |
+
res = ddgs_tool.forward(
|
| 37 |
+
query="What is the best Ramen restaurant in Stuttgart, Germany?"
|
| 38 |
+
)
|
| 39 |
+
breakpoint()
|