
    Ng                        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
ee          Z G d dee          Z G 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        ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/retrievers/zep.pyr   r      s#        ;;H'G((r   r   c                      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#        77J
CDDr   r   c                     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          ed'd                        Zd(dZd(dZddd)d$Z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valuesdictreturnr   c           	         	 ddl m} n# t          $ r t          d          w xY w|                    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$   )
zep_pythonr0   ImportErrorget)clsr,   r0   s      r   create_clientzZepRetriever.create_clientR   s    	,,,,,,, 	 	 	C  	
  &zzIve}fjj6K6KLLL 
  
| s   	 #resultsList[MemorySearchResult]List[Document]c                    d |D             S )Nc                    g | ]@}|j         	t          |j                             d           d|j        i|j                   AS )contentscorepage_contentmetadata)messager   popdist.0rs     r   
<listcomp>z?ZepRetriever._messages_search_result_to_doc.<locals>.<listcomp>e   sc     
 
 

 y
Y]]955!167QY7  
 
 
r   r   selfr7   s     r   _messages_search_result_to_docz+ZepRetriever._messages_search_result_to_docb   s%    
 

 
 
 
 	
r   c                    d |D             S )Nc           
         g | ]L}|j         	t          |j         j        |j        |j         j        |j         j        |j         j        d           MS ))r=   uuid
created_attoken_countr>   )r   r   r<   rC   rM   rN   rO   rD   s     r   rG   z>ZepRetriever._summary_search_result_to_doc.<locals>.<listcomp>q   so     
 
 
 y
Y.VIN"#)"6#$9#8	   
 
 
r   r   rH   s     r   _summary_search_result_to_docz*ZepRetriever._summary_search_result_to_docn   s%    
 
 
 
 
 	
r   )r@   queryrun_managerr   r@   Optional[Dict[str, Any]]c               N   ddl m} | j        st          d           |||| j        | j        | j                  }| j        j                            | j	        || j
                  }| j        t          j        k    r|                     |          S |                     |          S Nr   )MemorySearchPayloadzZep client not initialized.)textr@   r'   r(   r)   )limit)zep_python.memoryrV   r!   RuntimeErrorr'   r(   r)   memorysearch_memoryr%   r&   r   r   rP   rJ   rI   rQ   rR   r@   rV   payloadr7   s          r   _get_relevant_documentsz$ZepRetriever._get_relevant_documents   s     	:99999 	><===%%*(
 
 
 -1O,B,P,POWDJ -Q -
 -
  33355g>>>227;;;r   r
   c               ^  K   ddl m} | j        st          d           |||| j        | j        | j                  }| j        j                            | j	        || j
                   d {V }| j        t          j        k    r|                     |          S |                     |          S rU   )rY   rV   r!   rZ   r'   r(   r)   r[   asearch_memoryr%   r&   r   r   rP   rJ   r]   s          r   _aget_relevant_documentsz%ZepRetriever._aget_relevant_documents   s       	:99999 	><===%%*(
 
 
 37/2H2W2WOWDJ 3X 3
 3
 -
 -
 -
 -
 -
 -
  33355g>>>227;;;r   )r,   r-   r.   r   )r7   r8   r.   r9   )rQ   r"   rR   r   r@   rS   r.   r9   )rQ   r"   rR   r
   r@   rS   r.   r9   )r   r   r   r   r!   __annotations__r$   r   r   r'   r   r   r(   r)   r   classmethodr6   rJ   rP   r_   rb   r   r   r   r    r    $   sX         8 !%J$$$$HHH!!G!!!!OOO$ + 4L4444;(3K33336"&J&&&&&_(###   [ $#

 

 

 


 
 
 
, .2< < < < < <D .2< < < < < < < <r   r    N)
__future__r   enumr   typingr   r   r   r   r	   langchain_core.callbacksr
   r   langchain_core.documentsr   langchain_core.retrieversr   pydanticr   rY   r   r"   r   r   r    r   r   r   <module>rl      sk   " " " " " "       ; ; ; ; ; ; ; ; ; ; ; ; ; ;        . - - - - - 3 3 3 3 3 3 $ $ $ $ $ $ 5444444) ) ) ) )#t ) ) )E E E E Ed E E ES< S< S< S< S<= S< S< S< S< S<r   