Spaces:
Runtime error
Runtime error
agent custom search tool
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -33,6 +34,21 @@ 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 |
|
|
@@ -55,7 +71,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
+
from duckduckgo_search import DDGS
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
+
@tool
|
| 38 |
+
def search_web(query: str) -> str:
|
| 39 |
+
"""A tool that searches the web for information.
|
| 40 |
+
Args:
|
| 41 |
+
query: A string representing the search query.
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
with DDGS() as ddgs:
|
| 45 |
+
results = list(ddgs.text(query, max_results=3))
|
| 46 |
+
if not results:
|
| 47 |
+
return "No results found."
|
| 48 |
+
return "\n\n".join([f"{i+1}. {r['title']}\n{r['body']}" for i, r in enumerate(results)])
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error searching the web for query '{query}': {str(e)}"
|
| 51 |
+
|
| 52 |
|
| 53 |
final_answer = FinalAnswerTool()
|
| 54 |
|
|
|
|
| 71 |
|
| 72 |
agent = CodeAgent(
|
| 73 |
model=model,
|
| 74 |
+
tools=[final_answer, search_web], ## add your tools here (don't remove final answer)
|
| 75 |
max_steps=6,
|
| 76 |
verbosity_level=1,
|
| 77 |
grammar=None,
|