Spaces:
Sleeping
Sleeping
Commit ·
79a8ee7
1
Parent(s): 0e5f5ea
Fixed DuckDuckGoSeacrhTool issue
Browse files- tools/web_search.py +11 -3
tools/web_search.py
CHANGED
|
@@ -18,7 +18,7 @@ class DuckDuckGoSearchTool(Tool):
|
|
| 18 |
|
| 19 |
def __init__(self, max_results=10, **kwargs):
|
| 20 |
super().__init__()
|
| 21 |
-
self.
|
| 22 |
try:
|
| 23 |
from duckduckgo_search import DDGS
|
| 24 |
except ImportError as e:
|
|
@@ -27,8 +27,16 @@ class DuckDuckGoSearchTool(Tool):
|
|
| 27 |
) from e
|
| 28 |
self.ddgs = DDGS(**kwargs)
|
| 29 |
|
| 30 |
-
def forward(self, query: str) -> str:
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
if len(results) == 0:
|
| 33 |
raise Exception(
|
| 34 |
"No results found! Try a less restrictive/shorter query.")
|
|
|
|
| 18 |
|
| 19 |
def __init__(self, max_results=10, **kwargs):
|
| 20 |
super().__init__()
|
| 21 |
+
self.default_max_results = max_results
|
| 22 |
try:
|
| 23 |
from duckduckgo_search import DDGS
|
| 24 |
except ImportError as e:
|
|
|
|
| 27 |
) from e
|
| 28 |
self.ddgs = DDGS(**kwargs)
|
| 29 |
|
| 30 |
+
def forward(self, query: str, max_results: int = None) -> str:
|
| 31 |
+
"""Performs a web search
|
| 32 |
+
Args:
|
| 33 |
+
query: The search query to perform
|
| 34 |
+
max_results: Maximum number of results to return. If not specified, uses the value from __init__
|
| 35 |
+
Returns:
|
| 36 |
+
String containing formatted search results
|
| 37 |
+
"""
|
| 38 |
+
results_limit = max_results if max_results is not None else self.default_max_results
|
| 39 |
+
results = self.ddgs.text(query, max_results=results_limit)
|
| 40 |
if len(results) == 0:
|
| 41 |
raise Exception(
|
| 42 |
"No results found! Try a less restrictive/shorter query.")
|