
    Ng                        d dl mZ d dlmZmZmZmZmZ d dl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 d dlmZmZ  G d	 d
e          ZdS )    )annotations)TYPE_CHECKINGAnyDictListOptional)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRun)Document)BaseRetriever)model_validator)MemorySearchResultSearchScope
SearchTypeAsyncZepZepc                      e Zd ZU dZded<   	 ded<   	 ded<   	 ded<   	 d	ed
<   	 dZded<   	 dZded<   	 dZded<   	  ed          e	d)d                        Z
d*dZd*dZdd d+d&Zdd d,d(ZdS )-ZepCloudRetrievera1  `Zep Cloud` MemoryStore Retriever.

    Search your user's long-term chat history with Zep.

    Zep offers both simple semantic search and Maximal Marginal Relevance (MMR)
    reranking of search results.

    Note: You will need to provide the user's `session_id` to use this retriever.

    Args:
        api_key: Your Zep API key
        session_id: Identifies your user or a user's session (required)
        top_k: Number of documents to return (default: 3, optional)
        search_type: Type of search to perform (similarity / mmr)
            (default: similarity, optional)
        mmr_lambda: Lambda value for MMR search. Defaults to 0.5 (optional)

    Zep - Recall, understand, and extract data from chat histories.
    Power personalized AI experiences.
    =========
    Zep is a long-term memory service for AI Assistant apps.
    With Zep, you can provide AI assistants with the ability
    to recall past conversations,
    no matter how distant, while also reducing hallucinations, latency, and cost.

    see Zep Cloud Docs: https://help.getzep.com
    strapi_keyr   
zep_clientr   zep_client_async
session_idzOptional[int]top_kmessagesr   search_scope
similarityr   search_typeNzOptional[float]
mmr_lambdabefore)modevaluesdictreturnr   c                $   	 ddl m}m} n# t          $ r t          d          w xY w|                    d          t          d           ||                    d                    |d<    ||                    d                    |d<   |S )	Nr   r   zSCould not import zep-cloud package. Please install it with `pip install zep-cloud`.r   zZep API key is required.)r   r   r   )zep_cloud.clientr   r   ImportErrorget
ValueError)clsr#   r   r   s       d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/retrievers/zep_cloud.pycreate_clientzZepCloudRetriever.create_client@   s    	666666666 	 	 	B  	
 ::i  (7888"s6::i+@+@AAA|%-Xfjj6K6K%L%L%L!"s    %resultsList[MemorySearchResult]List[Document]c                    d |pg D             S )Nc                    g | ]p}|j         	t          t          |j         j                  |j        |j         j        |j         j        |j         j        |j         j        p|j         j	        d           qS ))scoreuuid
created_attoken_countrolepage_contentmetadata)
messager   r   contentr3   uuid_r5   r6   r7   	role_type.0rs     r,   
<listcomp>zDZepCloudRetriever._messages_search_result_to_doc.<locals>.<listcomp>S   s     
 
 
 y
 !233WIO"#)"6#$9#8INAai.A 	 	 	
 
 
     selfr.   s     r,   _messages_search_result_to_docz0ZepCloudRetriever._messages_search_result_to_docP   s)    
 
 ]
 
 
 	
rC   c                    d |D             S )Nc           
         g | ]Y}|j         	t          t          |j         j                  |j        |j         j        |j         j        |j         j        d           ZS ))r3   r4   r5   r6   r8   )summaryr   r   r<   r3   r=   r5   r6   r?   s     r,   rB   zCZepCloudRetriever._summary_search_result_to_doc.<locals>.<listcomp>e   sw     
 
 
 y
 !233WIO"#)"6#$9#8	   
 
 
rC   rD   rE   s     r,   _summary_search_result_to_docz/ZepCloudRetriever._summary_search_result_to_docb   s%    
 
 
 
 
 	
rC   )r:   queryrun_managerr
   r:   Optional[Dict[str, Any]]c          	        | j         st          d          | j         j                            | j        ||| j        | j        | j        | j                  }| j        dk    r| 	                    |          S | 
                    |          S NzZep client not initialized.)textr:   r   r   r    limitrJ   )r   RuntimeErrormemorysearchr   r   r   r    r   rK   rG   rF   rL   rM   r:   r.   s        r,   _get_relevant_documentsz)ZepCloudRetriever._get_relevant_documentss   s      	><===/(//O*(* 0 
 
 	))55g>>>227;;;rC   r	   c          	     &  K   | j         st          d          | j         j                            | j        ||| j        | j        | j        | j                   d {V }| j        dk    r| 	                    |          S | 
                    |          S rP   )r   rS   rT   rU   r   r   r   r    r   rK   rG   rV   s        r,   _aget_relevant_documentsz*ZepCloudRetriever._aget_relevant_documents   s       $ 	><===-4;;O*(* < 
 
 
 
 
 
 
 
 	))55g>>>227;;;rC   )r#   r$   r%   r   )r.   r/   r%   r0   )rL   r   rM   r
   r:   rN   r%   r0   )rL   r   rM   r	   r:   rN   r%   r0   )__name__
__module____qualname____doc____annotations__r   r   r    r   classmethodr-   rG   rK   rW   rY   rD   rC   r,   r   r      sD         8 LLLOOO28OOO$ *L****;*K****6"&J&&&&&_(###   [ $#
 
 
 
$
 
 
 
, .2< < < < < << .2< < < < < < < <rC   r   N)
__future__r   typingr   r   r   r   r   langchain_core.callbacksr	   r
   langchain_core.documentsr   langchain_core.retrieversr   pydanticr   	zep_cloudr   r   r   r'   r   r   r   rD   rC   r,   <module>rg      s   " " " " " " ; ; ; ; ; ; ; ; ; ; ; ; ; ;        . - - - - - 3 3 3 3 3 3 $ $ $ $ $ $ /EEEEEEEEEE........Q< Q< Q< Q< Q< Q< Q< Q< Q< Q<rC   