Spaces:
Sleeping
Sleeping
Update tools/web_search.py
Browse files- tools/web_search.py +82 -13
tools/web_search.py
CHANGED
|
@@ -1,27 +1,96 @@
|
|
| 1 |
-
from typing import
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
|
| 4 |
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
name = "web_search"
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
output_type = "string"
|
| 10 |
|
| 11 |
-
def __init__(self, max_results=
|
|
|
|
| 12 |
super().__init__()
|
|
|
|
| 13 |
self.max_results = max_results
|
|
|
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
except ImportError as e:
|
|
|
|
| 17 |
raise ImportError(
|
| 18 |
-
"
|
| 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 List
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
+
|
| 4 |
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
| 6 |
+
"""
|
| 7 |
+
Performs a DuckDuckGo web search and returns formatted search results.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
name = "web_search"
|
| 11 |
+
|
| 12 |
+
description = (
|
| 13 |
+
"Searches the web using DuckDuckGo and returns the most relevant "
|
| 14 |
+
"search results. Use this whenever factual or current information "
|
| 15 |
+
"is required."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
inputs = {
|
| 19 |
+
"query": {
|
| 20 |
+
"type": "string",
|
| 21 |
+
"description": "Search query."
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
output_type = "string"
|
| 26 |
|
| 27 |
+
def __init__(self, max_results: int = 5):
|
| 28 |
+
|
| 29 |
super().__init__()
|
| 30 |
+
|
| 31 |
self.max_results = max_results
|
| 32 |
+
|
| 33 |
try:
|
| 34 |
+
|
| 35 |
+
from ddgs import DDGS
|
| 36 |
+
|
| 37 |
+
self.ddgs = DDGS()
|
| 38 |
+
|
| 39 |
except ImportError as e:
|
| 40 |
+
|
| 41 |
raise ImportError(
|
| 42 |
+
"Install ddgs using:\n\npip install ddgs"
|
| 43 |
) from e
|
|
|
|
| 44 |
|
| 45 |
def forward(self, query: str) -> str:
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
|
| 49 |
+
results = list(
|
| 50 |
+
self.ddgs.text(
|
| 51 |
+
query,
|
| 52 |
+
max_results=self.max_results
|
| 53 |
+
)
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
|
| 58 |
+
return f"Search failed.\nReason: {e}"
|
| 59 |
+
|
| 60 |
+
if not results:
|
| 61 |
+
|
| 62 |
+
return "No search results found."
|
| 63 |
+
|
| 64 |
+
formatted = []
|
| 65 |
+
|
| 66 |
+
seen_urls = set()
|
| 67 |
+
|
| 68 |
+
for i, result in enumerate(results, start=1):
|
| 69 |
+
|
| 70 |
+
title = result.get("title", "No title")
|
| 71 |
+
|
| 72 |
+
url = result.get("href", "")
|
| 73 |
+
|
| 74 |
+
snippet = result.get("body", "")
|
| 75 |
+
|
| 76 |
+
if url in seen_urls:
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
seen_urls.add(url)
|
| 80 |
+
|
| 81 |
+
formatted.append(
|
| 82 |
+
f"""
|
| 83 |
+
Result {i}
|
| 84 |
+
|
| 85 |
+
Title:
|
| 86 |
+
{title}
|
| 87 |
+
|
| 88 |
+
URL:
|
| 89 |
+
{url}
|
| 90 |
+
|
| 91 |
+
Snippet:
|
| 92 |
+
{snippet}
|
| 93 |
+
"""
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return "\n".join(formatted)
|