
    3fi                       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       G d de             Z eddd       G d dee             Zy)    )annotations)Any)
deprecated)BaseChatMessageHistory)BaseLanguageModel)BaseMessageSystemMessageget_buffer_string)BasePromptTemplate)pre_init)	BaseModel)override)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/)sinceremovalmessagec                  |    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Z	 	 	 	 	 	 ddZy)SummarizerMixinzMixin for summarizer.Humanstrhuman_prefixAI	ai_prefixr   llmr   promptztype[BaseMessage]summary_message_clsc                    t        || j                  | j                        }t        | j                  | j
                        }|j                  ||      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   predictselfmessagesexisting_summaryr%   chains        ^/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/memory/summary.pypredict_new_summaryz#SummarizerMixin.predict_new_summary$   sK     &**nn
	 TXXdkk:}}%5}KK    c                   K   t        || j                  | j                        }t        | j                  | j
                        }|j                  ||       d{   S 7 w)r    r!   r"   r#   N)r
   r   r   r   r   r   apredictr'   s        r,   apredict_new_summaryz$SummarizerMixin.apredict_new_summary;   sU      &**nn
	 TXXdkk:^^,<	^RRRRs   AA#A!A#N)r)   zlist[BaseMessage]r*   r   returnr   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r	   r   r-   r1    r.   r,   r   r      s      L#Is	!/F/-:*:L#L L 
	L.S#S S 
	Sr.   r   z0.3.1z1.0.0z_Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/c                       e Zd ZU dZdZded<   dZded<   edd	 	 	 	 	 	 	 	 	 dd	       Ze	dd
       Z
edd       Zedd       Zd fdZd fdZ x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_stepc                    | d||d|}t        dt        |j                  j                        |      D ]=  }|j	                  |j                  j                  |||z    |j
                        |_        ? |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   chat_memoryr   r8   )rangelenrB   r)   r-   r<   )clsr   rB   r@   kwargsobjis          r,   from_messagesz'ConversationSummaryMemory.from_messagesf   sx    ( =c{=f=q#coo667H 	A00((Q-?@

CJ	
 
r.   c                    | j                   gS )z,Will always return list of memory variables.)r>   )r(   s    r,   memory_variablesz*ConversationSummaryMemory.memory_variables   s       r.   c                    | j                   r| j                  | j                        g}n| j                  }| j                  |iS )zReturn history buffer.)content)return_messagesr   r<   r>   )r(   inputsr<   s      r,   load_memory_variablesz/ConversationSummaryMemory.load_memory_variables   s?     33DKK3HIF[[F((r.   c                p    |d   j                   }ddh}|t        |      k7  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 .)input_variablesset
ValueError)rE   valuesprompt_variablesexpected_keysmsgs        r,   validate_prompt_input_variablesz9ConversationSummaryMemory.validate_prompt_input_variables   s[     "(+;;"K0C 011L#$$9-K  S/!r.   c                    t         |   ||       | j                  | j                  j                  dd | j
                        | _        y)z.Save context from this conversation to buffer.N)supersave_contextr-   rB   r)   r<   )r(   rO   outputs	__class__s      r,   r^   z&ConversationSummaryMemory.save_context   sA    VW-..%%bc*KK
r.   c                0    t         |           d| _        y)zClear memory contents.r;   N)r]   clearr<   )r(   r`   s    r,   rb   zConversationSummaryMemory.clear   s    r.   )
r   r   rB   r   r@   intrF   r   r2   r:   )r2   z	list[str])rO   dict[str, Any]r2   rd   )rV   dictr2   re   )rO   rd   r_   zdict[str, str]r2   None)r2   rf   )r3   r4   r5   r6   r<   r7   r>   classmethodrI   propertyrK   r   rP   r   rZ   r^   rb   __classcell__)r`   s   @r,   r:   r:   S   s     FCJ   ,
   
# 6 ! ! ) ) 
 

 r.   r:   N)
__future__r   typingr   langchain_core._apir   langchain_core.chat_historyr   langchain_core.language_modelsr   langchain_core.messagesr   r	   r
   langchain_core.promptsr   langchain_core.utilsr   pydanticr   typing_extensionsr   langchain_classic.chains.llmr   $langchain_classic.memory.chat_memoryr   langchain_classic.memory.promptr   r   r:   r8   r.   r,   <module>rw      s    "  * > < Q Q 5 )  & 1 ? : 
	d	5Si 5S5Sp 
	G	M MMr.   