tavily.py


Overview

The tavily.py module provides integration with Tavily, a specialized search and extraction engine optimized for large language models (LLMs). It defines components for performing:

These tools encapsulate parameter validation, API interaction, error handling with retry logic, and result formatting, serving as reusable components within the InfiniFlow agent system.


Classes and Functions

1. TavilySearchParam

Description

Encapsulates configuration parameters for the Tavily search functionality. It inherits from ToolParamBase and defines metadata for the component, including parameter types, defaults, and validation rules.

Properties

Property

Type

Description

Default

meta

ToolMeta

Metadata describing the tool and its parameters.

See below

api_key

str

API key for authenticating requests to the Tavily API.

""

search_depth

str

Level of search detail: "basic" or "advanced".

"basic"

max_results

int

Maximum number of search results to return (1-20).

6

days

int

Number of days to look back for search results (must be >1).

14

include_answer

bool

Whether to include direct answers (if available) in results.

False

include_raw_content

bool

Whether to include raw content of results.

False

include_images

bool

Whether to include images in search results.

False

include_image_descriptions

bool

Whether to include descriptions for images in results.

False

Metadata meta (Partial)

{
    "name": "tavily_search",
    "description": "Tavily is a search engine optimized for LLMs ...",
    "parameters": {
        "query": {
            "type": "string",
            "description": "Search keywords...",
            "default": "{sys.query}",
            "required": True
        },
        "topic": {
            "type": "string",
            "description": "Category of search ('general' or 'news')",
            "enum": ["general", "news"],
            "default": "general",
            "required": False
        },
        "include_domains": {
            "type": "array",
            "description": "Domains to include in results",
            "default": [],
            "items": {"type": "string"}
        },
        "exclude_domains": {
            "type": "array",
            "description": "Domains to exclude",
            "default": [],
            "items": {"type": "string"}
        }
    }
}

Methods

Usage Example

params = TavilySearchParam()
params.api_key = "my_api_key"
params.search_depth = "advanced"
params.max_results = 10
params.query = "machine learning"
params.check()  # Raises error if invalid

2. TavilySearch

Description

A tool class that implements the search functionality against the Tavily API. It inherits from ToolBase and ABC (Abstract Base Class), providing a protected _invoke method that executes the search workflow asynchronously with timeout and retry handling.

Properties

Methods

Implementation Details

Usage Example

search_tool = TavilySearch()
search_tool._param = TavilySearchParam()
search_tool._param.api_key = "my_api_key"
result = search_tool._invoke(query="latest AI breakthroughs")
print(result)

3. TavilyExtractParam

Description

Defines parameters for the Tavily content extraction component. Inherits from ToolParamBase, specifying metadata and validation for extraction options.

Properties

Property

Type

Description

Default

meta

ToolMeta

Metadata specifying the name, description, and parameters of the extraction tool.

See below

api_key

str

API key for Tavily.

""

extract_depth

str

Extraction detail level: "basic" or "advanced".

"basic"

urls

list

List of URLs to extract content from.

[]

format

str

Format of extracted content: "markdown" or "text".

"markdown"

include_images

bool

Whether to include images in extracted content.

False

Metadata meta (Partial)

{
    "name": "tavily_extract",
    "description": "Extract web page content from one or more specified URLs...",
    "parameters": {
        "urls": {
            "type": "array",
            "description": "URLs to extract content from",
            "items": {"type": "string"},
            "required": True
        },
        "extract_depth": {
            "type": "string",
            "description": "Depth of extraction: basic or advanced",
            "enum": ["basic", "advanced"],
            "default": "basic"
        },
        "format": {
            "type": "string",
            "description": "Output format of extracted content",
            "enum": ["markdown", "text"],
            "default": "markdown"
        }
    }
}

Methods

Usage Example

extract_params = TavilyExtractParam()
extract_params.api_key = "my_api_key"
extract_params.urls = ["https://example.com"]
extract_params.extract_depth = "advanced"
extract_params.check()

4. TavilyExtract

Description

A tool class that performs content extraction from URLs via the Tavily API. Inherits from ToolBase and ABC. It defines an _invoke method to execute the extraction process with retries and timeout.

Properties

Methods

Implementation Details

Usage Example

extract_tool = TavilyExtract()
extract_tool._param = TavilyExtractParam()
extract_tool._param.api_key = "my_api_key"
extract_tool._param.urls = ["https://example.com/article"]
result = extract_tool._invoke()
print(result)

Important Implementation Details and Algorithms


Interaction with Other System Components

Together, these classes serve as modular components within the InfiniFlow agent framework, enabling seamless search and extraction workflows powered by Tavily.


Visual Diagram

classDiagram
    class TavilySearchParam {
        +meta: ToolMeta
        +api_key: str
        +search_depth: str
        +max_results: int
        +days: int
        +include_answer: bool
        +include_raw_content: bool
        +include_images: bool
        +include_image_descriptions: bool
        +check()
        +get_input_form() dict
    }

    class TavilySearch {
        +component_name: str = "TavilySearch"
        -tavily_client: TavilyClient
        +_invoke(**kwargs) str
        +thoughts() str
    }

    class TavilyExtractParam {
        +meta: ToolMeta
        +api_key: str
        +extract_depth: str
        +urls: list
        +format: str
        +include_images: bool
        +check()
        +get_input_form() dict
    }

    class TavilyExtract {
        +component_name: str = "TavilyExtract"
        -tavily_client: TavilyClient
        +_invoke(**kwargs) str
        +thoughts() str
    }

    TavilySearchParam <|-- TavilySearch
    TavilyExtractParam <|-- TavilyExtract

Summary

The tavily.py file defines two main tool components for the InfiniFlow agent:

Both components integrate tightly with the InfiniFlow tool framework and the Tavily client, providing reliable, parameterized access to powerful web search and extraction capabilities optimized for large language models.


If you would like further details on integration or usage examples within the broader system, please let me know!