Spaces:
Sleeping
Sleeping
Update app.py
Browse filesUpdated search tool due to errors calling the DuckDuckGo api
app.py
CHANGED
|
@@ -20,22 +20,38 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 20 |
return "What magic will you build ?"
|
| 21 |
|
| 22 |
@tool
|
| 23 |
-
def search_duckduckgo_documents(query: str, max_results: int = 10) ->
|
| 24 |
"""
|
| 25 |
-
Search DuckDuckGo and retrieve
|
| 26 |
|
| 27 |
Args:
|
| 28 |
query (str): The search query
|
| 29 |
max_results (int): Maximum number of results to return (default: 10)
|
| 30 |
|
| 31 |
Returns:
|
| 32 |
-
|
| 33 |
"""
|
| 34 |
# Initialize the DuckDuckGo search tool
|
| 35 |
search_tool = DuckDuckGoSearchTool()
|
| 36 |
|
| 37 |
-
# Perform the search
|
| 38 |
-
results = search_tool(query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
return results
|
| 41 |
|
|
|
|
| 20 |
return "What magic will you build ?"
|
| 21 |
|
| 22 |
@tool
|
| 23 |
+
def search_duckduckgo_documents(query: str, max_results: int = 10) -> str:
|
| 24 |
"""
|
| 25 |
+
Search DuckDuckGo and retrieve top N search results.
|
| 26 |
|
| 27 |
Args:
|
| 28 |
query (str): The search query
|
| 29 |
max_results (int): Maximum number of results to return (default: 10)
|
| 30 |
|
| 31 |
Returns:
|
| 32 |
+
str: Formatted search results as a string (limited to max_results)
|
| 33 |
"""
|
| 34 |
# Initialize the DuckDuckGo search tool
|
| 35 |
search_tool = DuckDuckGoSearchTool()
|
| 36 |
|
| 37 |
+
# Perform the search (DuckDuckGoSearchTool doesn't accept max_results parameter)
|
| 38 |
+
results = search_tool(query)
|
| 39 |
+
|
| 40 |
+
# Manually limit results by parsing the string output
|
| 41 |
+
if isinstance(results, str):
|
| 42 |
+
# Split by lines and try to limit results
|
| 43 |
+
lines = results.split('\n')
|
| 44 |
+
print(len(lines))
|
| 45 |
+
result_count = 0
|
| 46 |
+
limited_lines = []
|
| 47 |
+
|
| 48 |
+
for line in lines:
|
| 49 |
+
if result_count >= max_results:
|
| 50 |
+
break
|
| 51 |
+
result_count += 1
|
| 52 |
+
limited_lines.append(line)
|
| 53 |
+
|
| 54 |
+
results = '\n'.join(limited_lines)
|
| 55 |
|
| 56 |
return results
|
| 57 |
|