rnrahate007 commited on
Commit
ebb36cd
·
verified ·
1 Parent(s): a195930

Update tools/web_search.py

Browse files
Files changed (1) hide show
  1. tools/web_search.py +28 -22
tools/web_search.py CHANGED
@@ -44,44 +44,44 @@ class DuckDuckGoSearchTool(Tool):
44
 
45
  def forward(self, query: str) -> str:
46
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  try:
48
 
49
  results = list(
50
  self.ddgs.text(
51
- query,
52
- max_results=self.max_results
53
  )
54
  )
55
 
56
- except Exception as e:
57
-
58
- return f"Search failed.\nReason: {e}"
59
 
60
- if not results:
61
 
62
- return "No search results found."
63
-
64
- formatted = []
65
 
66
- seen_urls = set()
 
67
 
68
- for i, result in enumerate(results, start=1):
69
 
70
  title = result.get("title", "No title")
71
-
72
- url = result.get("href", "")
73
-
74
  snippet = result.get("body", "")
75
 
76
- if url in seen_urls:
77
- continue
78
-
79
- seen_urls.add(url)
80
-
81
  formatted.append(
82
  f"""
83
- Result {i}
84
-
85
  Title:
86
  {title}
87
 
@@ -93,4 +93,10 @@ Snippet:
93
  """
94
  )
95
 
96
- return "\n".join(formatted)
 
 
 
 
 
 
 
44
 
45
  def forward(self, query: str) -> str:
46
 
47
+ queries = [
48
+ query,
49
+ query + " wikipedia",
50
+ query + " official",
51
+ query.replace("-", " "),
52
+ ]
53
+
54
+ seen = set()
55
+ formatted = []
56
+
57
+ for q in queries:
58
+
59
  try:
60
 
61
  results = list(
62
  self.ddgs.text(
63
+ q,
64
+ max_results=3
65
  )
66
  )
67
 
68
+ except Exception:
69
+ continue
 
70
 
71
+ for result in results:
72
 
73
+ url = result.get("href", "")
 
 
74
 
75
+ if url in seen:
76
+ continue
77
 
78
+ seen.add(url)
79
 
80
  title = result.get("title", "No title")
 
 
 
81
  snippet = result.get("body", "")
82
 
 
 
 
 
 
83
  formatted.append(
84
  f"""
 
 
85
  Title:
86
  {title}
87
 
 
93
  """
94
  )
95
 
96
+ if len(formatted) >= 5:
97
+ break
98
+
99
+ if not formatted:
100
+ return "No useful search results were found."
101
+
102
+ return "\n".join(formatted[:5])