
    3fi"                        d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZmZmZmZ erd dlmZmZmZmZ  ej0                  e      Z G d d	ee      Z G d
 dee      Z G d de      Zy)    )annotationsN)Enum)TYPE_CHECKINGAnyDictListOptionalSequence)BaseChatMessageHistory)	AIMessageBaseMessageHumanMessageSystemMessage)MemoryMemorySearchResultMessageNotFoundErrorc                      e Zd ZdZdZ	 dZy)SearchScopez5Scope for the document search. Messages or Summaries?messagessummaryN)__name__
__module____qualname____doc__r   r        l/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/chat_message_histories/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                  "   e Zd ZdZ	 	 d	 	 	 	 	 	 	 ddZedd       Zedd       Zedd       ZddZ		 d	 	 	 	 	 ddZ
	 d	 	 	 	 	 dd	Z	 d	 	 	 	 	 dd
ZddZddZdej                  ej"                  ddf	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZddZy)ZepChatMessageHistorya,  Chat message history that uses Zep as a backend.

    Recommended usage::

        # Set up Zep Chat History
        zep_chat_history = ZepChatMessageHistory(
            session_id=session_id,
            url=ZEP_API_URL,
            api_key=<your_api_key>,
        )

        # Use a standard ConversationBufferMemory to encapsulate the Zep chat history
        memory = ConversationBufferMemory(
            memory_key="chat_history", chat_memory=zep_chat_history
        )


    Zep provides long-term conversation storage for LLM apps. The server stores,
    summarizes, embeds, indexes, and enriches conversational AI chat
    histories, and exposes them via simple, low-latency APIs.

    For server installation instructions and more, see:
    https://docs.getzep.com/deployment/quickstart/

    This class is a thin wrapper around the zep-python package. Additional
    Zep functionality is exposed via the `zep_summary` and `zep_messages`
    properties.

    For more information on the zep-python package, see:
    https://github.com/getzep/zep-python
    Nc                n    	 ddl m}  |||      | _        || _        y # t        $ r t        d      w xY w)Nr   )	ZepClientzUCould not import zep-python package. Please install it with `pip install zep-python`.)base_urlapi_key)
zep_pythonr&   ImportError
zep_client
session_id)selfr,   urlr(   r&   s        r   __init__zZepChatMessageHistory.__init__H   sE    	, $S'B$  	C 	s    4c                <   | j                         }|sg S g }|j                  rQt        |j                  j                        dkD  r/|j	                  t        |j                  j                               |j                  r|j                  D ]  }|j                  |j                  |j                  |j                  d}|j                  dk(  r'|j	                  t        |j                  |             h|j	                  t        |j                  |              |S )z!Retrieve messages from Zep memoryr   content)uuid
created_attoken_countmetadataai)r2   additional_kwargs)_get_memoryr   lenr2   appendr   r   r3   r4   r5   r6   roler   r   )r-   
zep_memoryr   msgr6   s        r   r   zZepChatMessageHistory.messagesY   s     (,'7'7'9
I&(:%%--.2j6H6H6P6P QR!** HH"%..#&?? #	" 88t#OO!#++R OO$S[[HU  r   c                B    | j                         }|sg S |j                  S ) Retrieve summary from Zep memory)r9   r   r-   r=   s     r   zep_messagesz"ZepChatMessageHistory.zep_messagesy   s&     (,'7'7'9
I"""r   c                l    | j                         }|r|j                  sy|j                  j                  S )r@   N)r9   r   r2   rA   s     r   zep_summaryz!ZepChatMessageHistory.zep_summary   s2     (,'7'7'9
!3!3!!)))r   c                    ddl m} 	 | j                  j                  j	                  | j
                        }|S # |$ r& t        j                  d| j
                   d       Y yw xY w)zRetrieve memory from Zepr   )r   Session z! not found in Zep. Returning NoneN)r)   r   r+   memory
get_memoryr,   loggerwarning)r-   r   r=   s      r   r9   z!ZepChatMessageHistory._get_memory   sd    ,	!%!7!7!B!B4??!SJ   	NN4??++LM 		s   /9 (A$#A$c                >    | j                  t        |      |       y)zConvenience method for adding a human message string to the store.

        Args:
            message: The string contents of a human message.
            metadata: Optional metadata to attach to the message.
        r1   r6   N)add_messager   r-   messager6   s      r   add_user_messagez&ZepChatMessageHistory.add_user_message   s     	g6Jr   c                >    | j                  t        |      |       y)zConvenience method for adding an AI message string to the store.

        Args:
            message: The string contents of an AI message.
            metadata: Optional metadata to attach to the message.
        r1   rL   N)rM   r   rN   s      r   add_ai_messagez$ZepChatMessageHistory.add_ai_message   s     	73hGr   c                    ddl m}m}  ||j                  |j                  |      } ||g      }| j
                  j                  j                  | j                  |       y)z,Append the message to the Zep memory historyr   r   r   r2   r<   r6   r   N)	r)   r   r   r2   typer+   rG   
add_memoryr,   )r-   rO   r6   r   r   zep_messager=   s          r   rM   z!ZepChatMessageHistory.add_message   sL     	/OO',,
 k]3
))$//:Fr   c                   ddl m}m} |D cg c];  } ||j                  |j                  |j
                  j                  dd            = }} ||      }| j                  j                  j                  | j                  |       yc c}w )z-Append the messages to the Zep memory historyr   rT   r6   NrU   rV   )r)   r   r   r2   rW   r8   getr+   rG   rX   r,   r-   r   r   r   rO   rB   r=   s          r   add_messagesz"ZepChatMessageHistory.add_messages   s~    . $
  \\ 2266z4H
 
 \2
))$//:F
s   A B	c                8  K   ddl m}m} |D cg c];  } ||j                  |j                  |j
                  j                  dd            = }} ||      }| j                  j                  j                  | j                  |       d{    yc c}w 7 
w)z<Append the messages to the Zep memory history asynchronouslyr   rT   r6   NrU   rV   )r)   r   r   r2   rW   r8   r[   r+   rG   aadd_memoryr,   r\   s          r   aadd_messagesz#ZepChatMessageHistory.aadd_messages   s     . $
  \\ 2266z4H
 
 \2
oo$$00*MMM
 	Ns   BA B>BBBc                    ddl m}  ||||||      }| j                  j                  j	                  | j
                  ||      S )z1Search Zep memory for messages matching the queryr   )MemorySearchPayload)textr6   search_scopesearch_type
mmr_lambda)limit)r)   rb   r+   rG   search_memoryr,   )	r-   queryr6   rd   re   rf   rg   rb   payloads	            r   searchzZepChatMessageHistory.search   sO     	3%%#!
 %%33OOWE 4 
 	
r   c                    	 | j                   j                  j                  | j                         y# t        $ r& t
        j                  d| j                   d       Y yw xY w)zClear session memory from Zep. Note that Zep is long-term storage for memory
        and this is not advised unless you have specific data retention requirements.
        rF   # not found in Zep. Skipping delete.N)r+   rG   delete_memoryr,   r   rI   rJ   r-   s    r   clearzZepChatMessageHistory.clear   sP    	OO""00A 	NN4??++NO	s   /2 ,A! A!c                   K   	 | j                   j                  j                  | j                         d{    y7 # t        $ r& t
        j                  d| j                   d       Y yw xY ww)zClear session memory from Zep asynchronously.
        Note that Zep is long-term storage for memory and this is not advised
        unless you have specific data retention requirements.
        NrF   rm   )r+   rG   adelete_memoryr,   r   rI   rJ   ro   s    r   aclearzZepChatMessageHistory.aclear   sY     
	//((77HHH 	NN4??++NO	s1   A02> <> A0> ,A-*A0,A--A0)zhttp://localhost:8000N)r,   strr.   rt   r(   Optional[str]returnNone)rv   zList[BaseMessage])rv   zList[Message])rv   ru   )rv   zOptional[Memory])N)rO   rt   r6   Optional[Dict[str, Any]]rv   rw   )rO   r   r6   rx   rv   rw   )r   zSequence[BaseMessage]rv   rw   )ri   rt   r6   zOptional[Dict]rd   r   re   r    rf   zOptional[float]rg   zOptional[int]rv   zList[MemorySearchResult])rv   rw   )r   r   r   r   r/   propertyr   rB   rD   r9   rP   rR   rM   r]   r`   r   r    r!   rk   rp   rs   r   r   r   r$   r$   '   sp   F +!%	%% % 	%
 
%"  > # # * * BF	K	K&>	K		K BF	H	H&>	H		H JNG"G.FG	GG N& $($/$8$8","7"7&*#

 !
 "	

  
 $
 
 
"
0	
r   r$   )
__future__r   loggingenumr   typingr   r   r   r   r	   r
   langchain_core.chat_historyr   langchain_core.messagesr   r   r   r   r)   r   r   r   r   	getLoggerr   rI   rt   r   r    r$   r   r   r   <module>r      sk    "   E E >  MM			8	$)#t )Ed Ea2 ar   