a
    ag                     @   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                       sh  e Zd ZU dZdZeed< dZeed< dZ	eed< e
eeee f dd	d
Zeeee f dddZe
ee dddZeeef eeef dddZeeef eeef dddZeeedddZeeef eeef dd fddZeeef eeef dd fddZddddZdddd Zdd fd!d"Zd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   m/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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$   s    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)inputsr   c                 C   sR   | j j}| jdkr*| j| jdg}|| }| jr6|}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                    sZ   | j  I dH }| jdkr2| 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)valuesr   c                 C   s8   |d j }ddh}|t|kr4t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_variablesN)r   outputsr   c                    s   t  || |   dS )z.Save context from this conversation to buffer.N)supersave_contextpruner   r   r1   	__class__r   r   r3   ]   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)r2   asave_contextapruner5   r6   r   r   r8   b   s    z-ConversationSummaryBufferMemory.asave_contextc                 C   s^   | j j}| j|}|| jkrZg }|| jkrJ||d | j|}q"| || j| _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   r4   i   s    

z%ConversationSummaryBufferMemory.prunec                    sd   | j j}| j|}|| jkr`g }|| jkrJ||d | j|}q"| || jI dH | _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   r9   v   s    

z&ConversationSummaryBufferMemory.aprunec                    s   t    d| _dS )zClear memory contents.r   N)r2   clearr   r   r6   r   r   r?      s    
z%ConversationSummaryBufferMemory.clearc                    s   t   I dH  d| _dS )z%Asynchronously clear memory contents.Nr   )r2   aclearr   r   r6   r   r   r@      s    z&ConversationSummaryBufferMemory.aclear)__name__
__module____qualname____doc__r   int__annotations__r   strr   propertyr   r   r   r   r   r   r   r   r   r   r	   r0   r3   r8   r4   r9   r?   r@   __classcell__r   r   r6   r   r      s*   
	  &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   