ZBro7 commited on
Commit
482c62e
·
verified ·
1 Parent(s): b1dd335

Update search_tool.py

Browse files
Files changed (1) hide show
  1. 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
- def search_web(query):
7
- params = {
8
- "q": query,
9
- "format": "json"
10
- }
11
- response = requests.get(SEARXNG_URL, params=params)
12
- results = response.json().get("results", [])
13
-
14
- summarized = ""
15
- for r in results[:3]:
16
- summarized += r["title"] + "\n" + r["content"] + "\n\n"
17
-
18
- return summarized
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)}"