File size: 1,347 Bytes
24c28c4
66c2582
2d0eab1
 
81c0d7e
2d0eab1
 
 
68739dd
2d0eab1
 
 
 
 
 
 
b0bc6a4
2d0eab1
b0bc6a4
2d0eab1
bcf7902
2d0eab1
81c0d7e
 
2d0eab1
 
 
 
 
bcf7902
66c107d
2d0eab1
 
 
 
 
 
 
66c107d
2d0eab1
81c0d7e
2d0eab1
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
42
import os
from smolagents import Tool
from tavily import TavilyClient
class TavilySearchTool(Tool):
    """
    General-purpose Tavily web search.
    Use this FIRST before Wikipedia or other tools.
    Returns summarized results with sources.
    """
    
    name = "tavily_search"
    description = (
        "Use Tavily to search the web for up-to-date information. "
        "Returns summarized content and source links."
    )

    inputs = {
        "query": {"type": "string", "description": "Search query."}
    }

    output_type = "string"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        api_key = os.getenv("TAVILY_API_KEY")
        if not api_key:
            raise ValueError("Missing TAVILY_API_KEY in environment variables.")
        self.client = TavilyClient(api_key=api_key)

    def forward(self, query: str) -> str:
        try:
            response = self.client.search(query=query, max_results=5)
            out = []
            for r in response.get("results", []):
                out.append(
                    f"TITLE: {r.get('title')}\n"
                    f"CONTENT: {r.get('content')}\n"
                    f"SOURCE: {r.get('url')}"
                )
            return "\n\n---SEPARATOR---\n\n".join(out)
        except Exception as e:
            return f"Tavily search error: {e}"