Sborole commited on
Commit
2d0eab1
·
verified ·
1 Parent(s): 7de8b44

Update tools/WebSearchTool.py

Browse files
Files changed (1) hide show
  1. tools/WebSearchTool.py +29 -53
tools/WebSearchTool.py CHANGED
@@ -1,66 +1,42 @@
1
  import os
2
  from smolagents import Tool
3
- from googleapiclient.discovery import build
4
- class GoogleSearchTool(Tool):
5
  """
6
- A tool to perform web search Google searches using the Custom Search Engine (CSE) API.
7
- Use this tool first to get the necessary information for calculation or further tools.
 
8
  """
9
- # Use a descriptive name to guide the agent
10
- name = "google_search"
11
- # Update the description to reflect the new functionality
12
- description = "Use Google to find current information and general knowledge. Returns a snippet and URL."
13
- # Define the required input structure for the agent framework
 
 
14
  inputs = {
15
- "query": {"type": "string", "description": "The search term to look up."}
16
  }
 
17
  output_type = "string"
 
18
  def __init__(self, **kwargs):
19
  super().__init__(**kwargs)
20
- # Retrieve credentials from environment variables
21
- self.api_key = os.getenv("GOOGLE_API_KEY")
22
- self.cse_id = os.getenv("GOOGLE_CSE_ID")
23
- # Check for mandatory credentials
24
- if not self.api_key or not self.cse_id:
25
- raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID secret not found. Check environment variables.")
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
  def forward(self, query: str) -> str:
32
- """
33
- Executes a Google search query and formats the top results (up to 3).
34
- The output is formatted to prioritize the Title and Snippet (informational content)
35
- over the URL to address the confusion that it was only returning links.
36
- Args:
37
- query: The search term provided by the agent.
38
- Returns:
39
- A formatted string of search results, or an error message.
40
- """
41
- print(f"Executing Google search for: '{query}'")
42
  try:
43
- # Execute the search request for up to 3 results
44
- res = self.service.cse().list(
45
- q=query,
46
- cx=self.cse_id,
47
- num=3
48
- ).execute()
49
- items = res.get('items', [])
50
- if not items:
51
- # Return the specific failure message expected by the agent
52
- return "XX record info: No results found."
53
- search_results = []
54
- for i, item in enumerate(items):
55
- # We've adjusted the format here to put the snippet first,
56
- # which is usually the most important information for the agent.
57
- search_results.append(
58
- f"RESULT {i+1}: '{item.get('title')}'\n"
59
- f"CONTENT: {item.get('snippet')}\n"
60
- f"SOURCE: {item.get('link')}" # The URL is now clearly labeled as the SOURCE
61
  )
62
- # Join the results with a clear separator
63
- return "\n\n---SEPARATOR---\n\n".join(search_results)
64
  except Exception as e:
65
- # Provide an informative error message upon API failure
66
- return f"Error during Google Search API call: {e}"
 
1
  import os
2
  from smolagents import Tool
3
+ from tavily import TavilyClient
4
+ class TavilySearchTool(Tool):
5
  """
6
+ General-purpose Tavily web search.
7
+ Use this FIRST before Wikipedia or other tools.
8
+ Returns summarized results with sources.
9
  """
10
+
11
+ name = "tavily_search"
12
+ description = (
13
+ "Use Tavily to search the web for up-to-date information. "
14
+ "Returns summarized content and source links."
15
+ )
16
+
17
  inputs = {
18
+ "query": {"type": "string", "description": "Search query."}
19
  }
20
+
21
  output_type = "string"
22
+
23
  def __init__(self, **kwargs):
24
  super().__init__(**kwargs)
25
+ api_key = os.getenv("TAVILY_API_KEY")
26
+ if not api_key:
27
+ raise ValueError("Missing TAVILY_API_KEY in environment variables.")
28
+ self.client = TavilyClient(api_key=api_key)
29
+
 
 
 
 
 
 
30
  def forward(self, query: str) -> str:
 
 
 
 
 
 
 
 
 
 
31
  try:
32
+ response = self.client.search(query=query, max_results=5)
33
+ out = []
34
+ for r in response.get("results", []):
35
+ out.append(
36
+ f"TITLE: {r.get('title')}\n"
37
+ f"CONTENT: {r.get('content')}\n"
38
+ f"SOURCE: {r.get('url')}"
 
 
 
 
 
 
 
 
 
 
 
39
  )
40
+ return "\n\n---SEPARATOR---\n\n".join(out)
 
41
  except Exception as e:
42
+ return f"Tavily search error: {e}"