Spaces:
Sleeping
Sleeping
from smolagents import Tool | |
import time | |
import random | |
from duckduckgo_search import DDGS | |
class DuckDuckGoSearchToolWH(Tool): | |
name = "web_search" | |
description = """Performs a DuckDuckGo web search based on your query (think a Google search) then returns the top search results.""" | |
inputs = {"query": {"type": "string", "description": "The search query to perform."}} | |
output_type = "string" | |
def __init__(self, max_results=10, **kwargs): | |
super().__init__() | |
self.max_results = max_results | |
self.USER_AGENTS = [ | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0", | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", | |
"Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36", | |
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1" | |
] | |
self.kwargs = kwargs | |
self.kwargs.pop('headers', None) | |
def forward(self, query: str) -> str: | |
headers = {"User-Agent": random.choice(self.USER_AGENTS)} | |
self.ddgs = DDGS(headers=headers, **self.kwargs) | |
time.sleep(2.0) | |
results = self.ddgs.text(query, max_results=self.max_results) | |
if not results: | |
raise Exception("No results found! Try a less restrictive/shorter query.") | |
postprocessed_results = [ | |
f"[{result['title']}]({result['href']})\n{result['body']}" | |
for result in results | |
] | |
return "## Search Results\n\n" + "\n\n".join(postprocessed_results) | |