
    dhZ                        S SK Jr  S SKrS SKrS SKJrJr  S SKJrJ	r	J
r
JrJrJrJr  S SKJr  S SKJr  S SKJr  \(       a  S SKJr  S SKJr  \R0                  " 5       r\ " S	 S
5      5       r " S S\5      rg)    )annotationsN)asdict	dataclass)TYPE_CHECKINGAnyDictIterableListOptionalTupleDocument)
Embeddings)VectorStore)DocumentCollectionc                  L    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   Srg)CollectionConfig   aV  Configuration for a `Zep Collection`.

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

Attributes:
    name (str): The name of the collection.
    description (Optional[str]): An optional description of the collection.
    metadata (Optional[Dict[str, Any]]): Optional metadata for the collection.
    embedding_dimensions (int): The number of dimensions for the embeddings in
        the collection. This should match the Zep server configuration
        if auto-embed is true.
    is_auto_embedded (bool): A flag indicating whether the collection is
        automatically embedded by Zep.
strnameOptional[str]descriptionOptional[Dict[str, Any]]metadataintembedding_dimensionsboolis_auto_embedded N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r       \/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/zep.pyr   r      s&     I&&r'   r   c                  B  ^  \ rS rSrSrSSSS.           SU 4S jjjr\SS j5       rSS jrSS jr	  S       S S	 jjr
  S         S!S
 jjr  S         S!S jjr  S"           S#S jjr  S"           S#S jjr  S$         S%S jjr  S$         S&S jjr  S$         S&S jjr  S$         S&S jjr  S$         S%S jjr  S$         S'S jjr  S$         S'S jjr    S(             S)S jjr    S(             S)S jjr    S(             S*S jjr    S(             S*S jjr\      S+                 S,S jj5       rS-S.S jjrSrU =r$ )/ZepVectorStore,   a  `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:
    api_url (str): The URL of the Zep API.
    collection_name (str): The name of the collection in the Zep store.
    api_key (Optional[str]): The API key for the Zep API.
    config (Optional[CollectionConfig]): The configuration for the collection.
        Required if the collection does not already exist.
    embedding (Optional[Embeddings]): Optional embedding function to use to
        embed the texts. Required if the collection is not auto-embedded.
Napi_keyconfig	embeddingc               N  > [         TU ]  5         U(       d  [        S5      e SSKJn  U" X#S9U l        Xl        U(       a+  UR                  U R                  :w  a  U R                  Ul        X@l	        U R                  5       U l        XPl        g ! [
         a    [        S5      ef = f)Nz<collection_name must be specified when using ZepVectorStore.r   )	ZepClientz\Could not import zep-python python package. Please install it with `pip install zep-python`.)r-   )super__init__
ValueError
zep_pythonr1   ImportError_clientcollection_namer   _collection_config_load_collection_collection
_embedding)selfr8   api_urlr-   r.   r/   r1   	__class__s          r(   r3   ZepVectorStore.__init__>   s     	N 	, !:. fkkT%9%99..FK"(002#  	C 	s   B B$c                    U R                   $ )z/Access the query embedding object if available.)r<   )r=   s    r(   
embeddingsZepVectorStore.embeddingsa   s     r'   c                    SSK Jn   U R                  R                  R	                  U R
                  5      nU$ ! U a7    [        R                  SU R
                   S35        U R                  5       n U$ f = f)z+
Load the collection from the Zep backend.
r   )NotFoundErrorzCollection z$ not found. Creating new collection.)	r5   rE   r7   documentget_collectionr8   loggerinfo_create_collection)r=   rE   
collections      r(   r:   ZepVectorStore._load_collectionf   s{     	-	3..==d>R>RSJ   	3KKd2233WX 002J	3s   /9 9A65A6c                    U R                   (       d  [        S5      eU R                  R                  R                  " S0 [        U R                   5      D6nU$ )z-
Create a new collection in the Zep backend.
zCCollection config must be specified when creating a new collection.r   )r9   r4   r7   rF   add_collectionr   )r=   rK   s     r(   rJ   !ZepVectorStore._create_collectionv   sT     &&U  \\**99 
T,,-

 r'   c           
        SSK Jn  S nU R                  (       a>  U R                  R                  (       a#  U R                  b  [
        R                  " SSS9  OU R                  b  U R                  R                  [        U5      5      nU R                  (       aW  U R                  R                  [        US   5      :w  a1  [        SU R                  R                   S[        US   5       35      eO / n[        U5       H>  u  pxUR                  U" UU(       a  X'   OS U(       a  X7   OS U(       a  XW   OS S95        M@     U$ )	Nr   r   z{The collection is set to auto-embed and an embedding 
                function is present. Ignoring the embedding function.   )
stacklevelzkThe embedding dimensions of the collection and the embedding function do not match. Collection dimensions: z, Embedding dimensions: )contentr   document_idr/   )zep_python.documentr   r;   r   r<   warningswarnembed_documentslistr   lenr4   	enumerateappend)	r=   texts	metadatasdocument_idsZepDocumentrB   	documentsids	            r(   _generate_documents_to_add)ZepVectorStore._generate_documents_to_add   s*    	@
 0 0 A A*I 
 __(88eEJD$4$4$I$IS1N % !((==> ?JqM*+-  ')	e$DA-6Y\D3?T/9jmt	 % r'   c                    U R                   (       d  [        S5      eU R                  XU5      nU R                   R                  U5      nU$ )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.
<collection should be an instance of a Zep DocumentCollection)r;   r4   rd   add_documentsr=   r]   r^   r_   kwargsra   uuidss          r(   	add_textsZepVectorStore.add_texts   sL    $ N  33ElS	  ..y9r'   c                   #    U R                   (       d  [        S5      eU R                  XU5      nU R                   R                  U5      I Sh  vN nU$  N7f)zARun more texts through the embeddings and add to the vectorstore.rg   N)r;   r4   rd   aadd_documentsri   s          r(   
aadd_textsZepVectorStore.aadd_texts   sX      N  33ElS	&&55i@@ As   AAAAc                    US:X  a  U R                   " U4XCS.UD6$ US:X  a  U R                  " U4XCS.UD6$ [        SU S35      e)>Return docs most similar to query using specified search type.
similaritykr   mmrsearch_type of ? not allowed. Expected search_type to be 'similarity' or 'mmr'.)similarity_searchmax_marginal_relevance_searchr4   r=   querysearch_typer   rv   rj   s         r(   searchZepVectorStore.search   su     ,&))%R1R6RRE!5517  !+ /; ; r'   c                   #    US:X  a  U R                   " U4XCS.UD6I Sh  vN $ US:X  a  U R                  " U4XCS.UD6I Sh  vN $ [        SU S35      e N6 N7f)rs   rt   ru   Nrw   rx   ry   )asimilarity_searchamax_marginal_relevance_searchr4   r|   s         r(   asearchZepVectorStore.asearch   s      ,&0017   E!<<17   !+ /; ; s!   AA"AAAAc                d    U R                   " U4X#S.UD6nU VVs/ sH  u  pgUPM	     snn$ s  snnf )"Return docs most similar to query.ru   (_similarity_search_with_relevance_scoresr=   r}   rv   r   rj   resultsdoc_s           r(   rz    ZepVectorStore.similarity_search  sC     ??

-3
 #**''***s   ,c                ,    U R                   " U4X#S.UD6$ )z$Run similarity search with distance.ru   r   )r=   r}   rv   r   rj   s        r(   similarity_search_with_score+ZepVectorStore.similarity_search_with_score  s*     <<

-3
 	
r'   c                   U R                   (       d  [        S5      eU R                   R                  (       dL  U R                  (       a;  U R                  R	                  U5      nU R                   R
                  " SXRUS.UD6nOU R                   R
                  " U4X#S.UD6nU Vs/ sH6  n[        UR                  UR                  S9UR                  =(       d    S4PM8     sn$ s  snf )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)
rg   r/   limitr   r   r   page_contentr           r   )
r;   r4   r   r<   embed_queryr   r   rS   r   scorer=   r}   rv   r   rj   query_vectorr   r   s           r(   r   7ZepVectorStore._similarity_search_with_relevance_scores"  s    4 N  00T__??66u=L&&-- &(FLG &&--5;G 	
  !$ \\ 		 S 	
 		
 	
s   '<C&c                  #    U R                   (       d  [        S5      eU R                   R                  (       dT  U R                  (       aC  U R                  R	                  U5      nU R                   R
                  " SXRUS.UD6I Sh  vN nO'U R                   R
                  " U4X#S.UD6I Sh  vN nU Vs/ sH6  n[        UR                  UR                  S9UR                  =(       d    S4PM8     sn$  No NIs  snf 7f)r   rg   r   Nr   r   r   r   )
r;   r4   r   r<   r   r   r   rS   r   r   r   s           r(   (asimilarity_search_with_relevance_scores7ZepVectorStore.asimilarity_search_with_relevance_scoresV  s     N  00T__??66u=L ,,44 &(FL G !,,445; G 	
  !$ \\ 		 S 	
 		
	
s6   BDC8	'D0C:1D9<C<5D:D<Dc                   #    U R                   " X4SU0UD6I Sh  vN nU VVs/ sH  u  pgUPM	     snn$  Ns  snnf 7f)r   r   N)r   r   s           r(   r   !ZepVectorStore.asimilarity_searchy  sR      EE
'
+1
 
 #**''**	
 +s   >6	>8>>c                    U R                   (       d  [        S5      eU R                   R                  " SXUS.UD6nU Vs/ sH!  n[        UR                  UR
                  S9PM#     sn$ s  snf )a  Return docs most similar to embedding vector.

Args:
    embedding: Embedding to look up documents similar to.
    k: Number of Documents to return. Defaults to 4.
    metadata: Optional, metadata filter

Returns:
    List of Documents most similar to the query vector.
rg   r   r   r   r;   r4   r   r   rS   r   r=   r/   rv   r   rj   r   r   s          r(   similarity_search_by_vector*ZepVectorStore.similarity_search_by_vector  s    " N  "")) 
8
?E
 

 	  [[ 
 	
 
s    'A*c                   #    U R                   (       d  [        S5      eU R                   R                  " SXUS.UD6nU Vs/ sH!  n[        UR                  UR
                  S9PM#     sn$ s  snf 7f)z-Return docs most similar to embedding vector.rg   r   r   r   r   r   s          r(   asimilarity_search_by_vector+ZepVectorStore.asimilarity_search_by_vector  s      N  "")) 
8
?E
 

 	  [[ 
 	
 
s   A A1'A,)A1c           	        U R                   (       d  [        S5      eU R                   R                  (       dO  U R                  (       a>  U R                  R	                  U5      nU R                   R
                  " SUUUSUS.UD6nO$U R                   R                  " U4UUSUS.UD6u  pU V	s/ sH!  n	[        U	R                  U	R                  S9PM#     sn	$ s  sn	f )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.
rg   rw   r/   r   r   r~   
mmr_lambdar   r   r~   r   r   r   )
r;   r4   r   r<   r   r   search_return_query_vectorr   rS   r   
r=   r}   rv   fetch_klambda_multr   rj   r   r   rc   s
             r(   r{   ,ZepVectorStore.max_marginal_relevance_search  s    : N  00T__??66u=L&&-- &!!& G %)$4$4$O$O%!!&% %!G PWWw!aii!**EwWWWs   /'Cc           	       #    U R                   (       d  [        S5      eU R                   R                  (       dW  U R                  (       aF  U R                  R	                  U5      nU R                   R
                  " SUUUSUS.UD6I Sh  vN nO,U R                   R                  " U4UUSUS.UD6I Sh  vN u  pU V	s/ sH!  n	[        U	R                  U	R                  S9PM#     sn	$  N_ N6s  sn	f 7f):Return docs selected using the maximal marginal relevance.rg   rw   r   Nr   r   r   )
r;   r4   r   r<   r   r   asearch_return_query_vectorr   rS   r   r   s
             r(   r   -ZepVectorStore.amax_marginal_relevance_search  s      N  00T__??66u=L ,,44 &!!&  G +/*:*:*V*V+!!&+ + %!G PWWw!aii!**EwWW%% Xs6   B	C4C+*C46C-7
C4'C/(C4-C4/C4c           	         U R                   (       d  [        S5      eU R                   R                  " SUUUSUS.UD6nU Vs/ sH!  n[        UR                  UR
                  S9PM#     sn$ s  snf )a8  Return docs selected using the maximal marginal relevance.

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

Args:
    embedding: Embedding 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.
rg   rw   r   r   r   r   	r=   r/   rv   r   r   r   rj   r   rc   s	            r(   'max_marginal_relevance_search_by_vector6ZepVectorStore.max_marginal_relevance_search_by_vector!  s~    8 N  "")) 
"
 
 PWWw!aii!**EwWWWs   'A-c           	        #    U R                   (       d  [        S5      eU R                   R                  " SUUUSUS.UD6I Sh  vN nU Vs/ sH!  n[        UR                  UR
                  S9PM#     sn$  N2s  snf 7f)r   rg   rw   r   Nr   r   )r;   r4   r   r   rS   r   r   s	            r(   (amax_marginal_relevance_search_by_vector7ZepVectorStore.amax_marginal_relevance_search_by_vectorM  s      N  ((00 
"
 
 
 PWWw!aii!**EwWW
 Xs$   AA>A7A>'A94A>9A>c                <    U " UUUUUS9n	U	R                  X5        U	$ )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.
    embedding (Optional[Embeddings]): Optional embedding function to use to
       embed the texts.
    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_url (str): The URL of the Zep API.
    api_key (Optional[str]): The API key for the Zep API.
    config (Optional[CollectionConfig]): The configuration for the collection.
    kwargs: Additional parameters specific to the vectorstore.

Returns:
    ZepVectorStore: An instance of ZepVectorStore.
r,   )rl   )
clsr]   r/   r^   r8   r>   r-   r.   rj   vecstores
             r(   
from_textsZepVectorStore.from_textsg  s3    @ 
 	5,r'   c                    Ub  [        U5      S:X  a  [        S5      eU R                  c  [        S5      eU H  nU R                  R                  U5        M      g)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.zNo collection name provided.)rZ   r4   r;   delete_document)r=   idsrj   us       r(   deleteZepVectorStore.delete  sU     ;#c(a-;<<#;<<A,,Q/ r'   )r7   r;   r9   r<   r8   )r8   r   r>   r   r-   r   r.   Optional[CollectionConfig]r/   Optional[Embeddings]returnNone)r   r   )r   r   )NN)r]   Iterable[str]r^   zOptional[List[Dict[Any, Any]]]r_   Optional[List[str]]r   zList[ZepDocument])
r]   r   r^   zOptional[List[Dict[str, Any]]]r_   r   rj   r   r   	List[str])N   )r}   r   r~   r   r   r   rv   r   rj   r   r   List[Document])   N)
r}   r   rv   r   r   r   rj   r   r   r   )
r}   r   rv   r   r   r   rj   r   r   zList[Tuple[Document, float]])
r/   List[float]rv   r   r   r   rj   r   r   r   )r   r   g      ?N)r}   r   rv   r   r   r   r   floatr   r   rj   r   r   r   )r/   r   rv   r   r   r   r   r   r   r   rj   r   r   r   )NN r   NN)r]   r   r/   r   r^   zOptional[List[dict]]r8   r   r>   r   r-   r   r.   r   rj   r   r   r*   )N)r   r   rj   r   r   r   )r    r!   r"   r#   r$   r3   propertyrB   r:   rJ   rd   rl   rp   r   r   rz   r   r   r   r   r   r   r{   r   r   r   classmethodr   r   r&   __classcell__)r?   s   @r(   r*   r*   ,   sj   , "&-1*.$$ $
 $ +$ ($ 
$ $F     59,0	(( 2( *	(
 
(Z 59,0	 2 *	
  
> 59,0	 2 *	
  
, .2  +	
   
2 .2  +	
   
4 -1	++ + +	+
 + 
+" -1	

 
 +	

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

 2
 
&2
n -1	!
!
 !
 +	!

 !
 
&!
L -1	++ + +	+
 + 
+$ -1	 
 
  
 +	 

  
 
 
J -1	

 
 +	

 
 

8  -16X6X 6X 	6X
 6X +6X 6X 
6Xv  -1$X$X $X 	$X
 $X +$X $X 
$XR  -1*X*X *X 	*X
 *X +*X *X 
*X^  -1XX X 	X
 X +X X 
X4  +/*.!!%-1'' (' (	'
 ' ' ' +' ' 
' 'R0 0r'   r*   )
__future__r   loggingrV   dataclassesr   r   typingr   r   r   r	   r
   r   r   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   rU   r`   r   	getLoggerrH   r   r*   r   r'   r(   <module>r      sc    "   ) L L L - 0 3;6 
			   .z	0[ z	0r'   