"""Research tools using Tavily API for web search and content extraction.""" import os from tavily import TavilyClient def get_tavily_client() -> TavilyClient: """Get Tavily client with API key from environment.""" api_key = os.getenv("TAVILY_API_KEY") if not api_key: raise ValueError("TAVILY_API_KEY environment variable not set") return TavilyClient(api_key=api_key) def search_web(query: str, max_results: int = 5, search_depth: str = "basic") -> dict: """ Search the web for information. Args: query: Search query max_results: Maximum number of results search_depth: "basic" or "advanced" Returns: Search results with title, url, and content """ client = get_tavily_client() response = client.search( query=query, search_depth=search_depth, max_results=max_results, include_answer=True ) return response def search_news(query: str, max_results: int = 5, days: int = 7) -> dict: """ Search for recent news articles. Args: query: Search query max_results: Maximum number of results days: How many days back to search Returns: News search results """ client = get_tavily_client() response = client.search( query=query, topic="news", days=days, max_results=max_results, include_answer=True ) return response def research_speaker(speaker_name: str, topic: str = "") -> dict: """ Research a speaker's background. Args: speaker_name: Name of the speaker topic: Optional topic context Returns: Combined research results """ client = get_tavily_client() # Search for speaker background query = f"{speaker_name} professional background expertise" if topic: query += f" {topic}" background = client.search( query=query, search_depth="advanced", max_results=5, include_answer=True ) # Search for recent news/activity news_query = f"{speaker_name} recent news talks publications" recent = client.search( query=news_query, topic="news", days=30, max_results=3, include_answer=True ) return { "background": background, "recent_activity": recent } def fact_check(claim: str) -> dict: """ Fact-check a claim or statement. Args: claim: The claim to verify Returns: Search results related to the claim """ client = get_tavily_client() response = client.search( query=f"fact check: {claim}", search_depth="advanced", max_results=5, include_answer=True ) return response def get_topic_trends(topic: str) -> dict: """ Get recent trends and developments in a topic. Args: topic: The topic to research Returns: Recent developments and trends """ client = get_tavily_client() response = client.search( query=f"{topic} recent developments trends 2024 2025", topic="news", days=30, max_results=5, include_answer=True ) return response