U
    h                     @  s   d dl mZ 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rxd dlmZ G d	d
 d
eeZG dd deeZG dd deZdS )    )annotations)Enum)TYPE_CHECKINGAnyDictListOptional)#AsyncCallbackManagerForRetrieverRunCallbackManagerForRetrieverRun)Document)root_validator)BaseRetriever)MemorySearchResultc                   @  s   e Zd ZdZdZdZdS )SearchScopez1Which documents to search. Messages or Summaries?messagessummaryN)__name__
__module____qualname____doc__r   r    r   r   F/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/retrievers/zep.pyr      s   r   c                   @  s   e Zd ZdZdZdZdS )
SearchTypez-Enumerator of the types of search to perform.
similaritymmrN)r   r   r   r   r   r   r   r   r   r   r      s   r   c                   @  s   e Zd ZU dZdZded< ded< dZded< ded	< d
ed< ejZ	ded< e
jZded< dZded< edd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 )(ZepRetrieveram  `Zep` 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:
        url: URL of your Zep server (required)
        api_key: Your Zep API key (optional)
        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 - Fast, scalable building blocks for LLM Apps
    =========
    Zep is an open source platform for productionizing LLM apps. Go from a prototype
    built in LangChain or LlamaIndex, or a custom app, to production in minutes without
    rewriting code.

    For server installation instructions, see:
    https://docs.getzep.com/deployment/quickstart/
    NzOptional[Any]
zep_clientstrurlzOptional[str]api_key
session_idzOptional[int]top_kr   search_scoper   search_typezOptional[float]
mmr_lambdaT)predict)valuesreturnc                 C  sT   zddl m} W n tk
r,   tdY nX |d||d |dd|d< |S )Nr   )	ZepClientzUCould not import zep-python package. Please install it with `pip install zep-python`.r   r   r   )base_urlr   )Z
zep_pythonr)   ImportErrorget)clsr'   r)   r   r   r   create_clientR   s    
zZepRetriever.create_clientzList[MemorySearchResult]zList[Document])resultsr(   c                 C  s   dd |D S )Nc                 S  s2   g | ]*}|j rt|j d d|ji|j dqS )contentscoreZpage_contentmetadata)messager   popdist.0rr   r   r   
<listcomp>d   s   
z?ZepRetriever._messages_search_result_to_doc.<locals>.<listcomp>r   selfr/   r   r   r   _messages_search_result_to_doca   s    z+ZepRetriever._messages_search_result_to_docc                 C  s   dd |D S )Nc              	   S  s:   g | ]2}|j rt|j j|j|j j|j j|j jd dqS ))r1   uuid
created_attoken_countr2   )r   r   r0   r6   r>   r?   r@   r7   r   r   r   r:   p   s   
z>ZepRetriever._summary_search_result_to_doc.<locals>.<listcomp>r   r;   r   r   r   _summary_search_result_to_docm   s    
z*ZepRetriever._summary_search_result_to_doc)r3   r
   zOptional[Dict[str, Any]])queryrun_managerr3   r(   c                C  sj   ddl m} | jstd|||| j| j| jd}| jjj| j	|| j
d}| jtjkr`| |S | |S Nr   )MemorySearchPayloadzZep client not initialized.)textr3   r"   r#   r$   )limit)zep_python.memoryrE   r   RuntimeErrorr"   r#   r$   memoryZsearch_memoryr    r!   r   r   rA   r=   r<   rB   rC   r3   rE   payloadr/   r   r   r   _get_relevant_documents~   s$      
z$ZepRetriever._get_relevant_documentsr	   c                  sp   ddl m} | jstd|||| j| j| jd}| jjj| j	|| j
dI d H }| jtjkrf| |S | |S rD   )rH   rE   r   rI   r"   r#   r$   rJ   Zasearch_memoryr    r!   r   r   rA   r=   rK   r   r   r   _aget_relevant_documents   s$      
z%ZepRetriever._aget_relevant_documents)r   r   r   r   r   __annotations__r   r   r   r"   r   r   r#   r$   r   r.   r=   rA   rM   rN   r   r   r   r   r   $   s"   
"r   N)
__future__r   enumr   typingr   r   r   r   r   Zlangchain_core.callbacksr	   r
   Zlangchain_core.documentsr   Zlangchain_core.pydantic_v1r   Zlangchain_core.retrieversr   rH   r   r   r   r   r   r   r   r   r   <module>   s   		