o
    Zh                     @   s|   d dl mZmZmZmZ d dl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ddd	d
G dd deeZdS )    )AnyDictListUnion)
deprecated)BaseMessageget_buffer_string)pre_init)BaseChatMemory)SummarizerMixinz0.3.1z1.0.0z_Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/)ZsinceZremovalmessagec                       sf  e Zd ZU dZdZeed< dZeed< dZ	eed< e
deeee f fd	d
Zdeeee f fddZe
dee fddZdeeef deeef fddZdeeef deeef fddZededefddZdeeef deeef ddf fddZdeeef deeef ddf fddZd%ddZd%dd Zd% fd!d"Zd% fd#d$Z  ZS )&ConversationSummaryBufferMemorya  Buffer with summarizer for storing conversation memory.

    Provides a running summary of the conversation together with the most recent
    messages in the conversation under the constraint that the total number of
    tokens in the conversation does not exceed a certain limit.
    i  max_token_limit moving_summary_bufferhistory
memory_keyreturnc                 C   s   |  i | j S )zString buffer of memory.)load_memory_variablesr   self r   V/var/www/html/lang_env/lib/python3.10/site-packages/langchain/memory/summary_buffer.pybuffer   s   z&ConversationSummaryBufferMemory.bufferc                    s   |  i I dH }|| j S )zAsync memory buffer.N)aload_memory_variablesr   )r   memory_variablesr   r   r   abuffer$      
z'ConversationSummaryBufferMemory.abufferc                 C   s   | j gS )zMWill always return list of memory variables.

        :meta private:
        )r   r   r   r   r   r   )   s   z0ConversationSummaryBufferMemory.memory_variablesinputsc                 C   sR   | j j}| jdkr| j| jdg}|| }| jr|}n	t|| j| jd}| j|iS )zReturn history buffer.r   contenthuman_prefix	ai_prefix)	chat_memorymessagesr   summary_message_clsreturn_messagesr   r"   r#   r   r   r   r   Zfirst_messagesZfinal_bufferr   r   r   r   1   s   


z5ConversationSummaryBufferMemory.load_memory_variablesc                    s\   | j  I dH }| jdkr| j| jdg}|| }| jr |}n	t|| j| jd}| j|iS )zHAsynchronously return key-value pairs given the text input to the chain.Nr   r   r!   )	r$   Zaget_messagesr   r&   r'   r   r"   r#   r   r(   r   r   r   r   A   s   


z6ConversationSummaryBufferMemory.aload_memory_variablesvaluesc                 C   s8   |d j }ddh}|t|krtd| d| d|S )z4Validate that prompt input variables are consistent.promptsummary	new_linesz:Got unexpected prompt input variables. The prompt expects z, but it should have .)Zinput_variablesset
ValueError)clsr)   Zprompt_variablesZexpected_keysr   r   r   validate_prompt_input_variablesQ   s   
z?ConversationSummaryBufferMemory.validate_prompt_input_variablesoutputsNc                    s   t  || |   dS )z.Save context from this conversation to buffer.N)supersave_contextpruner   r   r2   	__class__r   r   r4   ]   s   z,ConversationSummaryBufferMemory.save_contextc                    s(   t  ||I dH  |  I dH  dS )z=Asynchronously save context from this conversation to buffer.N)r3   asave_contextapruner6   r7   r   r   r9   b   s   z-ConversationSummaryBufferMemory.asave_contextc                 C   sj   | j j}| j|}|| jkr3g }|| jkr)||d | j|}|| jks| || j| _dS dS )z*Prune buffer if it exceeds max token limitr   N)	r$   r%   llmget_num_tokens_from_messagesr   appendpopZpredict_new_summaryr   r   r   Zcurr_buffer_lengthZpruned_memoryr   r   r   r5   i   s   



z%ConversationSummaryBufferMemory.prunec                    sr   | j j}| j|}|| jkr7g }|| jkr*||d | j|}|| jks| || jI dH | _dS dS )z9Asynchronously prune buffer if it exceeds max token limitr   N)	r$   r%   r;   r<   r   r=   r>   Zapredict_new_summaryr   r?   r   r   r   r:   v   s   


z&ConversationSummaryBufferMemory.aprunec                    s   t    d| _dS )zClear memory contents.r   N)r3   clearr   r   r7   r   r   r@      s   

z%ConversationSummaryBufferMemory.clearc                    s   t   I dH  d| _dS )z%Asynchronously clear memory contents.Nr   )r3   aclearr   r   r7   r   r   rA      r   z&ConversationSummaryBufferMemory.aclear)r   N)__name__
__module____qualname____doc__r   int__annotations__r   strr   propertyr   r   r   r   r   r   r   r   r   r   r	   r1   r4   r9   r5   r:   r@   rA   __classcell__r   r   r7   r   r      s4   
 	""*



r   N)typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.messagesr   r   Zlangchain_core.utilsr	   Zlangchain.memory.chat_memoryr
   Zlangchain.memory.summaryr   r   r   r   r   r   <module>   s    