Skip to content

TavilySearchTool

autogen.tools.experimental.TavilySearchTool #

TavilySearchTool(*, llm_config=None, tavily_api_key=None)

Bases: Tool

TavilySearchTool is a tool that uses the Tavily Search API to perform a search.

This tool allows agents to leverage the Tavily search engine for information retrieval. It requires a Tavily API key, which can be provided during initialization or set as an environment variable TAVILY_API_KEY.

ATTRIBUTE DESCRIPTION
tavily_api_key

The API key used for authenticating with the Tavily API.

TYPE: str

Initializes the TavilySearchTool.

PARAMETER DESCRIPTION
llm_config

LLM configuration. (Currently unused but kept for potential future integration).

TYPE: Optional[Union[LLMConfig, dict[str, Any]]] DEFAULT: None

tavily_api_key

The API key for the Tavily Search API. If not provided, it attempts to read from the TAVILY_API_KEY environment variable.

TYPE: Optional[str] DEFAULT: None

RAISES DESCRIPTION
ValueError

If tavily_api_key is not provided either directly or via the environment variable.

Source code in autogen/tools/experimental/tavily/tavily_search.py
def __init__(
    self, *, llm_config: Optional[Union[LLMConfig, dict[str, Any]]] = None, tavily_api_key: Optional[str] = None
):
    """
    Initializes the TavilySearchTool.

    Args:
        llm_config (Optional[Union[LLMConfig, dict[str, Any]]]): LLM configuration. (Currently unused but kept for potential future integration).
        tavily_api_key (Optional[str]): The API key for the Tavily Search API. If not provided,
            it attempts to read from the `TAVILY_API_KEY` environment variable.

    Raises:
        ValueError: If `tavily_api_key` is not provided either directly or via the environment variable.
    """
    self.tavily_api_key = tavily_api_key or os.getenv("TAVILY_API_KEY")

    if self.tavily_api_key is None:
        raise ValueError("tavily_api_key must be provided either as an argument or via TAVILY_API_KEY env var")

    def tavily_search(
        query: Annotated[str, "The search query."],
        tavily_api_key: Annotated[Optional[str], Depends(on(self.tavily_api_key))],
        search_depth: Annotated[Optional[str], "Either 'advanced' or 'basic'"] = "basic",
        include_answer: Annotated[Optional[str], "Either 'advanced' or 'basic'"] = "basic",
        include_raw_content: Annotated[Optional[bool], "Include the raw contents"] = False,
        include_domains: Annotated[Optional[list[str]], "Specific web domains to search"] = [],
        num_results: Annotated[int, "The number of results to return."] = 5,
    ) -> list[dict[str, Any]]:
        """
        Performs a search using the Tavily API and returns formatted results.

        Args:
            query: The search query string.
            tavily_api_key: The API key for Tavily (injected dependency).
            search_depth: The depth of the search ('basic' or 'advanced'). Defaults to "basic".
            include_answer: Whether to include an AI-generated answer ('basic' or 'advanced'). Defaults to "basic".
            include_raw_content: Whether to include raw content in the results. Defaults to False.
            include_domains: A list of domains to include in the search. Defaults to [].
            num_results: The maximum number of results to return. Defaults to 5.

        Returns:
            A list of dictionaries, each containing 'title', 'link', and 'snippet' of a search result.

        Raises:
            ValueError: If the Tavily API key is not available.
        """
        if tavily_api_key is None:
            raise ValueError("Tavily API key is missing.")
        return _tavily_search(
            query=query,
            tavily_api_key=tavily_api_key,
            search_depth=search_depth or "basic",
            include_answer=include_answer or "basic",
            include_raw_content=include_raw_content or False,
            include_domains=include_domains or [],
            num_results=num_results,
        )

    super().__init__(
        name="tavily_search",
        description="Use the Tavily Search API to perform a search.",
        func_or_tool=tavily_search,
    )

name property #

name

description property #

description

func property #

func

tool_schema property #

tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

function_schema property #

function_schema

Get the schema for the function.

This is the old way of handling function calls with OpenAI and compatible frameworks. It is provided for backward compatibility.

realtime_tool_schema property #

realtime_tool_schema

Get the schema for the tool.

This is the preferred way of handling function calls with OpeaAI and compatible frameworks.

tavily_api_key instance-attribute #

tavily_api_key = tavily_api_key or getenv('TAVILY_API_KEY')

register_for_llm #

register_for_llm(agent)

Registers the tool for use with a ConversableAgent's language model (LLM).

This method registers the tool so that it can be invoked by the agent during interactions with the language model.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_llm(self, agent: "ConversableAgent") -> None:
    """Registers the tool for use with a ConversableAgent's language model (LLM).

    This method registers the tool so that it can be invoked by the agent during
    interactions with the language model.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    if self._func_schema:
        agent.update_tool_signature(self._func_schema, is_remove=False)
    else:
        agent.register_for_llm()(self)

register_for_execution #

register_for_execution(agent)

Registers the tool for direct execution by a ConversableAgent.

This method registers the tool so that it can be executed by the agent, typically outside of the context of an LLM interaction.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_for_execution(self, agent: "ConversableAgent") -> None:
    """Registers the tool for direct execution by a ConversableAgent.

    This method registers the tool so that it can be executed by the agent,
    typically outside of the context of an LLM interaction.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    agent.register_for_execution()(self)

register_tool #

register_tool(agent)

Register a tool to be both proposed and executed by an agent.

Equivalent to calling both register_for_llm and register_for_execution with the same agent.

Note: This will not make the agent recommend and execute the call in the one step. If the agent recommends the tool, it will need to be the next agent to speak in order to execute the tool.

PARAMETER DESCRIPTION
agent

The agent to which the tool will be registered.

TYPE: ConversableAgent

Source code in autogen/tools/tool.py
def register_tool(self, agent: "ConversableAgent") -> None:
    """Register a tool to be both proposed and executed by an agent.

    Equivalent to calling both `register_for_llm` and `register_for_execution` with the same agent.

    Note: This will not make the agent recommend and execute the call in the one step. If the agent
    recommends the tool, it will need to be the next agent to speak in order to execute the tool.

    Args:
        agent (ConversableAgent): The agent to which the tool will be registered.
    """
    self.register_for_llm(agent)
    self.register_for_execution(agent)