# ##not implemented include_images and include_image_descriptions attributes # import os # from tavily import TavilyClient # from smolagents import Tool # #from pydantic import BaseModel, Field # from typing import Optional,List, Dict, Any, Literal # # class TavilySearchInput(BaseModel): # # """Input for Tavily Search.""" # # query : str = Field(description="The search query string.") # # max_results : Optional[int] = Field(default = 5, # # description="Maximum number of search results to return.") # # topic : Optional[str] = Field(default = "general", # # description="Category of the search. Can be 'general', 'news', or 'finance'.") # # include_answer : Optional[bool] = Field(default = False, # # description="Whether to include an answer to original query in results.") # # include_raw_content : Optional[bool] = Field(default = False, # # description="Whether to include cleaned and parsed HTML of each search result.") # # search_depth: Optional[str] = Field(default = "basic", # # description="Depth of the search, either 'basic' or 'advanced'.") # # time_range: Optional[str] = Field(default = None, # # description="The time range back from the current date to filter results - 'day', 'week', 'month', or 'year'.") # # include_domains: Optional[List[str]] = Field(default = None, # # description="List of domains to specifically include.") # # exclude_domains: Optional[List[str]] = Field(default = None, # # description="List of domains to specifically exclude.") # # class TavilySearchTool(Tool): # # name: str = "tavily_search" # # description = """ # # A tool to perform comprehensive web searches using the Tavily Search API. # # It can retrieve real-time, accurate, and factual information from across the web. # # Useful for answering questions, gathering information, and reducing hallucinations.""" # # inputs = inputs # # output_type = "list" # # def __init__(self, api_key:Optional[str] = None, **kwargs): # # super().__init__(**kwargs) # # if api_key is None: # # api_key = os.getenv("TRAVILY_SECRET_KEY") # # if not api_key: # # raise ValueError( # # "Tavily API key not found. Please set the TAVILY_API_KEY environment variable " # # "or pass it directly to TavilySearchTool(api_key='YOUR_API_KEY')." # # ) # # self.tavily_client = TavilyClient(api_key=api_key) # # print('TavilyClient is initiated') # # def forward(self, query: str, # # max_results: int = 5, # # include_answer: bool = False, # # include_raw_content: bool = False, # # search_depth: str = "basic", # # topic: str = "general", # # time_range: Optional[str] = None, # # include_domains: Optional[List[str]] = None, # # exclude_domains: Optional[List[str]] = None) -> List[Dict[str, Any]]: # # """ # # Executes a search query using the Tavily API. # # Args: # # query (str): The search query. # # max_results (int): Maximum number of search results to return. # # include_answer (bool): Whether to include a direct answer synthesized from the search results. # # include_raw_content (bool): Whether to include the raw HTML content of the searched pages. # # search_depth (str): Depth of the search, either 'basic' or 'advanced'. # # topic (str): Category of the search. Can be 'general', 'news', or 'finance'. # # time_range (Optional[str]): The time range back from the current date to filter results - 'day', 'week', 'month', or 'year'. # # include_domains (Optional[List[str]]): List of domains to specifically include. # # exclude_domains (Optional[List[str]]): List of domains to specifically exclude. # # Returns: # # List[Dict[str, Any]]: A list of dictionaries, where each dictionary represents a search result. # # Each result typically contains 'title', 'url', and 'content'. # # If include_answer is True, it will also include the 'answer' field # # in the top-level response. # # """ # # try: # # response = self.tavily_client.search( # # query=query, # # max_results=max_results, # # include_answer=include_answer, # # include_raw_content=include_raw_content, # # search_depth=search_depth, # # topic=topic, # # time_range=time_range, # # include_domains=include_domains, # # exclude_domains=exclude_domains # # ) # # return response # # except Exception as e: # # return [{"error": f"Tavily Search Error: {e}"}] # # class TavilySearchTool(Tool): # # name = "tavily_search" # # description = """ # # A tool to perform comprehensive web searches using the Tavily Search API. # # It can retrieve real-time, accurate, and factual information from across the web. # # Useful for answering questions, gathering information, and reducing hallucinations.""" # # inputs = { # # "query": {"type": "string", "description": "The search query string."}, # # "max_results": {"type": "integer", "description": "Maximum number of results.", "default": 5, "nullable": False}, # # "topic": {"type": "string", "description": "Search category: 'general', 'news', or 'finance'.", "default": "general", "nullable": False}, # # "include_answer": {"type": "boolean", "description": "Whether to include answer in results.", "default": False, "nullable": False}, # # "include_raw_content": {"type": "boolean", "description": "Whether to include HTML content.", "default": False, "nullable": False}, # # "search_depth": {"type": "string", "description": "Search depth: 'basic' or 'advanced'.", "default": "basic", "nullable": False}, # # "time_range": {"type": "string", "description": "Filter by time range.", "default": None, "nullable": True}, # # "include_domains": {"type": "array", "description": "List of domains to include.", "default": None, "nullable": True}, # # "exclude_domains": {"type": "array", "description": "List of domains to exclude.", "default": None, "nullable": True}, # # } # # output_type = "any" # # def __init__(self, api_key:Optional[str] = None, **kwargs): # # super().__init__(**kwargs) # # if api_key is None: # # api_key = os.getenv("TRAVILY_SECRET_KEY") # # if not api_key: # # raise ValueError( # # "Tavily API key not found. Please set the TAVILY_API_KEY environment variable " # # "or pass it directly to TavilySearchTool(api_key='YOUR_API_KEY')." # # ) # # self.tavily_client = TavilyClient(api_key=api_key) # # print('TavilyClient is initiated') # # def forward(self, query: str, # # max_results: int = 5, # # include_answer: bool = False, # # include_raw_content: bool = False, # # search_depth: str = "basic", # # topic: str = "general", # # time_range: Optional[str] = None, # # include_domains: Optional[List[str]] = None, # # exclude_domains: Optional[List[str]] = None) -> List[Dict[str, Any]]: # # """ # # Executes a search query using the Tavily API. # # Args: # # query (str): The search query. # # max_results (int): Maximum number of search results to return. # # include_answer (bool): Whether to include a direct answer synthesized from the search results. # # include_raw_content (bool): Whether to include the raw HTML content of the searched pages. # # search_depth (str): Depth of the search, either 'basic' or 'advanced'. # # topic (str): Category of the search. Can be 'general', 'news', or 'finance'. # # time_range (Optional[str]): The time range back from the current date to filter results - 'day', 'week', 'month', or 'year'. # # include_domains (Optional[List[str]]): List of domains to specifically include. # # exclude_domains (Optional[List[str]]): List of domains to specifically exclude. # # Returns: # # List[Dict[str, Any]]: A list of dictionaries, where each dictionary represents a search result. # # Each result typically contains 'title', 'url', and 'content'. # # If include_answer is True, it will also include the 'answer' field # # in the top-level response. # # """ # # try: # # response = self.tavily_client.search( # # query=query, # # max_results=max_results, # # include_answer=include_answer, # # include_raw_content=include_raw_content, # # search_depth=search_depth, # # topic=topic, # # time_range=time_range, # # include_domains=include_domains, # # exclude_domains=exclude_domains # # ) # # return response # # except Exception as e: # # return [{"error": f"Tavily Search Error: {e}"}] # class WebSearchTool(Tool): # name = "web_search" # description = """ # A tool to perform comprehensive web searches using the Tavily Search API. # It can retrieve real-time, accurate, and factual information from across the web. # Useful for answering questions, gathering information, and reducing hallucinations.""" # inputs = { # "query": {"type": "string", # "description": "The search query string." # } # } # output_type = "any" # def __init__(self, api_key:Optional[str] = None, **kwargs): # super().__init__(**kwargs) # if api_key is None: # api_key = os.getenv("TRAVILY_SECRET_KEY") # if not api_key: # raise ValueError( # "Tavily API key not found. Please set the TAVILY_API_KEY environment variable " # "or pass it directly to TavilySearchTool(api_key='YOUR_API_KEY')." # ) # self.tavily_client = TavilyClient(api_key=api_key) # print('TavilyClient is initiated') # def forward(self, # query: str) -> Any: # try: # print(f'trying travily search...') # response = self.tavily_client.search( # query=query, # max_results=5, # search_depth="advanced", # topic='general', # ) # #print(response) # if "results" in response: # return response.get("results", []) # else: # return('try shorter query or different approach.') # except Exception as e: # return [{"error": f"Tavily Search Error: {e}"}]