a
    •˜ëjî  ã                   @  sæ   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZ d d	l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ƒƒZedddd�G dd„ deeƒƒZdS )é    )Úannotations)ÚAny)Ú
deprecated)ÚBaseChatMessageHistory)ÚBaseLanguageModel)ÚBaseMessageÚSystemMessageÚget_buffer_string)ÚBasePromptTemplate)Úpre_init)Ú	BaseModel)Úoverride)ÚLLMChain)ÚBaseChatMemory)ÚSUMMARY_PROMPTz0.2.12z1.0zžRefer here for how to incorporate summaries of conversation history: https://langchain-ai.github.io/langgraph/how-tos/memory/add-summary-conversation-history/)ZsinceZremovalÚmessagec                   @  sn   e Zd ZU dZdZded< dZded< ded< eZd	ed
< e	Z
ded< ddddœdd„Zddddœdd„ZdS )ÚSummarizerMixinzMixin for summarizer.ZHumanÚstrÚhuman_prefixZAIÚ	ai_prefixr   Úllmr
   Úpromptztype[BaseMessage]Úsummary_message_clszlist[BaseMessage])ÚmessagesÚexisting_summaryÚreturnc                 C  s0   t || j| jd�}t| j| jd�}|j||d�S )á   Predict a new summary based on the messages and existing summary.

        Args:
            messages: List of messages to summarize.
            existing_summary: Existing summary to build upon.

        Returns:
            A new summary string.
        ©r   r   ©r   r   ©ÚsummaryÚ	new_lines)r	   r   r   r   r   r   Zpredict©Úselfr   r   r!   Úchain© r%   úW/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/memory/summary.pyÚpredict_new_summary$   s    ýz#SummarizerMixin.predict_new_summaryc                 Ã  s6   t || j| jd�}t| j| jd�}|j||d�I dH S )r   r   r   r   N)r	   r   r   r   r   r   Zapredictr"   r%   r%   r&   Úapredict_new_summary;   s    ýz$SummarizerMixin.apredict_new_summaryN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r   r   r   r'   r(   r%   r%   r%   r&   r      s   
	r   z0.3.1z1.0.0z_Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/c                      s°   e Zd ZU dZdZded< dZded< eddœd	d
ddd dœdd„ƒZe	ddœdd„ƒZ
edddœdd„ƒZedddœdd„ƒZddddœ‡ fdd „Zddœ‡ fd!d"„Z‡  ZS )#ÚConversationSummaryMemoryzìContinually summarizes the conversation history.

    The summary is updated after each conversation turn.
    The implementations returns a summary of the conversation history which
    can be used to provide context to the model.
    Ú r   ÚbufferÚhistoryÚ
memory_keyé   )Úsummarize_stepr   r   Úintr   )r   Úchat_memoryr4   Úkwargsr   c                K  sR   | f ||dœ|¤Ž}t dt|jjƒ|ƒD ]$}| |jj||| … |j¡|_q(|S )a½  Create a ConversationSummaryMemory from a list of messages.

        Args:
            llm: The language model to use for summarization.
            chat_memory: The chat history to summarize.
            summarize_step: Number of messages to summarize at a time.
            **kwargs: Additional keyword arguments to pass to the class.

        Returns:
            An instance of ConversationSummaryMemory with the summarized history.
        )r   r6   r   )ÚrangeÚlenr6   r   r'   r0   )Úclsr   r6   r4   r7   ÚobjÚir%   r%   r&   Úfrom_messagesf   s    þz'ConversationSummaryMemory.from_messagesz	list[str])r   c                 C  s   | j gS )zMWill always return list of memory variables.

        :meta private:
        )r2   ©r#   r%   r%   r&   Úmemory_variables‚   s    z*ConversationSummaryMemory.memory_variableszdict[str, Any])Úinputsr   c                 C  s(   | j r| j| jd�g}n| j}| j|iS )zReturn history buffer.)Úcontent)Zreturn_messagesr   r0   r2   )r#   r@   r0   r%   r%   r&   Úload_memory_variablesŠ   s    z/ConversationSummaryMemory.load_memory_variablesÚdict)Úvaluesr   c                 C  s<   |d j }ddh}|t|ƒkr8d|› d|› d�}t|ƒ‚|S )z4Validate that prompt input variables are consistent.r   r    r!   z:Got unexpected prompt input variables. The prompt expects z, but it should have Ú.)Zinput_variablesÚsetÚ
ValueError)r:   rD   Zprompt_variablesZexpected_keysÚmsgr%   r%   r&   Úvalidate_prompt_input_variables“   s    
ÿÿÿz9ConversationSummaryMemory.validate_prompt_input_variableszdict[str, str]ÚNone)r@   Úoutputsr   c                   s.   t ƒ  ||¡ |  | jjdd… | j¡| _dS )z.Save context from this conversation to buffer.éþÿÿÿN)ÚsuperÚsave_contextr'   r6   r   r0   )r#   r@   rK   ©Ú	__class__r%   r&   rN       s
    þz&ConversationSummaryMemory.save_contextc                   s   t ƒ  ¡  d| _dS )zClear memory contents.r/   N)rM   Úclearr0   r>   rO   r%   r&   rQ   ¨   s    
zConversationSummaryMemory.clear)r)   r*   r+   r,   r0   r-   r2   Úclassmethodr=   Úpropertyr?   r   rB   r   rI   rN   rQ   Ú__classcell__r%   r%   rO   r&   r.   S   s   
	ûr.   N)Ú
__future__r   Útypingr   Zlangchain_core._apir   Zlangchain_core.chat_historyr   Zlangchain_core.language_modelsr   Zlangchain_core.messagesr   r   r	   Zlangchain_core.promptsr
   Zlangchain_core.utilsr   Zpydanticr   Ztyping_extensionsr   Zlangchain.chains.llmr   Zlangchain.memory.chat_memoryr   Zlangchain.memory.promptr   r   r.   r%   r%   r%   r&   Ú<module>   s0   ü8ü