Dkapsis's picture
organize project
9ac9d5e
raw
history blame
1.03 kB
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
from smolagents.tools import tool
# Tools
simple_web_search_tool = DuckDuckGoSearchTool()
visit_web_page_tool = VisitWebpageTool()
@tool
def web_search_tool(query: str) -> str:
"""
Given a question, search the web and return a summary answer.
Args:
query (str): The search query to look up.
Returns:
str: A relevant summary or result from DuckDuckGo.
"""
try:
url = "https://api.duckduckgo.com/"
params = {"q": query, "format": "json", "no_html": 1}
response = requests.get(url, params=params)
data = response.json()
if abstract := data.get("AbstractText"):
return abstract
elif related := data.get("RelatedTopics"):
return related[0]["Text"] if related else "No result found."
else:
return "No relevant information found via DuckDuckGo."
except Exception as e:
raise RuntimeError(f"DuckDuckGo search failed: {str(e)}")