
    7|h                        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y)    )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y)SearchScopez1Which documents to search. Messages or Summaries?messagessummaryN)__name__
__module____qualname____doc__r   r        a/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/retrievers/zep.pyr   r      s    ;H'G(r   r   c                      e Zd ZdZdZ	 dZy)
SearchTypez-Enumerator of the types of search to perform.
similaritymmrN)r   r   r   r   r   r   r   r   r   r   r      s    7J
CDr   r   c                  0   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y)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)modec           	         	 ddl m} |j                  d ||d   |j                  d                  |d<   |S # t        $ r t        d      w xY w)Nr   )	ZepClientzUCould not import zep-python package. Please install it with `pip install zep-python`.r!   r#   r$   )base_urlr$   )
zep_pythonr-   ImportErrorget)clsvaluesr-   s      r   create_clientzZepRetriever.create_clientR   sd    	,  &zzve}fjj6KL 
|   	C 	s	   9 Ac                    |D cg c]L  }|j                   r>t        |j                   j                  d      d|j                  i|j                         N c}S c c}w )Ncontentscorepage_contentmetadata)messager   popdistselfresultsrs      r   _messages_search_result_to_docz+ZepRetriever._messages_search_result_to_docb   sW     

 yy YY]]95!1667QYY7
 	
 
s   AAc                   |D cg c]z  }|j                   rlt        |j                   j                  |j                  |j                   j                  |j                   j
                  |j                   j                  d      | c}S c c}w )N)r7   uuid
created_attoken_countr8   )r   r   r6   r=   rD   rE   rF   r>   s      r   _summary_search_result_to_docz*ZepRetriever._summary_search_result_to_docn   sp     
 yy YY..VVIINN"#))"6"6#$99#8#8	
 	
 
s   A?B)r:   c                  ddl m} | j                  st        d       |||| j                  | j
                  | j                        }| j                  j                  j                  | j                  || j                        }| j                  t        j                  k(  r| j                  |      S | j                  |      S Nr   )MemorySearchPayloadzZep client not initialized.)textr:   r'   r(   r)   )limit)zep_python.memoryrJ   r!   RuntimeErrorr'   r(   r)   memorysearch_memoryr%   r&   r   r   rG   rB   r?   queryrun_managerr:   rJ   payloadr@   s          r   _get_relevant_documentsz$ZepRetriever._get_relevant_documents   s     	:<==%**((
 -1OO,B,B,P,POOWDJJ -Q -
  3 3355g>>227;;r   c                 K   ddl m} | j                  st        d       |||| j                  | j
                  | j                        }| j                  j                  j                  | j                  || j                         d {   }| j                  t        j                  k(  r| j                  |      S | j                  |      S 7 CwrI   )rM   rJ   r!   rN   r'   r(   r)   rO   asearch_memoryr%   r&   r   r   rG   rB   rQ   s          r   _aget_relevant_documentsz%ZepRetriever._aget_relevant_documents   s      	:<==%**((
 37//2H2H2W2WOOWDJJ 3X 3
 -
  3 3355g>>227;;-
s   BC
CAC)r3   dictreturnr   )r@   zList[MemorySearchResult]rZ   List[Document])rR   r"   rS   r   r:   Optional[Dict[str, Any]]rZ   r[   )rR   r"   rS   r
   r:   r\   rZ   r[   )r   r   r   r   r!   __annotations__r$   r   r   r'   r   r   r(   r)   r   classmethodr4   rB   rG   rU   rX   r   r   r   r    r    $   s   8 !%J$	H!!G]!O$ + 4 4L+4;(33K36"&J&&(#  $

/

	


/
	
, .2<< 4	<
 +< 
<D .2<< 9	<
 +< 
<r   r    N)
__future__r   enumr   typingr   r   r   r   r	   langchain_core.callbacksr
   r   langchain_core.documentsr   langchain_core.retrieversr   pydanticr   rM   r   r"   r   r   r    r   r   r   <module>rf      sS    "  ; ; . 3 $4)#t )Ed ES<= S<r   