API Reference
Client
The TavilyHybridClient
class is your gateway to Tavily Hybrid RAG. There are a few important parameters to keep in mind when you are instantiating a Tavily Hybrid Client.
Constructor parameters
api_key
: str - Your Tavily API Keydb_provider
: str - Your database provider. Currently, only"mongodb"
is supported.collection
: Collection - A reference to the MongoDB collection that will be used for local search.index
: str - The name of the vector index to be used for vector search on the specifiedcollection
.embeddings_field
: str (optional) - The name of the field that stores the embeddings in the specified collection. This field MUST be the same one used in the specified index. This will also be used when inserting web search results in the database using our default function. Default is"embeddings"
.content_field
: str (optional) - The name of the field that stores the text content in the specified collection. This will also be used when inserting web search results in the database using our default function. Default is"content"
.embedding_function
: function (optional) - A custom embedding function (if you want to use one). The function MUST take in astr
and return alist[float]
. If no function is provided, defaults to Cohere's Embed. Keep in mind that you shouldn't mix different embeddings in the same database collection.ranking_function
: function (optional) - A custom ranking function (if you want to use one). If no function is provided, defaults to Cohere's Rerank. It should return an orderedlist[dict]
where the documents are sorted by decreasing relevancy to your query. Each returned document will have two properties -content
, which is astr
, andscore
, which is afloat
. The fuction MUST accept the following parameters:query
: str - This is the query you are executing. When your ranking function is called during Hybrid RAG, thequery
parameter of yoursearch
call (more details below) will be passed asquery
.documents
list[dict]: - This is the list of documents that are returned by your Hybrid RAG call and that you want to sort. Each document will have two properties -content
, which is astr
, andscore
, which is afloat
.top_n
: int - This is the number of results you want to return after ranking. When your ranking function is called during Hybrid RAG, themax_results
value will be passed astop_n
.
Methods
search
(query, max_results=10, max_local=None, max_foreign=None, save_foreign=False, **kwargs)Performs a Tavily Hybrid RAG query and returns the retrieved documents as a
list[dict]
where the documents are sorted by decreasing relevancy to your query. Each returned document will have three properties -content
(str
),score
(float
), andorigin
, which is eitherlocal
orforeign
.Core Parameters
query
: str - The query you want to search for.max_results
: int - The maximum number of total search results to return. Default is10
.max_local
: int - The maximum number of local search results to return. Default isNone
, which defaults tomax_results
.max_foreign
: int - The maximum number of web search results to return. Default isNone
, which defaults tomax_results
.save_foreign
: Union[bool, function] - Save documents from the web search in the local database. IfTrue
is passed, our default saving function (which only saves the contentstr
and the embeddinglist[float]
will be used.) IfFalse
is passed, no web search result documents will be saved in the local database. If a function is passed, that function MUST take in adict
as a parameter, and return anotherdict
. The inputdict
contains all properties of the returned Tavily result object. The output dict is the final document that will be inserted in the database. You are free to add to it any fields that are supported by the database, as well as remove any of the default ones. If this function returnsNone
, the document will not be saved in the database.
Additional parameters can be provided as keyword arguments (detailed below). The keyword arguments supported by this method are:
search_depth
,topic
,include_raw_content
,include_domains
,exclude_domains
. Refer to the Tavily Search API Reference for more information on these parameters.