
    3fi;                        d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZ er
d dlmZmZmZ  ej,                         Z G d d	e      Zy)
    )annotationsN)TYPE_CHECKINGAnyDictIterableListOptionalTuple)Document)
Embeddings)VectorStore)CreateDocumentRequestDocumentCollectionResponse
SearchTypec                      e Zd ZdZ	 	 	 	 	 	 d fdZedd       ZddZddZ	 	 d	 	 	 	 	 	 	 ddZ		 	 d	 	 	 	 	 	 	 	 	 ddZ
	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 dd	Z	 	 d	 	 	 	 	 	 	 	 	 	 	 d d
Z	 	 d!	 	 	 	 	 	 	 	 	 d"dZ	 	 d!	 	 	 	 	 	 	 	 	 d#dZ	 	 d!	 	 	 	 	 	 	 	 	 d#dZ	 	 d!	 	 	 	 	 	 	 	 	 d#dZ	 	 d!	 	 	 	 	 	 	 	 	 d"dZ	 	 d!	 	 	 	 	 	 	 	 	 d$dZ	 	 d!	 	 	 	 	 	 	 	 	 d$dZ	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 d&dZ	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 d&dZ	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZ	 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZe	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 d)d       Zd*d+dZ xZS ),ZepCloudVectorStoreau  `Zep` vector store.

    It provides methods for adding texts or documents to the store,
    searching for similar documents, and deleting documents.

    Search scores are calculated using cosine similarity normalized to [0, 1].

    Args:
        collection_name (str): The name of the collection in the Zep store.
        api_key (str): The API key for the Zep API.
    c                    t         |           |st        d      	 ddlm}m}  ||      | _         ||      | _        || _	        | j                          y # t        $ r t        d      w xY w)Nz<collection_name must be specified when using ZepVectorStore.r   )AsyncZepZepz\Could not import zep-python python package. Please install it with `pip install zep-python`.)api_key)super__init__
ValueErrorzep_cloud.clientr   r   ImportError_client_client_asynccollection_name_load_collection)selfr   r   r   r   	__class__s        h/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/vectorstores/zep_cloud.pyr   zZepCloudVectorStore.__init__   s~    
 	N 	6 7+%g6.  	C 	s   A A/c                     y)zUnavailable for ZepCloudN )r    s    r"   
embeddingszZepCloudVectorStore.embeddings6   s         c                    ddl m} 	 | j                  j                  j	                  | j
                        }|S # |$ r7 t        j                  d| j
                   d       | j                         }Y |S w xY w)z;
        Load the collection from the Zep backend.
        r   )NotFoundErrorzCollection z$ not found. Creating new collection.)		zep_cloudr(   r   documentget_collectionr   loggerinfo_create_collection)r    r(   
collections      r"   r   z$ZepCloudVectorStore._load_collection;   s{     	,	3..==d>R>RSJ   	3KKd2233WX 002J	3s   /9 8A54A5c                    | j                   j                  j                  | j                         | j                   j                  j	                  | j                        }|S )z=
        Create a new collection in the Zep backend.
        )r   r*   add_collectionr   r+   )r    r/   s     r"   r.   z&ZepCloudVectorStore._create_collectionK   sH     	,,T-A-AB\\**99$:N:NO
r&   c           	         ddl m} g }t        |      D ]-  \  }}|j                   |||r||   nd |r||   nd              / |S )Nr   )r   )contentmetadatadocument_id)r)   r   	enumerateappend)r    texts	metadatasdocument_idsZepDocument	documentsids           r"   _generate_documents_to_addz.ZepCloudVectorStore._generate_documents_to_addS   s\     	C')	e$ 	DAq-6Yq\D3?QT	 r&   c                    | j                  |||      }| j                  j                  j                  | j                  |      }|S )a  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            document_ids: Optional list of document ids associated with the texts.
            kwargs: vectorstore specific parameters

        Returns:
            List of ids from adding the texts into the vectorstore.
        request)r?   r   r*   add_documentsr   r    r8   r9   r:   kwargsr<   uuidss          r"   	add_textszZepCloudVectorStore.add_textsf   sI    & 33E9lS	%%33  ) 4 
 r&   c                   K   | j                  |||      }| j                  j                  j                  | j                  |       d{   }|S 7 w)zARun more texts through the embeddings and add to the vectorstore.rA   N)r?   r   r*   rC   r   rD   s          r"   
aadd_textszZepCloudVectorStore.aadd_texts   sZ      33E9lS	((11??  ) @ 
 
 	
s   AA
AAc                    |dk(  r | j                   |f||d|S |dk(  r | j                  |f||d|S t        d| d      )>Return docs most similar to query using specified search type.
similaritykr4   mmrsearch_type of ? not allowed. Expected search_type to be 'similarity' or 'mmr'.)similarity_searchmax_marginal_relevance_searchr   r    querysearch_typer4   rN   rE   s         r"   searchzZepCloudVectorStore.search   s}     ,&)4))%R1xR6RRE!5455X17  !+ /; ; r&   c                   K   |dk(  r | j                   |f||d| d{   S |dk(  r | j                  |f||d| d{   S t        d| d      7 67 w)rK   rL   rM   NrO   rP   rQ   )asimilarity_searchamax_marginal_relevance_searchr   rT   s         r"   asearchzZepCloudVectorStore.asearch   s      ,&000X17   E!<<<X17   !+ /; ; s!   AA"AAAAc                d     | j                   |f||d|}|D cg c]  \  }}|	 c}}S c c}}w )"Return docs most similar to query.rM   (_similarity_search_with_relevance_scoresr    rU   rN   r4   rE   resultsdoc_s           r"   rR   z%ZepCloudVectorStore.similarity_search   sF     @$??

-3
 #**Q***s   ,c                .     | j                   |f||d|S )z$Run similarity search with distance.rM   r^   )r    rU   rN   r4   rE   s        r"   similarity_search_with_scorez0ZepCloudVectorStore.similarity_search_with_score   s.     =t<<

-3
 	
r&   c                "    | j                   j                  j                  d| j                  |||d|}|j                  xs g D cg c]<  }t        t        |j                        |j                        |j                  xs df> c}S c c}w )a  
        Default similarity search with relevance scores. Modify if necessary
        in subclass.
        Return docs and relevance scores in the range [0, 1].

        0 is dissimilar, 1 is most similar.

        Args:
            query: input text
            k: Number of Documents to return. Defaults to 4.
            metadata: Optional, metadata filter
            **kwargs: kwargs to be passed to similarity search. Should include:
                score_threshold: Optional, a floating point value between 0 to 1 and
                    filter the resulting set of retrieved docs

        Returns:
            List of Tuples of (doc, similarity_score)
        r   textlimitr4   page_contentr4           r$   )
r   r*   rW   r   ra   r   strr3   r4   scorer    rU   rN   r4   rE   ra   rb   s          r"   r_   z<ZepCloudVectorStore._similarity_search_with_relevance_scores   s    4 /$,,''.. 
 00	

 
  ,"	
  !$S[[!1 \\ 		 S	
 		
 	
s   ABc                >  K    | j                   j                  j                  d| j                  |||d| d{   }|j                  xs g D cg c]<  }t        t        |j                        |j                        |j                  xs df> c}S 7 Zc c}w w)r]   rg   Nrj   rl   r$   )
r   r*   rW   r   ra   r   rm   r3   r4   rn   ro   s          r"   (asimilarity_search_with_relevance_scoresz<ZepCloudVectorStore.asimilarity_search_with_relevance_scores  s      ;**33:: 
 00	

 
 
  ,"	
  !$S[[!1 \\ 		 S	
 		

	
s"   9BBBABBBc                   K    | j                   ||fd|i| d{   }|D cg c]  \  }}|	 c}}S 7 c c}}w w)r]   r4   N)rq   r`   s           r"   rY   z&ZepCloudVectorStore.asimilarity_search  sV      FEE1
'
+1
 
 #**Q**	
 +s   >6	>8>>c                0    t        j                  d       g S )Unsupported in Zep Cloudz9similarity_search_by_vector is not supported in Zep Cloudwarningswarnr    	embeddingrN   r4   rE   s        r"   similarity_search_by_vectorz/ZepCloudVectorStore.similarity_search_by_vector.  s     	QR	r&   c                8   K   t        j                  d       g S w)rt   z:asimilarity_search_by_vector is not supported in Zep Cloudru   rx   s        r"   asimilarity_search_by_vectorz0ZepCloudVectorStore.asimilarity_search_by_vector9  s      	RS	   c           
         | j                   j                  j                  d| j                  |||d|d|}|j                  xs g D cg c],  }t        t        |j                        |j                        . c}S c c}w )a  Return docs selected using the maximal marginal relevance.

        Maximal marginal relevance optimizes for similarity to query AND diversity
        among selected documents.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            fetch_k: Number of Documents to fetch to pass to MMR algorithm.
                     Zep determines this automatically and this parameter is
                        ignored.
            lambda_mult: Number between 0 and 1 that determines the degree
                        of diversity among the results with 0 corresponding
                        to maximum diversity and 1 to minimum diversity.
                        Defaults to 0.5.
            metadata: Optional, metadata to filter the resulting set of retrieved docs
        Returns:
            List of Documents selected by maximal marginal relevance.
        rO   r   rh   ri   r4   rV   
mmr_lambdarj   r$   )	r   r*   rW   r   ra   r   rm   r3   r4   	r    rU   rN   fetch_klambda_multr4   rE   ra   r>   s	            r"   rS   z1ZepCloudVectorStore.max_marginal_relevance_searchD  s    : /$,,''.. 
 00"
 
 __*
 #aii.1::F
 	
 
s   
1A>c           
     "  K    | j                   j                  j                  d| j                  |||d|d| d{   }|j                  xs g D cg c],  }t        t        |j                        |j                        . c}S 7 Jc c}w w)z:Return docs selected using the maximal marginal relevance.rO   r   Nrj   r$   )	r   r*   rW   r   ra   r   rm   r3   r4   r   s	            r"   rZ   z2ZepCloudVectorStore.amax_marginal_relevance_searchp  s      ;**33:: 
 00"
 
 
 __*
 #aii.1::F
 	


s!   ;BBB1B
B
Bc                0    t        j                  d       g S )rt   zEmax_marginal_relevance_search_by_vector is not supported in Zep Cloudru   r    ry   rN   r   r   r4   rE   s          r"   'max_marginal_relevance_search_by_vectorz;ZepCloudVectorStore.max_marginal_relevance_search_by_vector  s     	S	
 	r&   c                8   K   t        j                  d       g S w)rt   zFamax_marginal_relevance_search_by_vector is not supported in Zep Cloudru   r   s          r"   (amax_marginal_relevance_search_by_vectorz<ZepCloudVectorStore.amax_marginal_relevance_search_by_vector  s      	T	
 	r}   c                X    |st        d       | ||      }|j                  ||       |S )a  
        Class method that returns a ZepVectorStore instance initialized from texts.

        If the collection does not exist, it will be created.

        Args:
            texts (List[str]): The list of texts to add to the vectorstore.
            metadatas (Optional[List[Dict[str, Any]]]): Optional list of metadata
               associated with the texts.
            collection_name (str): The name of the collection in the Zep store.
            api_key (str): The API key for the Zep API.
            kwargs: Additional parameters specific to the vectorstore.

        Returns:
            ZepVectorStore: An instance of ZepVectorStore.
        z4api_key must be specified when using ZepVectorStore.)r   r   )r   rG   )clsr8   ry   r9   r   r   rE   vecstores           r"   
from_textszZepCloudVectorStore.from_texts  s:    4 STT+
 	5),r&   c                    |t        |      dk(  rt        d      |D ]2  }| j                  j                  j	                  | j
                  |       4 y)zDelete by Zep vector UUIDs.

        Parameters
        ----------
        ids : Optional[List[str]]
            The UUIDs of the vectors to delete.

        Raises
        ------
        ValueError
            If no UUIDs are provided.
        Nr   zNo uuids provided to delete.)lenr   r   r*   delete_documentr   )r    idsrE   us       r"   deletezZepCloudVectorStore.delete  sP     ;#c(a-;<< 	KALL!!11$2F2FJ	Kr&   )r   rm   r   rm   returnNone)r   zOptional[Embeddings])r   r   )NN)r8   Iterable[str]r9   zOptional[List[Dict[Any, Any]]]r:   Optional[List[str]]r   zList[CreateDocumentRequest])
r8   r   r9   zOptional[List[Dict[str, Any]]]r:   r   rE   r   r   	List[str])N   )rU   rm   rV   r   r4   Optional[Dict[str, Any]]rN   intrE   r   r   List[Document])rU   rm   rV   rm   r4   r   rN   r   rE   r   r   r   )   N)
rU   rm   rN   r   r4   r   rE   r   r   r   )
rU   rm   rN   r   r4   r   rE   r   r   zList[Tuple[Document, float]])
ry   List[float]rN   r   r4   r   rE   r   r   r   )r      g      ?N)rU   rm   rN   r   r   r   r   floatr4   r   rE   r   r   r   )ry   r   rN   r   r   r   r   r   r4   r   rE   r   r   r   )N N)r8   r   ry   r   r9   zOptional[List[dict]]r   rm   r   zOptional[str]rE   r   r   r   )N)r   r   rE   r   r   r   )__name__
__module____qualname____doc__r   propertyr%   r   r.   r?   rG   rI   rW   r[   rR   re   r_   rq   rY   rz   r|   rS   rZ   r   r   classmethodr   r   __classcell__)r!   s   @r"   r   r      s   
     
	 0    59,0	 2 *	
 
%, 59,0	 2 *	
  
: 59,0	 2 *	
  
& .2   +	
   
2 .2  +	
   
4 -1	++ + +	+
 + 
+" -1	

 
 +	

 
 
&
  -1	+
+
 +
 +	+

 +
 
&+
` -1	

 
 +	

 
 
&
> -1	++ + +	+
 + 
+$ -1			 	 +		
 	 
	 -1			 	 +		
 	 
	  -1*
*
 *
 	*

 *
 +*
 *
 
*
^  -1

 
 	

 
 +
 
 

:  -1  	
  +  
$  -1  	
  +  
 
 +/!!%     (	 
       
   DK Kr&   r   )
__future__r   loggingrv   typingr   r   r   r   r   r	   r
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   r)   r   r   r   	getLoggerr,   r   r$   r&   r"   <module>r      sI    "   L L L - 0 3WW				LK+ LKr&   