Spaces:
Sleeping
Sleeping
Update tools/web_search.py
Browse files- tools/web_search.py +15 -18
tools/web_search.py
CHANGED
@@ -1,26 +1,26 @@
|
|
1 |
-
from typing import Any
|
2 |
from smolagents.tools import Tool
|
3 |
from ddgs import DDGS
|
4 |
|
|
|
5 |
class DuckDuckGoSearchTool(Tool):
|
6 |
name = "web_search"
|
7 |
-
description = "Performs a
|
8 |
-
inputs:
|
9 |
-
|
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,
|
@@ -30,27 +30,24 @@ class DuckDuckGoSearchTool(Tool):
|
|
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:
|
39 |
for it in items:
|
40 |
href = (it.get("href") or "").strip()
|
41 |
if not href:
|
42 |
-
continue
|
43 |
results.append(
|
44 |
{
|
45 |
"title": str(it.get("title", "")),
|
46 |
-
"url": href,
|
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 |
|
|
|
|
1 |
+
from typing import Any
|
2 |
from smolagents.tools import Tool
|
3 |
from ddgs import DDGS
|
4 |
|
5 |
+
|
6 |
class DuckDuckGoSearchTool(Tool):
|
7 |
name = "web_search"
|
8 |
+
description = "Performs a DuckDuckGo web search based on your query and returns the top results."
|
9 |
+
inputs: dict[str, dict[str, Any]] = {
|
10 |
+
"query": {"type": "string", "description": "The search query to perform."}
|
11 |
}
|
12 |
+
output_type = "string" # you can switch to "json" if your smolagents version supports it
|
13 |
|
14 |
def __init__(self, max_results: int = 10, **kwargs: Any) -> None:
|
15 |
super().__init__()
|
16 |
self.max_results = max_results
|
17 |
self.ddgs = DDGS(**kwargs)
|
18 |
|
19 |
+
def forward(self, query: str) -> list[dict[str, str]]:
|
20 |
q = (query or "").strip()
|
21 |
if not q:
|
22 |
raise ValueError("Query must be a non-empty string.")
|
23 |
+
|
|
|
24 |
items = list(
|
25 |
self.ddgs.text(
|
26 |
keywords=q,
|
|
|
30 |
max_results=self.max_results,
|
31 |
)
|
32 |
)
|
33 |
+
|
|
|
34 |
if not items:
|
35 |
raise RuntimeError("No results found. Try a broader/simpler query.")
|
36 |
+
|
37 |
+
results: list[dict[str, str]] = []
|
38 |
for it in items:
|
39 |
href = (it.get("href") or "").strip()
|
40 |
if not href:
|
41 |
+
continue
|
42 |
results.append(
|
43 |
{
|
44 |
"title": str(it.get("title", "")),
|
45 |
+
"url": href,
|
46 |
"snippet": str(it.get("body", "")),
|
47 |
}
|
48 |
)
|
49 |
+
|
|
|
50 |
if not results:
|
51 |
raise RuntimeError("Results were returned but none contained valid URLs.")
|
|
|
|
|
52 |
|
53 |
+
return results
|