File size: 1,104 Bytes
cfcf013
0ae9a64
b18df76
9004ba0
b18df76
80c2545
99c200d
b18df76
b0bc6a4
7e141b9
 
 
 
b0bc6a4
bcf7902
 
7e141b9
bcf7902
b18df76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9004ba0
b18df76
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
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."