File size: 2,605 Bytes
75d7142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# tools.py
import os
import logging
from typing import List, Dict, Any
from tavily import TavilyClient # <-- Import Tavily
from dotenv import load_dotenv

# Load environment variables to get the API key
load_dotenv()

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - [%(funcName)s] - %(message)s')

# --- Tavily Search Tool ---
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
if not TAVILY_API_KEY:
    raise ValueError("TAVILY_API_KEY not found in environment variables.")

tavily_client = TavilyClient(api_key=TAVILY_API_KEY)

def search_with_tavily(query: str, search_depth: str = "basic", max_results: int = 5, include_answer: bool = False, include_raw_content: bool = False) -> Dict[str, Any]:
    """

    Performs a search using the Tavily Search API.



    Args:

        query: The search query.

        search_depth: "basic" or "advanced". Advanced performs deeper research.

        max_results: The maximum number of search results to return.

        include_answer: Whether to include a synthesized answer from Tavily.

        include_raw_content: Whether to include raw scraped content for results.



    Returns:

        A dictionary containing search results, and potentially an answer

        and raw content, or an error message.

    """
    logging.info(f"Performing Tavily search for: '{query}' (depth: {search_depth}, max_results: {max_results})")
    try:
        # Use tavily_client.search method
        response = tavily_client.search(
            query=query,
            search_depth=search_depth,
            max_results=max_results,
            include_answer=include_answer,
            include_raw_content=include_raw_content,
            # include_images=False, # Optional parameter
        )
        logging.info(f"Tavily search successful for '{query}'. Found {len(response.get('results', []))} results.")
        # response structure includes keys like 'query', 'response_time', 'answer', 'results'
        # 'results' is a list of dicts, each with 'title', 'url', 'content', 'score', 'raw_content' (if requested)
        return response

    except Exception as e:
        logging.error(f"Error during Tavily search for '{query}': {e}")
        # Return an error structure that the agent can recognize
        return {"error": f"Tavily API error: {e}", "results": []}

# --- Remove or Comment Out Old Tools ---
# def search_web(...) -> ...:
#     # ... (keep commented out or remove) ...
# def scrape_website(...) -> ...:
#     # ... (keep commented out or remove) ...