Spaces:
Paused
Paused
Update search_tool.py
Browse files- search_tool.py +40 -14
search_tool.py
CHANGED
|
@@ -3,17 +3,43 @@ import os
|
|
| 3 |
|
| 4 |
SEARXNG_URL = os.getenv("SEARXNG_URL")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
SEARXNG_URL = os.getenv("SEARXNG_URL")
|
| 5 |
|
| 6 |
+
|
| 7 |
+
def search_web(query, max_results=5):
|
| 8 |
+
|
| 9 |
+
if not SEARXNG_URL:
|
| 10 |
+
return "Search service unavailable."
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
response = requests.get(
|
| 14 |
+
SEARXNG_URL,
|
| 15 |
+
params={"q": query, "format": "json"},
|
| 16 |
+
timeout=15
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
data = response.json()
|
| 20 |
+
results = data.get("results", [])
|
| 21 |
+
|
| 22 |
+
if not results:
|
| 23 |
+
return "No search results found."
|
| 24 |
+
|
| 25 |
+
compiled = ""
|
| 26 |
+
|
| 27 |
+
for r in results[:max_results]:
|
| 28 |
+
title = r.get("title", "")
|
| 29 |
+
content = r.get("content", "")
|
| 30 |
+
url = r.get("url", "")
|
| 31 |
+
|
| 32 |
+
if not title or not content:
|
| 33 |
+
continue
|
| 34 |
+
|
| 35 |
+
compiled += f"""
|
| 36 |
+
Title: {title}
|
| 37 |
+
Source: {url}
|
| 38 |
+
Snippet: {content}
|
| 39 |
+
---
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
return compiled.strip()
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Search failed: {str(e)}"
|