Sborole commited on
Commit
80c2545
·
verified ·
1 Parent(s): 3609da6

Update tools/WebSearchTool.py

Browse files
Files changed (1) hide show
  1. tools/WebSearchTool.py +17 -50
tools/WebSearchTool.py CHANGED
@@ -1,63 +1,30 @@
1
- import os
2
  from smolagents import Tool
3
- from googleapiclient.discovery import build
4
 
5
- class GoogleSearchTool(Tool):
6
- # Use a descriptive name to guide the agent
7
- name = "google_search"
8
- # Update the description to reflect the new functionality
9
- description = "Use Google to find current information and general knowledge. Returns a snippet and URL."
10
 
11
  inputs = {
12
- "query": {"type": "string", "description": "The search term to look up."}
13
  }
14
  output_type = "string"
15
 
16
- def __init__(self, **kwargs):
17
- super().__init__(**kwargs)
18
- # Retrieve credentials from environment variables
19
- self.api_key = os.getenv("GOOGLE_API_KEY")
20
- self.cse_id = os.getenv("GOOGLE_CSE_ID")
21
-
22
- if not self.api_key or not self.cse_id:
23
- # Raise an error if credentials are missing
24
- raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID secret not found.")
25
-
26
- # Initialize the Google Custom Search service
27
- # 'customsearch' is the API name, 'v1' is the version
28
- self.service = build(
29
- "customsearch", "v1", developerKey=self.api_key
30
- )
31
-
32
  def forward(self, query: str) -> str:
33
- """
34
- Executes a Google search query and formats the top results.
35
- """
36
  try:
37
- # Execute the search request for up to 3 results
38
- res = self.service.cse().list(
39
- q=query,
40
- cx=self.cse_id,
41
- num=3
42
- ).execute()
 
 
43
 
44
- items = res.get('items', [])
45
-
46
- if not items:
47
- # Return the specific failure message expected by the agent
48
- return "XX record info: No results found."
49
-
50
- search_results = []
51
- for item in items:
52
- # Format the output clearly for the LLM
53
- search_results.append(
54
- f"Title: {item.get('title')}\n"
55
- f"Snippet: {item.get('snippet')}\n"
56
- f"URL: {item.get('link')}"
57
- )
58
 
59
- return "\n---\n".join(search_results)
60
 
61
  except Exception as e:
62
- # Provide an informative error message
63
- return f"Error during Google Search API call: {e}"
 
 
1
  from smolagents import Tool
2
+ from duckduckgo_search import DDGS
3
 
4
+ class DuckDuckGoSearchTool(Tool):
5
+ name = "web_search"
6
+ description = "Use DuckDuckGo to search the web. Returns short snippets and URLs."
 
 
7
 
8
  inputs = {
9
+ "query": {"type": "string", "description": "Search term"}
10
  }
11
  output_type = "string"
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def forward(self, query: str) -> str:
 
 
 
14
  try:
15
+ results = []
16
+ with DDGS() as ddgs:
17
+ for r in ddgs.text(query, max_results=3):
18
+ results.append(
19
+ f"Title: {r.get('title')}\n"
20
+ f"Snippet: {r.get('body')}\n"
21
+ f"URL: {r.get('href')}"
22
+ )
23
 
24
+ if not results:
25
+ return "No search results found."
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ return "\n---\n".join(results)
28
 
29
  except Exception as e:
30
+ return f"DuckDuckGo search error: {e}"