Ë
    ´Œj<  ã                   ó¢   — d 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  ed
dd¬«       G d„ de«      «       Zy)z6Chain that carries on a conversation and calls an LLM.é    )Ú
deprecated)Ú
BaseMemory)ÚBasePromptTemplate)Ú
ConfigDictÚFieldÚmodel_validator)ÚSelf)ÚPROMPT)ÚLLMChain)ÚConversationBufferMemoryz0.2.7z;langchain_core.runnables.history.RunnableWithMessageHistoryz1.0)ÚsinceÚalternativeÚremovalc                   óÆ   — e Zd ZU dZ ee¬«      Zeed<   	 e	Z
eed<   	 dZeed<   dZeed<    ed	d
¬«      Zedefd„«       Zedee   fd„«       Z ed¬«      defd„«       Zy)ÚConversationChaina}  Chain to have a conversation and load context from memory.

    This class is deprecated in favor of ``RunnableWithMessageHistory``. Please refer
    to this tutorial for more detail: https://python.langchain.com/docs/tutorials/chatbot/

    ``RunnableWithMessageHistory`` offers several benefits, including:

    - Stream, batch, and async support;
    - More flexible memory handling, including the ability to manage memory
      outside the chain;
    - Support for multiple threads.

    Below is a minimal implementation, analogous to using ``ConversationChain`` with
    the default ``ConversationBufferMemory``:

        .. code-block:: python

            from langchain_core.chat_history import InMemoryChatMessageHistory
            from langchain_core.runnables.history import RunnableWithMessageHistory
            from langchain_openai import ChatOpenAI


            store = {}  # memory is maintained outside the chain

            def get_session_history(session_id: str) -> InMemoryChatMessageHistory:
                if session_id not in store:
                    store[session_id] = InMemoryChatMessageHistory()
                return store[session_id]

            llm = ChatOpenAI(model="gpt-3.5-turbo-0125")

            chain = RunnableWithMessageHistory(llm, get_session_history)
            chain.invoke(
                "Hi I'm Bob.",
                config={"configurable": {"session_id": "1"}},
            )  # session_id determines thread
    Memory objects can also be incorporated into the ``get_session_history`` callable:

        .. code-block:: python

            from langchain.memory import ConversationBufferWindowMemory
            from langchain_core.chat_history import InMemoryChatMessageHistory
            from langchain_core.runnables.history import RunnableWithMessageHistory
            from langchain_openai import ChatOpenAI


            store = {}  # memory is maintained outside the chain

            def get_session_history(session_id: str) -> InMemoryChatMessageHistory:
                if session_id not in store:
                    store[session_id] = InMemoryChatMessageHistory()
                    return store[session_id]

                memory = ConversationBufferWindowMemory(
                    chat_memory=store[session_id],
                    k=3,
                    return_messages=True,
                )
                assert len(memory.memory_variables) == 1
                key = memory.memory_variables[0]
                messages = memory.load_memory_variables({})[key]
                store[session_id] = InMemoryChatMessageHistory(messages=messages)
                return store[session_id]

            llm = ChatOpenAI(model="gpt-3.5-turbo-0125")

            chain = RunnableWithMessageHistory(llm, get_session_history)
            chain.invoke(
                "Hi I'm Bob.",
                config={"configurable": {"session_id": "1"}},
            )  # session_id determines thread

    Example:
        .. code-block:: python

            from langchain.chains import ConversationChain
            from langchain_community.llms import OpenAI

            conversation = ConversationChain(llm=OpenAI())
    )Údefault_factoryÚmemoryÚpromptÚinputÚ	input_keyÚresponseÚ
output_keyTÚforbid)Úarbitrary_types_allowedÚextraÚreturnc                  ó   — y)NF© )Úclss    úl/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain/chains/conversation/base.pyÚis_lc_serializablez$ConversationChain.is_lc_serializabler   s   € àó    c                 ó   — | j                   gS )z5Use this since so some prompt vars come from history.)r   )Úselfs    r    Ú
input_keyszConversationChain.input_keysv   s   € ð —‘ÐÐr"   Úafter)Úmodec                 ó  — | j                   j                  }| j                  }||v rd|› d|› d�}t        |«      ‚| j                  j
                  }g |¢|‘}t        |«      t        |«      k7  rd|› d|› d|› d�}t        |«      ‚| S )z4Validate that prompt input variables are consistent.zThe input key z$ was also found in the memory keys (z+) - please provide keys that don't overlap.z:Got unexpected prompt input variables. The prompt expects z
, but got z as inputs from memory, and z as the normal input key.)r   Úmemory_variablesr   Ú
ValueErrorr   Úinput_variablesÚset)r$   Úmemory_keysr   ÚmsgÚprompt_variablesÚexpected_keyss         r    Úvalidate_prompt_input_variablesz1ConversationChain.validate_prompt_input_variables{   s¹   € ð —k‘k×2Ñ2ˆØ—N‘Nˆ	Ø˜Ñ#à   ð ,Ø�=Ð KðMð ô ˜S“/Ð!ØŸ;™;×6Ñ6ÐØ1˜+Ð1 yÐ1ˆÜˆ}Ó¤Ð%5Ó!6Ò6àLØ#Ð$ J¨{¨mð <Ø(˜kÐ)BðDð ô
 ˜S“/Ð!Øˆr"   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Ú__annotations__r
   r   r   r   Ústrr   r   Úmodel_configÚclassmethodÚboolr!   ÚpropertyÚlistr%   r   r	   r1   r   r"   r    r   r      sª   … ñOñb Ð/GÔH€FˆJÓHØØ!'€FÐÓ'Ø-à€IˆsÓØ €J�Ó áØ $Øô€Lð
 ð 4ò ó ðð ð ˜D ™Iò  ó ð ñ ˜'Ô"ð°ò ó #ñr"   r   N)r5   Úlangchain_core._apir   Úlangchain_core.memoryr   Úlangchain_core.promptsr   Úpydanticr   r   r   Útyping_extensionsr	   Ú$langchain.chains.conversation.promptr
   Úlangchain.chains.llmr   Úlangchain.memory.bufferr   r   r   r"   r    Ú<module>rE      sO   ðÙ <å *Ý ,Ý 5ß 7Ñ 7Ý "å 7Ý )Ý <ñ Ø
ØMØôô
|˜ó |óñ
|r"   