o
    Zh                     @  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r<d 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                   @     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   Y/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/retrievers/zep.pyr          r   c                   @  r   )
SearchTypez-Enumerator of the types of search to perform.
similaritymmrN)r   r   r   r   r   r   r   r   r   r   r      r   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Zd-ddZd-dd Zdd!d.d'd(Zdd!d/d*d+ZdS )0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valuesdictreturnr   c                 C  sN   zddl m} W n ty   tdw |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_clientresultsList[MemorySearchResult]List[Document]c                 C     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   selfr2   r   r   r   _messages_search_result_to_docb   s   z+ZepRetriever._messages_search_result_to_docc                 C  r5   )Nc              	   S  s:   g | ]}|j rt|j j|j|j j|j j|j jd dqS ))r7   uuid
created_attoken_countr8   )r   r   r6   r<   rD   rE   rF   r=   r   r   r   r@   q   s    
z>ZepRetriever._summary_search_result_to_doc.<locals>.<listcomp>r   rA   r   r   r   _summary_search_result_to_docn   s   
z*ZepRetriever._summary_search_result_to_doc)r9   queryrun_managerr
   r9   Optional[Dict[str, Any]]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r0| |S | |S Nr   )MemorySearchPayloadzZep client not initialized.)textr9   r$   r%   r&   )limit)zep_python.memoryrL   r   RuntimeErrorr$   r%   r&   memoryZsearch_memoryr"   r#   r   r   rG   rC   rB   rH   rI   r9   rL   payloadr2   r   r   r   _get_relevant_documents   s    


z$ZepRetriever._get_relevant_documentsr	   c                  sr   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r4| |S | |S rK   )rO   rL   r   rP   r$   r%   r&   rQ   Zasearch_memoryr"   r#   r   r   rG   rC   rR   r   r   r   _aget_relevant_documents   s"   


z%ZepRetriever._aget_relevant_documents)r)   r*   r+   r   )r2   r3   r+   r4   )rH   r   rI   r
   r9   rJ   r+   r4   )rH   r   rI   r	   r9   rJ   r+   r4   )r   r   r   r   r   __annotations__r!   r   r   r$   r   r   r%   r&   r   classmethodr1   rC   rG   rT   rU   r   r   r   r   r   $   s6   
 

"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   rO   r   r   r   r   r   r   r   r   r   <module>   s    		