BetsyFromR commited on
Commit
ce5a2b1
·
verified ·
1 Parent(s): 1162870

Update tools/web_search.py

Browse files
Files changed (1) hide show
  1. tools/web_search.py +13 -6
tools/web_search.py CHANGED
@@ -1,6 +1,8 @@
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
- import duckduckgo_search
 
 
4
 
5
  class DuckDuckGoSearchTool(Tool):
6
  name = "web_search"
@@ -12,7 +14,8 @@ class DuckDuckGoSearchTool(Tool):
12
  super().__init__()
13
  self.max_results = max_results
14
  try:
15
- from duckduckgo_search import DDGS
 
16
  except ImportError as e:
17
  raise ImportError(
18
  "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
@@ -20,8 +23,12 @@ class DuckDuckGoSearchTool(Tool):
20
  self.ddgs = DDGS(**kwargs)
21
 
22
  def forward(self, query: str) -> str:
23
- results = self.ddgs.text(query, max_results=self.max_results)
24
- if len(results) == 0:
25
  raise Exception("No results found! Try a less restrictive/shorter query.")
26
- postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
27
- return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
 
 
 
 
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
+ # import duckduckgo_search
4
+ import ddgs
5
+
6
 
7
  class DuckDuckGoSearchTool(Tool):
8
  name = "web_search"
 
14
  super().__init__()
15
  self.max_results = max_results
16
  try:
17
+ # from duckduckgo_search import DDGS
18
+ from ddgs import DDGS
19
  except ImportError as e:
20
  raise ImportError(
21
  "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
 
23
  self.ddgs = DDGS(**kwargs)
24
 
25
  def forward(self, query: str) -> str:
26
+ results = list(self.ddgs.text(query, max_results=self.max_results))
27
+ if not results:
28
  raise Exception("No results found! Try a less restrictive/shorter query.")
29
+
30
+ post = [
31
+ f"[{r.get('title')}]({r.get('href')})\n{r.get('body')}"
32
+ for r in results
33
+ ]
34
+ return "## Search Results\n\n" + "\n\n".join(post)