U
    h                     @   sb   d dl 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 G dd deeZdS )	    )AnyDictListUnion)BaseMessageget_buffer_string)pre_init)BaseChatMemory)SummarizerMixinc                       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 )%ConversationSummaryBufferMemoryz7Buffer with summarizer for storing conversation memory.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   C/tmp/pip-unpacked-wheel-bo69hh5q/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   #   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   3   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_variablesC   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   r/   	__class__r   r   r1   O   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)r0   asave_contextapruner3   r4   r   r   r6   T   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   r2   [   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"   r8   r9   r   r:   r;   Zapredict_new_summaryr   r<   r   r   r   r7   h   s    

 z&ConversationSummaryBufferMemory.aprunec                    s   t    d| _dS )zClear memory contents.r   N)r0   clearr   r   r4   r   r   r=   u   s    
z%ConversationSummaryBufferMemory.clearc                    s   t   I dH  d| _dS )z%Asynchronously clear memory contents.Nr   )r0   aclearr   r   r4   r   r   r>   z   s    z&ConversationSummaryBufferMemory.aclear)__name__
__module____qualname____doc__r   int__annotations__r   strr   propertyr   r   r   r   r   r   r   r   r   r   r   r.   r1   r6   r2   r7   r=   r>   __classcell__r   r   r4   r   r   
   s,   
  &
 
r   N)typingr   r   r   r   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
   