Spaces:
Sleeping
Sleeping
Update tools/web_search.py
Browse files- tools/web_search.py +22 -22
tools/web_search.py
CHANGED
|
@@ -44,43 +44,43 @@ class DuckDuckGoSearchTool(Tool):
|
|
| 44 |
|
| 45 |
def forward(self, query: str) -> str:
|
| 46 |
|
| 47 |
-
|
| 48 |
query,
|
| 49 |
query + " wikipedia",
|
| 50 |
query + " official",
|
| 51 |
query.replace("-", " "),
|
| 52 |
]
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
q,
|
| 64 |
max_results=3
|
| 65 |
)
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
f"""
|
| 85 |
Title:
|
| 86 |
{title}
|
|
@@ -93,10 +93,10 @@ Snippet:
|
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
|
|
|
| 44 |
|
| 45 |
def forward(self, query: str) -> str:
|
| 46 |
|
| 47 |
+
queries = [
|
| 48 |
query,
|
| 49 |
query + " wikipedia",
|
| 50 |
query + " official",
|
| 51 |
query.replace("-", " "),
|
| 52 |
]
|
| 53 |
|
| 54 |
+
seen = set()
|
| 55 |
+
formatted = []
|
| 56 |
|
| 57 |
+
for q in queries:
|
| 58 |
|
| 59 |
+
try:
|
| 60 |
|
| 61 |
+
results = list(
|
| 62 |
+
self.ddgs.text(
|
| 63 |
q,
|
| 64 |
max_results=3
|
| 65 |
)
|
| 66 |
)
|
| 67 |
|
| 68 |
+
except Exception:
|
| 69 |
+
continue
|
| 70 |
|
| 71 |
+
for result in results:
|
| 72 |
|
| 73 |
+
url = result.get("href", "")
|
| 74 |
|
| 75 |
+
if url in seen:
|
| 76 |
+
continue
|
| 77 |
|
| 78 |
+
seen.add(url)
|
| 79 |
|
| 80 |
+
title = result.get("title", "No title")
|
| 81 |
+
snippet = result.get("body", "")
|
| 82 |
|
| 83 |
+
formatted.append(
|
| 84 |
f"""
|
| 85 |
Title:
|
| 86 |
{title}
|
|
|
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
| 96 |
+
if len(formatted) >= 5:
|
| 97 |
+
break
|
| 98 |
|
| 99 |
+
if not formatted:
|
| 100 |
+
return "No useful search results were found."
|
| 101 |
|
| 102 |
+
return "\n".join(formatted[:5])
|