Spaces:
Sleeping
Sleeping
Add the search tool
Browse files
app.py
CHANGED
@@ -3,20 +3,44 @@ 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 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def
|
13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""
|
15 |
Args:
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -55,7 +79,10 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[
|
|
|
|
|
|
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
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 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
+
def search_tool(query:str, num_results:int = 3)-> str: #it's import to specify the return type
|
13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
+
"""searches the web using DuckDuckGo and return relevant results
|
15 |
Args:
|
16 |
+
query: The search query string
|
17 |
+
num_results: Number of results to return
|
18 |
"""
|
19 |
+
|
20 |
+
try:
|
21 |
+
#initialize
|
22 |
+
search = DuckDuckGoSearchTool()
|
23 |
+
|
24 |
+
#perform the search
|
25 |
+
results = search.search(query,num_results = num_results)
|
26 |
+
|
27 |
+
#format the results
|
28 |
+
formatted_results = []
|
29 |
+
for i, result in enumerate(results,1):
|
30 |
+
formatted_results.append(
|
31 |
+
f"{i}. Title: {result.title}\n"
|
32 |
+
f" snippet: {result.snippet}\n"
|
33 |
+
f" URL:{result.link}\n"
|
34 |
+
)
|
35 |
+
|
36 |
+
#combine results into a single string
|
37 |
+
if formatted_results:
|
38 |
+
return "search Results:\n\n" + "\n".join(formatted_results)
|
39 |
+
else:
|
40 |
+
return "no results found for the given query."
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
return f"error performing search: {str(e)}"
|
44 |
|
45 |
@tool
|
46 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
79 |
|
80 |
agent = CodeAgent(
|
81 |
model=model,
|
82 |
+
tools=[
|
83 |
+
final_answer,
|
84 |
+
search_tool,
|
85 |
+
get_current_time_in_timezone],
|
86 |
max_steps=6,
|
87 |
verbosity_level=1,
|
88 |
grammar=None,
|