a
    bg                     @  s   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rd dlmZmZmZ d dlmZmZ G d	d
 d
eZdS )    )annotations)TYPE_CHECKINGAnyDictListOptional)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRun)Document)BaseRetriever)model_validator)MemorySearchResultSearchScope
SearchTypeAsyncZepZepc                   @  s   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de	dddddZ
dddddZdddd d!Zdd"dd#d$dd%d&d'Zdd"dd(d$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_scopeZ
similarityr   search_typeNzOptional[float]
mmr_lambdabefore)modedictr   )valuesreturnc                 C  sr   zddl m}m} W n ty.   tdY n0 |dd u rFtd||d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    r(   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/retrievers/zep_cloud.pycreate_client@   s    
zZepCloudRetriever.create_clientzList[MemorySearchResult]zList[Document])resultsr"   c                 C  s   dd |pg D S )Nc              
   S  sL   g | ]D}|j rtt|j j|j|j j|j j|j j|j jp>|j j	d dqS ))scoreuuid
created_attoken_countroleZpage_contentmetadata)
messager
   r   contentr,   uuid_r.   r/   r0   Z	role_type.0rr(   r(   r)   
<listcomp>S   s   
zDZepCloudRetriever._messages_search_result_to_doc.<locals>.<listcomp>r(   selfr+   r(   r(   r)   _messages_search_result_to_docP   s    z0ZepCloudRetriever._messages_search_result_to_docc                 C  s   dd |D S )Nc              	   S  s>   g | ]6}|j rtt|j j|j|j j|j j|j jd dqS ))r,   r-   r.   r/   r1   )summaryr
   r   r4   r,   r5   r.   r/   r6   r(   r(   r)   r9   e   s   

zCZepCloudRetriever._summary_search_result_to_doc.<locals>.<listcomp>r(   r:   r(   r(   r)   _summary_search_result_to_docb   s    
z/ZepCloudRetriever._summary_search_result_to_doc)r2   r	   zOptional[Dict[str, Any]])queryrun_managerr2   r"   c             	   C  sR   | j std| j jj| j||| j| j| j| jd}| jdkrH| 	|S | 
|S NzZep client not initialized.)textr2   r   r   r   limitr=   )r   RuntimeErrormemorysearchr   r   r   r   r   r>   r<   r;   r?   r@   r2   r+   r(   r(   r)   _get_relevant_documentss   s    


z)ZepCloudRetriever._get_relevant_documentsr   c             	     sX   | j std| j jj| j||| j| j| j| jdI d H }| jdkrN| 	|S | 
|S rA   )r   rD   rE   rF   r   r   r   r   r   r>   r<   rG   r(   r(   r)   _aget_relevant_documents   s    


z*ZepCloudRetriever._aget_relevant_documents)__name__
__module____qualname____doc____annotations__r   r   r   r   classmethodr*   r<   r>   rH   rI   r(   r(   r(   r)   r      s$   
r   N)
__future__r   typingr   r   r   r   r   Zlangchain_core.callbacksr   r	   Zlangchain_core.documentsr
   Zlangchain_core.retrieversr   Zpydanticr   Z	zep_cloudr   r   r   r#   r   r   r   r(   r(   r(   r)   <module>   s   