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) })