from smolagents import Tool from serpapi.google_search import GoogleSearch import os class SerpApiSearchTool(Tool): name = "web_search" description = "Primary tool: Use SerpAPI to find current info first before Wikipedia." inputs = { "query": { "type": "string", "description": "The search query to look up via SerpAPI." } } output_type = "string" def forward(self, query: str) -> str: params = { "q": query, "api_key": os.getenv("SERPAPI_KEY"), "num": 3 } search = GoogleSearch(params) results = search.get_dict() if "organic_results" not in results: return "XX record info: No results." formatted = [] for item in results.get("organic_results", []): formatted.append( f"Title: {item.get('title')}\n" f"Snippet: {item.get('snippet')}\n" f"URL: {item.get('link')}" ) return "\n---\n".join(formatted) if formatted else "No record info: No results."