Spaces:
Sleeping
Sleeping
Using newer version of ddgs which returns a generator and return JSON
Browse files- tools/web_search.py +44 -15
tools/web_search.py
CHANGED
|
@@ -1,27 +1,56 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
import
|
| 4 |
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
| 6 |
name = "web_search"
|
| 7 |
-
description = "Performs a duckduckgo web search based on your query
|
| 8 |
-
inputs
|
|
|
|
|
|
|
| 9 |
output_type = "string"
|
| 10 |
|
| 11 |
-
def __init__(self, max_results=10, **kwargs):
|
| 12 |
super().__init__()
|
| 13 |
self.max_results = max_results
|
| 14 |
-
try:
|
| 15 |
-
from duckduckgo_search import DDGS
|
| 16 |
-
except ImportError as e:
|
| 17 |
-
raise ImportError(
|
| 18 |
-
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 19 |
-
) from e
|
| 20 |
self.ddgs = DDGS(**kwargs)
|
| 21 |
|
| 22 |
def forward(self, query: str) -> str:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
+
from ddgs import DDGS
|
| 4 |
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
| 6 |
name = "web_search"
|
| 7 |
+
description = "Performs a duckduckgo web search based on your query and returns the top results."
|
| 8 |
+
inputs: Dict[str, Dict[str, Any]] = {
|
| 9 |
+
'query': {'type': 'string', 'description': 'The search query to perform.'}
|
| 10 |
+
}
|
| 11 |
output_type = "string"
|
| 12 |
|
| 13 |
+
def __init__(self, max_results: int = 10, **kwargs: Any) -> None:
|
| 14 |
super().__init__()
|
| 15 |
self.max_results = max_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
self.ddgs = DDGS(**kwargs)
|
| 17 |
|
| 18 |
def forward(self, query: str) -> str:
|
| 19 |
+
q = (query or "").strip()
|
| 20 |
+
if not q:
|
| 21 |
+
raise ValueError("Query must be a non-empty string.")
|
| 22 |
+
|
| 23 |
+
# 1) ddgs.text: correct args + materialize generator
|
| 24 |
+
items = list(
|
| 25 |
+
self.ddgs.text(
|
| 26 |
+
keywords=q,
|
| 27 |
+
region="wt-wt",
|
| 28 |
+
safesearch="moderate",
|
| 29 |
+
timelimit=None,
|
| 30 |
+
max_results=self.max_results,
|
| 31 |
+
)
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# 3) Better emptiness check + error type
|
| 35 |
+
if not items:
|
| 36 |
+
raise RuntimeError("No results found. Try a broader/simpler query.")
|
| 37 |
+
|
| 38 |
+
results: List[Dict[str, str]] = []
|
| 39 |
+
for it in items:
|
| 40 |
+
href = (it.get("href") or "").strip()
|
| 41 |
+
if not href:
|
| 42 |
+
continue # skip malformed entries without a URL
|
| 43 |
+
results.append(
|
| 44 |
+
{
|
| 45 |
+
"title": str(it.get("title", "")),
|
| 46 |
+
"url": href, # normalized key name
|
| 47 |
+
"snippet": str(it.get("body", "")),
|
| 48 |
+
}
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if not results:
|
| 53 |
+
raise RuntimeError("Results were returned but none contained valid URLs.")
|
| 54 |
+
|
| 55 |
+
return results
|
| 56 |
+
|