a
    •˜ëjR  ã                   @   s€   d dl 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 d dlmZ edd	d
d�G dd„ deeƒƒZdS )é    )ÚAnyÚUnion)Ú
deprecated)ÚBaseMessageÚget_buffer_string)Úpre_init)Úoverride)Ú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                       sp  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ef eeef dœdd„ƒZe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   ú^/mnt/bigstor/www/usecases/.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   2   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   E   s    
ÿýz6ConversationSummaryBufferMemory.aload_memory_variables)Úvaluesr   c                 C   s<   |d j }ddh}|t|ƒkr8d|› d|› d�}t|ƒ‚|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_keysÚmsgr   r   r   Úvalidate_prompt_input_variablesX   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   e   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   j   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 limit.r   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   s   s    

þz%ConversationSummaryBufferMemory.prunec                 Ã   sd   | j j}| j |¡}|| jkr`g }|| jkrJ| | d¡¡ | j |¡}q"|  || j¡I dH | _dS )z:Asynchronously prune buffer if it exceeds max token limit.r   N)	r"   r#   r:   r;   r   r<   r=   Zapredict_new_summaryr   r>   r   r   r   r9   �   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   Úlistr   r   r   r   r   Údictr   r   r   r   r0   r3   r8   r4   r9   r?   r@   Ú__classcell__r   r   r6   r   r      s0   
	""&

ü	r   N)Útypingr   r   Zlangchain_core._apir   Zlangchain_core.messagesr   r   Zlangchain_core.utilsr   Ztyping_extensionsr   Zlangchain.memory.chat_memoryr	   Zlangchain.memory.summaryr
   r   r   r   r   r   Ú<module>   s   ü