
    ZiG                         d dl Z d dlmZmZmZ d dlmZ 	 d dlZ ej                    Z	n	#  dZ	Y nxY wd Z
d Zd Z G d d          ZdS )	    N)UnionOptionalLiteral)TavilyClientc           	      
   d}| j                                         D ]}|d         | j        k    r|d         dk    rt          d| j         d          d}|d         d         D ]g}|d	         | j        k    r|d         d
k    rt          d| j         d          |d         dk    r!t          d| j         d|d          d          d} |st          d| j         d          d}|st          d| j         d          dS )aA  
    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        W/root/projects/butler/venv/lib/python3.11/site-packages/tavily/hybrid_rag/hybrid_rag.py_validate_indexr      s    L"6688  =FL((=N** /v| / / / 0 0 0 -.x8 	 	EV} 777V}((  "@6+B "@ "@ "@ A A A|$00  "\6+B "\ "\05l0C"\ "\ "\ ] ] ]  L 	8 7v'> 7 7 7 8 8 8  DB6<BBBCCCD D    c                 F    t                               d| |          j        S )Nzembed-english-v3.0)modeltexts
input_type)coembed
embeddings)r   r	   s     r   _cohere_embedr$   5   s+    88"    	r   c                 z    t                               d| d D             |          }fd|j        D             S )Nzrerank-english-v3.0c                     g | ]
}|d          S content ).0docs     r   
<listcomp>z"_cohere_rerank.<locals>.<listcomp>>   s    #H#H#HsC	N#H#H#Hr   )r   query	documentstop_nc                 >    g | ]}|j                  d |j        iz  S )score)r   relevance_score)r*   resultr.   s     r   r,   z"_cohere_rerank.<locals>.<listcomp>@   s=        	&,7F,B"CC  r   )r!   rerankresults)r-   r.   r/   responses    `  r   _cohere_rerankr7   <   sd    yy4E#H#Hi#H#H#HPU  W WH   &   r   c                   |    e Zd Z	 	 	 	 ddeedf         ded         deded	ed
ee         dee         fdZ	 	 ddZ	dS )TavilyHybridClientr#   r(   Napi_keydb_providermongodbr   r   content_fieldembedding_functionranking_functionc	                     t          |          | _        |dk    rt          d          || _        || _        || _        || _        |t          n|| _        |t          n|| _
        t          |            dS )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.
        r<   z;Only MongoDB is currently supported as a database provider.N)r   tavilyr   r   r   r   r=   r$   r>   r7   r?   r   )	selfr:   r;   r   r   r   r=   r>   r?   s	            r   __init__zTavilyHybridClient.__init__F   s    2 #7++)##Z[[[$
 0*3E3M--Se2B2JP`r   
   Fc                 j   ||}||}|                      |gd          d         }t          | j                            d| j        | j        ||dz   |diddd| j         d	d
iddig                    }|dk    r | j        j        |fd|i|d         }	ng }	d |	D             }
||
z   }t          |          dk    rg S | 
                    |||          }t          |          |k    r
|d|         }|dk    r|dk    rg }|                      d |	D             d          }t          |	          D ]h\  }}||         |d<   |dk    r0|                    | j        |d         | j        |d         i           F ||          }|r|                    |           i| j                            |           |S )au  
        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(   r1   originmax_resultsr5   c                 2    g | ]}|d          |d         ddS )r(   r1   foreign)r(   r1   rO   r)   r*   r3   s     r   r,   z-TavilyHybridClient.search.<locals>.<listcomp>   sC     %
 %
 %
 	 "),# %
 %
 %
r   Fc                     g | ]
}|d          S r'   r)   rS   s     r   r,   z-TavilyHybridClient.search.<locals>.<listcomp>   s    1b1b1b&2C1b1b1br   search_documentr#   Tr(   )r>   listr   	aggregater   r   r=   rA   searchlenr?   	enumerateappendinsert_many)rB   r-   rP   	max_localmax_foreignsave_foreignkwargsquery_embeddingslocal_resultsforeign_resultsprojected_foreign_resultscombined_resultsr.   r#   ir3   s                   r   rX   zTavilyHybridClient.searchn   s|    #I%K22E7NKKAN T_66!Z 1#3%.]&" " 74#577!4 & 	8
    . ??0dk0ZZKZSYZZ[deOO O%
 %
 *%
 %
 %
! )+DD  A%%I  008H+VV  ;../= ??|u44I001b1bRa1b1b1bduvvJ&77 1 1	6'1!}|$4''$$*F9,=-vl/C&     *\&11F 1!((000 O''	222r   )r#   r(   NN)rD   NNF)
__name__
__module____qualname__r   strr   r   callablerC   rX   r)   r   r   r9   r9   E   s         %1!*5937& &39%& !+&
 & "& & !) 2& 'x0& & & &P IM!_  _  _  _  _  _ r   r9   )ostypingr   r   r   rA   r   cohereClientr!   r   r$   r7   r9   r)   r   r   <module>rp      s    				 + + + + + + + + + +      MMM	BB	BBB'D 'D 'DR    H  H  H  H  H  H  H  H  H  H s   * 0