File size: 956 Bytes
5ada353
 
7bccf8e
 
 
5ada353
 
 
 
 
 
 
06c6a48
5ada353
 
 
06c6a48
5ada353
 
 
 
 
06c6a48
5ada353
 
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
from tavily import TavilyClient
from langchain.tools import tool
from config import TAVILY_API_KEY
# import os
# TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")

class SearchTool:
    def __init__(self, api_key: str):
        self.client = TavilyClient(api_key=api_key)

    def search(self, query: str):
        response = self.client.search(query)
        # Extract a string summary of results
        results = response.get("results", [])
        if not results:
            return "No results found."
        # For simplicity, joining first 3 results' titles or snippets
        summaries = [res.get("title", "") or res.get("snippet", "") for res in results[:3]]
        return " | ".join(summaries)

search_tool_instance = SearchTool(api_key=TAVILY_API_KEY)

@tool(description="Tool to search for information on the web using Tavily API and return a summary of results.")
def search_tool(query: str) -> str:
    return search_tool_instance.search(query)