Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -72,14 +72,22 @@ def wiki_search(query: str) -> str:
|
|
| 72 |
@tool
|
| 73 |
def web_search(query: str) -> str:
|
| 74 |
"""
|
| 75 |
-
Search the
|
| 76 |
(title, URL, and snippet).
|
| 77 |
"""
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
formatted = []
|
| 80 |
for r in results:
|
| 81 |
-
title = r.get("title", "").strip()
|
| 82 |
-
url = r.get("href", "").strip()
|
| 83 |
body = r.get("body", "").strip()
|
| 84 |
formatted.append(f"• **{title}**\n {url}\n _{body}_")
|
| 85 |
return "\n\n".join(formatted)
|
|
|
|
| 72 |
@tool
|
| 73 |
def web_search(query: str) -> str:
|
| 74 |
"""
|
| 75 |
+
Search DuckDuckGo via the DDGS class and return the top 3 results
|
| 76 |
(title, URL, and snippet).
|
| 77 |
"""
|
| 78 |
+
try:
|
| 79 |
+
with DDGS() as ddgs:
|
| 80 |
+
results = ddgs.text(query, max_results=3)
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"DuckDuckGo search error: {e}"
|
| 83 |
+
|
| 84 |
+
if not results:
|
| 85 |
+
return "No results from DuckDuckGo."
|
| 86 |
+
|
| 87 |
formatted = []
|
| 88 |
for r in results:
|
| 89 |
+
title = r.get("title", "<no title>").strip()
|
| 90 |
+
url = r.get("href", "<no URL>").strip()
|
| 91 |
body = r.get("body", "").strip()
|
| 92 |
formatted.append(f"• **{title}**\n {url}\n _{body}_")
|
| 93 |
return "\n\n".join(formatted)
|