Sborole-Final-Assignment / tools /WebSearchTool.py
Sborole's picture
Update tools/WebSearchTool.py
b9431c1 verified
raw
history blame
853 Bytes
from smolagents import Tool
from duckduckgo_search import DDGS
class DuckDuckGoSearchTool(Tool):
name = "duckduckgo_search"
description = "Fetch a short answer from Wikipedia; fallback to DuckDuckGo if needed."
inputs = {
"query": {"type": "string", "description": "Search query"}
}
output_type = "string"
def forward(self, query: str) -> str:
try:
with DDGS() as ddgs:
results = ddgs.text(query, max_results=3) # returns a generator
results = list(results)
if not results:
return "No results found."
# Return the first result title as concise answer
return results[0].get("title", "No results found.")
except Exception as e:
return f"Error during search: {e}"