Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,26 @@ from tools.web_search import DuckDuckGoSearchTool
|
|
| 11 |
@tool
|
| 12 |
def DuckDuckGoSearchTool (web_search: str) -> str:
|
| 13 |
""""A tool Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."""
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@tool
|
| 17 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 11 |
@tool
|
| 12 |
def DuckDuckGoSearchTool (web_search: str) -> str:
|
| 13 |
""""A tool Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."""
|
| 14 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 15 |
+
output_type = "string"
|
| 16 |
|
| 17 |
+
def __init__(self, max_results=10, **kwargs):
|
| 18 |
+
super().__init__()
|
| 19 |
+
self.max_results = max_results
|
| 20 |
+
try:
|
| 21 |
+
from duckduckgo_search import DDGS
|
| 22 |
+
except ImportError as e:
|
| 23 |
+
raise ImportError(
|
| 24 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 25 |
+
) from e
|
| 26 |
+
self.ddgs = DDGS(**kwargs)
|
| 27 |
+
|
| 28 |
+
def forward(self, query: str) -> str:
|
| 29 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 30 |
+
if len(results) == 0:
|
| 31 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 32 |
+
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
| 33 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
| 34 |
|
| 35 |
@tool
|
| 36 |
def get_current_time_in_timezone(timezone: str) -> str:
|