
    /j$                          S SK r S SKJrJrJr  S SKrS SKJr   S SKr\R                  " 5       r
S rS rS r " S S5      rg!   Sr
 N= f)	    N)UnionOptionalLiteral)TavilyClientc           	          SnU R                   R                  5        H  nUS   U R                  :w  a  M  US   S:w  a  [        SU R                   S35      eSnUS   S    Hd  nUS	   U R                  :w  a  M  US   S
:w  a  [        SU R                   S35      eUS   S:w  a  [        SU R                   SUS    S35      eSn  O   U(       d  [        SU R                   S35      eSnM     U(       d  [        SU R                   S35      eg)a-  
Check that the index specified by the parameters exists and is a valid vector search index.

Raises:
    ValueError: If the index does not exist, is not of type 'vectorSearch', or if the embeddings field
                does not exist, is not of type 'vector', or has similarity other than 'cosine'.
FnametypevectorSearchzIndex 'z+' exists but is not of type 'vectorSearch'.latestDefinitionfieldspathvectorzField 'z%' exists but is not of type 'vector'.
similaritycosinez' exists but has similarity 'z' instead of 'cosine'.Tz+' does not exist in index '{client.index}'.z' does not exist.N)
collectionlist_search_indexesindex
ValueErrorembeddings_field)clientindex_existsr   field_existsfields        M/app/agent/.venv/lib/python3.13/site-packages/tavily/hybrid_rag/hybrid_rag.py_validate_indexr      si    L""668=FLL(=N*wv||n 5/ / 0 0 -.x8EV} 7 77V}( 76+B+B*C D@ "@ A A|$0 76+B+B*C D005l0C/DDZ"\ ] ]  L 9 wv'>'>&? @7 7 8 8 7 9: 76<<.0ABCC     c                 @    [         R                  SU US9R                  $ )Nzembed-english-v3.0)modeltexts
input_type)coembed
embeddings)r   r	   s     r   _cohere_embedr$   6   s(    88"   j	r   c           	          [         R                  SU U Vs/ s H  o3S   PM	     snUS9nUR                   Vs/ s H   nXR                     SUR                  0-  PM"     sn$ s  snf s  snf )Nzrerank-english-v3.0content)r   query	documentstop_nscore)r!   rerankresultsr   relevance_score)r'   r(   r)   docresponseresults         r   _cohere_rerankr1   =   s|    yy4E>G#His	Ni#HPU  WH
 &&&F 	,,7F,B,B"CC&  $Is
   A"'A'c                       \ rS rSr     SS\\S4   S\S   S\S\S\S	\\   S
\\   S\\	R                     4S jjr  SS jrSrg)TavilyHybridClientF   Napi_keydb_providermongodbr   r   content_fieldembedding_functionranking_functionsessionc
                     [        XS9U l        US:w  a  [        S5      eX0l        X@l        XPl        X`l        Uc  [        OUU l        Uc  [        OUU l
        [        U 5        g)a  
A client for performing hybrid RAG using both the Tavily API and a local database collection.

Parameters:
api_key (str): The Tavily API key. If this is set to None, it will be loaded from the environment variable TAVILY_API_KEY.
db_provider (str): The database provider. Currently only 'mongodb' is supported.
collection (str): The name of the collection in the database that will be used for local search.
index (str): The name of the collection's vector search index.
embeddings_field (str): The name of the field in the collection that contains the embeddings.
content_field (str): The name of the field in the collection that contains the content.
embedding_function (callable): If provided, this function will be used to generate embeddings for the search query and documents.
ranking_function (callable): If provided, this function will be used to rerank the combined results.
session (requests.Session): If provided, this pre-configured session will be used for HTTP requests. When set, api_key is optional.
)r;   r7   z;Only MongoDB is currently supported as a database provider.N)r   tavilyr   r   r   r   r8   r$   r9   r1   r:   r   )
selfr5   r6   r   r   r   r8   r9   r:   r;   s
             r   __init__TavilyHybridClient.__init__G   sb    6 #7<)#Z[[$
 0*3E3M-Se2B2JP`r   c                    Uc  UnUc  UnU R                  U/S5      S   n[        U R                  R                  SU R                  U R
                  UUS-   US.0SSSU R                   3S	S
0SS.0/5      5      nUS:  a#  U R                  R                  " U4SU0UD6S   n	O/ n	U	 V
s/ s H  n
U
S   U
S   SS.PM     nn
X-   n[        U5      S:X  a  / $ U R                  XU5      n[        U5      U:  a  USU nUS:  a  US:w  a  / nU R                  U	 V
s/ s H  oS   PM	     sn
S5      n[        U	5       He  u  pX   U
S'   US:X  a1  UR                  U R                  U
S   U R
                  U
S   05        MC  U" U
5      n
U
(       d  MT  UR                  U
5        Mg     U R                  R                  U5        U$ s  sn
f s  sn
f )a  
Return results for the given query from both the tavily API (foreign) and
the specified mongo collection (local).

Parameters:
query (str): The query to search for.
max_results (int): The maximum number of results to return.
max_local (int): The maximum number of local results to return.
max_foreign (int): The maximum number of foreign results to return.
save_foreign (bool or function): Whether to save the foreign results in the collection.
    If a function is provided, it will be used to transform the foreign results before saving.
Nsearch_queryr   z$vectorSearch   )r   r   queryVectornumCandidateslimitz$project$z$metavectorSearchScorelocal)_idr&   r*   originmax_resultsr,   r&   r*   foreign)r&   r*   rK   Fsearch_documentr#   T)r9   listr   	aggregater   r   r8   r=   searchlenr:   	enumerateappendinsert_many)r>   r'   rL   	max_localmax_foreignsave_foreignkwargsquery_embeddingslocal_resultsforeign_resultsr0   projected_foreign_resultscombined_resultsr(   r#   is                   r   rQ   TavilyHybridClient.searchq   sH    #I%K22E7NKAN T__66!ZZ 11#3%.]&" !"4#5#5"67!4 &	8
  . ?"kk00ZKZSYZ[deO O *%
 *	 "),#
 * 	" %
 )D A%I  00+V ;./= ?|u4I00Ra1bRa2CRa1bduvJ&7	'1}|$4'$$**F9,=--vl/C&  *&1Fv!((0 8  OO''	2U%
. 2cs   +GG)r   r8   r9   r   r   r:   r=   )r#   r&   NNN)
   NNF)__name__
__module____qualname____firstlineno__r   strr   r   callablerequestsSessionr?   rQ   __static_attributes__ r   r   r3   r3   F   s     %1!*593726(39%( !+(
 ( "( ( !) 2( 'x0( h../(T IM!_ r   r3   )ostypingr   r   r   rh   r=   r   cohereClientr!   r   r$   r1   r3   rk   r   r   <module>rp      sQ    	 + +  	B'DRJ  J y	Bs   A A	