Shane commited on
Commit
5ba26df
·
1 Parent(s): 68fa990

added google search tool

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. app.py +1 -5
  3. requirements.txt +1 -0
  4. tools/web_search.py +37 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .DS_Store
app.py CHANGED
@@ -34,10 +34,6 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  except Exception as e:
35
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
36
 
37
-
38
- final_answer = FinalAnswerTool()
39
- websearch = DuckDuckGoSearchTool()
40
-
41
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
42
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
43
 
@@ -57,7 +53,7 @@ with open("prompts.yaml", 'r') as stream:
57
 
58
  agent = CodeAgent(
59
  model=model,
60
- tools=[get_current_time_in_timezone, websearch, final_answer], ## add your tools here (don't remove final answer)
61
  max_steps=6,
62
  verbosity_level=1,
63
  grammar=None,
 
34
  except Exception as e:
35
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
36
 
 
 
 
 
37
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
38
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
39
 
 
53
 
54
  agent = CodeAgent(
55
  model=model,
56
+ tools=[get_current_time_in_timezone, GoogleSearchTool(), DuckDuckGoSearchTool(), FinalAnswerTool()], ## add your tools here (don't remove final answer)
57
  max_steps=6,
58
  verbosity_level=1,
59
  grammar=None,
requirements.txt CHANGED
@@ -2,4 +2,5 @@ markdownify
2
  smolagents
3
  requests
4
  duckduckgo_search
 
5
  pandas
 
2
  smolagents
3
  requests
4
  duckduckgo_search
5
+ googlesearch-python
6
  pandas
tools/web_search.py CHANGED
@@ -1,6 +1,7 @@
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"
@@ -25,3 +26,39 @@ class DuckDuckGoSearchTool(Tool):
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 googlesearch
5
 
6
  class DuckDuckGoSearchTool(Tool):
7
  name = "web_search"
 
26
  raise Exception("No results found! Try a less restrictive/shorter query.")
27
  postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
28
  return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
29
+
30
+ class GoogleSearchTool(Tool):
31
+ name = "google_search"
32
+ description = "Performs a Google web search based on your query and returns the top search results."
33
+ inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
34
+ output_type = "string"
35
+
36
+ def __init__(self, max_results=10, **kwargs):
37
+ super().__init__()
38
+ self.max_results = max_results
39
+ try:
40
+ from googlesearch import search
41
+ except ImportError as e:
42
+ raise ImportError(
43
+ "You must install package `googlesearch-python` to run this tool: for instance run `pip install googlesearch-python`."
44
+ ) from e
45
+ self.search = search
46
+
47
+ def forward(self, query: str) -> str:
48
+ results = []
49
+ try:
50
+ # Get search results (URLs only)
51
+ search_results = list(self.search(query, num_results=self.max_results))
52
+
53
+ # If no results found
54
+ if len(search_results) == 0:
55
+ raise Exception("No results found! Try a less restrictive/shorter query.")
56
+
57
+ # Format the results
58
+ for i, url in enumerate(search_results):
59
+ title = f"Result {i+1}" # Basic title since we don't get title data from this library
60
+ results.append(f"[{title}]({url})\n{url}")
61
+
62
+ return "## Search Results\n\n" + "\n\n".join(results)
63
+ except Exception as e:
64
+ return f"Error performing search: {str(e)}"