o
    {qi                     @  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 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)LLMChain)BaseChatMemory)SUMMARY_PROMPTz0.2.12z1.0zRefer 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                   @  s^   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ZdddZdS )SummarizerMixinzMixin for summarizer.ZHumanstrhuman_prefixZAI	ai_prefixr   llmr
   promptztype[BaseMessage]summary_message_clsmessageslist[BaseMessage]existing_summaryreturnc                 C  s0   t || j| jd}t| j| jd}|j||dS N)r   r   )r   r   )summary	new_lines)r	   r   r   r   r   r   Zpredictselfr   r   r   chain r"   _/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/langchain/memory/summary.pypredict_new_summary#   s   z#SummarizerMixin.predict_new_summaryc                   s8   t || j| jd}t| j| jd}|j||dI d H S r   )r	   r   r   r   r   r   Zapredictr   r"   r"   r#   apredict_new_summary1   s   z$SummarizerMixin.apredict_new_summaryN)r   r   r   r   r   r   )__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Ze	d'ddZ
d(ddZed)ddZd* fd"d#Zd+ fd$d%Z  ZS ),ConversationSummaryMemoryzContinually 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   chat_memoryr   r1   intkwargsr   r   c                K  sR   | d||d|}t dt|jj|D ]}||jj|||  |j|_q|S )N)r   r2   r   r"   )rangelenr2   r   r$   r-   )clsr   r2   r1   r4   objir"   r"   r#   from_messagesS   s   	z'ConversationSummaryMemory.from_messages	list[str]c                 C  s   | j gS )zMWill always return list of memory variables.

        :meta private:
        )r/   r    r"   r"   r#   memory_variablesd   s   z*ConversationSummaryMemory.memory_variablesinputsdict[str, Any]c                 C  s(   | j r| j| jdg}n| j}| j|iS )zReturn history buffer.)content)Zreturn_messagesr   r-   r/   )r    r>   r-   r"   r"   r#   load_memory_variablesl   s   
z/ConversationSummaryMemory.load_memory_variablesvaluesdictc                 C  s<   |d j }ddh}|t|krd| 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)r7   rB   Zprompt_variablesZexpected_keysmsgr"   r"   r#   validate_prompt_input_variablest   s   
z9ConversationSummaryMemory.validate_prompt_input_variablesoutputsdict[str, str]Nonec                   s.   t  || | | jjdd | j| _dS )z.Save context from this conversation to buffer.N)supersave_contextr$   r2   r   r-   )r    r>   rI   	__class__r"   r#   rN      s
   
z&ConversationSummaryMemory.save_contextc                   s   t    d| _dS )zClear memory contents.r,   N)rM   clearr-   r<   rO   r"   r#   rQ      s   

zConversationSummaryMemory.clear)
r   r   r2   r   r1   r3   r4   r   r   r+   )r   r;   )r>   r?   r   r?   )rB   rC   r   rC   )r>   r?   rI   rJ   r   rK   )r   rK   )r&   r'   r(   r)   r-   r*   r/   classmethodr:   propertyr=   rA   r   rH   rN   rQ   __classcell__r"   r"   rO   r#   r+   @   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langchain.chains.llmr   Zlangchain.memory.chat_memoryr   Zlangchain.memory.promptr   r   r+   r"   r"   r"   r#   <module>   s0    &