File size: 1,176 Bytes
0d3af20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from agency_swarm.tools import BaseTool
from pydantic import Field
import logging
import os
from googlesearch import search
import json

class SearchAndScrape(BaseTool):
    """
    A tool to perform Google searches and return results.
    """
    
    query: str = Field(
        ..., 
        description="The search query to look up",
        examples=["best restaurants in New York", "how to learn python"]
    )

    def run(self):
        """
        Performs a Google search and returns the search results
        """
        try:
            # Use the stop parameter to limit results
            search_results = search(self.query, stop=5, lang="en")
            
            # Convert generator to list
            results = list(search_results)
            
            return json.dumps({
                "success": True,
                "message": f"Found {len(results)} results for query: {self.query}",
                "results": results
            })
            
        except Exception as e:
            logging.error(f"Search error: {str(e)}")
            return json.dumps({
                "success": False,
                "error": str(e)
            })