Spaces:
Sleeping
Sleeping
Update tools/web_search.py
Browse files- tools/web_search.py +35 -35
tools/web_search.py
CHANGED
|
@@ -16,41 +16,41 @@ class DuckDuckGoSearchTool(Tool):
|
|
| 16 |
self.ddgs = DDGS(**kwargs)
|
| 17 |
|
| 18 |
def forward(self, query: str) -> str:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 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 |
|