Spaces:
Sleeping
Sleeping
AyrtonSur
commited on
Commit
·
0faa702
1
Parent(s):
de888d3
feat: creating tool for websearching using duckduckgo_search
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -33,6 +34,26 @@ 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 +76,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,DuckDuckGoSearchTool, 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_for_something(query: str) -> str:
|
| 39 |
+
"""A tool that fetches the 5 first websites that appear in a query, with their title, url and snippets.
|
| 40 |
+
Args:
|
| 41 |
+
query: String that represent what is being searched
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
ddg = DDGS()
|
| 45 |
+
results = ddg.text(query, max_results=5)
|
| 46 |
+
formatted_results = []
|
| 47 |
+
for result in results:
|
| 48 |
+
title = result.get("title", "No title available")
|
| 49 |
+
url = result.get("href", "No URL available")
|
| 50 |
+
snippet = result.get("body", "No snippet available")
|
| 51 |
+
formatted_results.append(f"Title: {title}\nURL: {url}\nSnippet: {snippet}\n")
|
| 52 |
+
|
| 53 |
+
return "\n".join(formatted_results)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"Error searching for {query} in the Web: {str(e)}"
|
| 56 |
+
|
| 57 |
|
| 58 |
final_answer = FinalAnswerTool()
|
| 59 |
|
|
|
|
| 76 |
|
| 77 |
agent = CodeAgent(
|
| 78 |
model=model,
|
| 79 |
+
tools=[final_answer, get_current_time_in_timezone, search_for_something], ## add your tools here (don't remove final answer)
|
| 80 |
max_steps=6,
|
| 81 |
verbosity_level=1,
|
| 82 |
grammar=None,
|