rnrahate007 commited on
Commit
a8fc2f1
·
verified ·
1 Parent(s): 2f893db

Update tools/web_search.py

Browse files
Files changed (1) hide show
  1. tools/web_search.py +22 -22
tools/web_search.py CHANGED
@@ -44,43 +44,43 @@ class DuckDuckGoSearchTool(Tool):
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}
@@ -93,10 +93,10 @@ Snippet:
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])
 
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}
 
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])