a
    bg                     @  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)BaseRetriever)model_validator)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   p/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/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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 ))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_lambdabefore)modedictr   )valuesreturnc                 C  sR   zddl m} W n ty*   tdY n0 |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>e   s   
z?ZepRetriever._messages_search_result_to_doc.<locals>.<listcomp>r   selfr0   r   r   r   _messages_search_result_to_docb   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 ))r2   uuid
created_attoken_countr3   )r   r   r1   r7   r?   r@   rA   r8   r   r   r   r;   q   s   
z>ZepRetriever._summary_search_result_to_doc.<locals>.<listcomp>r   r<   r   r   r   _summary_search_result_to_docn   s    
z*ZepRetriever._summary_search_result_to_doc)r4   r
   zOptional[Dict[str, Any]])queryrun_managerr4   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.)textr4   r"   r#   r$   )limit)zep_python.memoryrF   r   RuntimeErrorr"   r#   r$   memoryZsearch_memoryr    r!   r   r   rB   r>   r=   rC   rD   r4   rF   payloadr0   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 rE   )rI   rF   r   rJ   r"   r#   r$   rK   Zasearch_memoryr    r!   r   r   rB   r>   rL   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   classmethodr/   r>   rB   rN   rO   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.retrieversr   Zpydanticr   rI   r   r   r   r   r   r   r   r   r   <module>   s   		