BetsyFromR commited on
Commit
55ce585
·
verified ·
1 Parent(s): db6830a

Update tools/web_search.py

Browse files
Files changed (1) hide show
  1. tools/web_search.py +26 -20
tools/web_search.py CHANGED
@@ -2,15 +2,17 @@ from typing import Any, Optional
2
  from smolagents.tools import Tool
3
  import duckduckgo_search
4
 
 
5
  class DuckDuckGoSearchTool(Tool):
6
  name = "web_search"
7
  description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
8
  inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
9
  output_type = "string"
10
 
11
- def __init__(self, max_results=10, **kwargs):
12
  super().__init__()
13
  self.max_results = max_results
 
14
  try:
15
  from duckduckgo_search import DDGS
16
  except ImportError as e:
@@ -20,25 +22,29 @@ 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
- # results = list(self.ddgs.text(query, max_results=self.max_results))
26
- # if not results:
27
- # raise Exception("No results found! Try a less restrictive/shorter query.")
28
- # postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
29
- try:
30
- results = list(self.ddgs.text(query, max_results=self.max_results))
31
- except Exception as e:
 
 
 
 
 
 
 
 
32
  return (
33
  "Web search failed. This can happen if outbound network access is blocked or the "
34
- f"DuckDuckGo API is rate-limiting requests. Error: {e}"
35
  )
36
- if not results:
37
- return (
38
- "No results found. This may indicate network restrictions or a temporary "
39
- "DuckDuckGo issue. Try a shorter query or retry later."
40
- )
41
- postprocessed_results = [
42
- f"[{result['title']}]({result['href']})\n{result['body']}" for result in results
43
- ]
44
- return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
 
2
  from smolagents.tools import Tool
3
  import duckduckgo_search
4
 
5
+
6
  class DuckDuckGoSearchTool(Tool):
7
  name = "web_search"
8
  description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
9
  inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
10
  output_type = "string"
11
 
12
+ def __init__(self, max_results=10, backend="auto", **kwargs):
13
  super().__init__()
14
  self.max_results = max_results
15
+ self.backend = backend
16
  try:
17
  from duckduckgo_search import DDGS
18
  except ImportError as e:
 
22
  self.ddgs = DDGS(**kwargs)
23
 
24
  def forward(self, query: str) -> str:
25
+ backends_to_try = [self.backend] + [b for b in ("auto", "lite", "html", "api") if b != self.backend]
26
+ last_error = None
27
+ for backend in backends_to_try:
28
+ try:
29
+ results = list(
30
+ self.ddgs.text(query, max_results=self.max_results, backend=backend)
31
+ )
32
+ except Exception as e:
33
+ last_error = e
34
+ continue
35
+ if results:
36
+ postprocessed_results = [
37
+ f"[{result['title']}]({result['href']})\n{result['body']}"
38
+ for result in results
39
+ ]
40
+ return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
41
+ if last_error is not None:
42
  return (
43
  "Web search failed. This can happen if outbound network access is blocked or the "
44
+ f"DuckDuckGo API is rate-limiting requests. Error: {last_error}"
45
  )
46
+ return (
47
+ "No results found. This may indicate network restrictions or a temporary "
48
+ "DuckDuckGo issue. Try a shorter query or retry later."
49
+ )
50
+