Skip to content

Tavily research

TavilyToolSpec #

Bases: BaseToolSpec

Tavily tool spec.

Source code in llama-index-integrations/tools/llama-index-tools-tavily-research/llama_index/tools/tavily_research/base.py
 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
class TavilyToolSpec(BaseToolSpec):
    """Tavily tool spec."""

    spec_functions = [
        "search",
    ]

    def __init__(self, api_key: str) -> None:
        """Initialize with parameters."""
        from tavily import TavilyClient

        self.client = TavilyClient(api_key=api_key)

    def search(self, query: str, max_results: Optional[int] = 6) -> List[Document]:
        """
        Run query through Tavily Search and return metadata.

        Args:
            query: The query to search for.
            max_results: The maximum number of results to return.

        Returns:
            results: A list of dictionaries containing the results:
                url: The url of the result.
                content: The content of the result.

        """
        response = self.client.search(
            query, max_results=max_results, search_depth="advanced"
        )
        return [
            Document(text=result["content"], extra_info={"url": result["url"]})
            for result in response["results"]
        ]

search #

search(query: str, max_results: Optional[int] = 6) -> List[Document]

Run query through Tavily Search and return metadata.

Parameters:

Name Type Description Default
query str

The query to search for.

required
max_results Optional[int]

The maximum number of results to return.

6

Returns:

Name Type Description
results List[Document]

A list of dictionaries containing the results: url: The url of the result. content: The content of the result.

Source code in llama-index-integrations/tools/llama-index-tools-tavily-research/llama_index/tools/tavily_research/base.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def search(self, query: str, max_results: Optional[int] = 6) -> List[Document]:
    """
    Run query through Tavily Search and return metadata.

    Args:
        query: The query to search for.
        max_results: The maximum number of results to return.

    Returns:
        results: A list of dictionaries containing the results:
            url: The url of the result.
            content: The content of the result.

    """
    response = self.client.search(
        query, max_results=max_results, search_depth="advanced"
    )
    return [
        Document(text=result["content"], extra_info={"url": result["url"]})
        for result in response["results"]
    ]