§
    ™Štj8  ã                   ó¼   — d 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
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e¦  «        ¦   «         ZdS )z6Chain that carries on a conversation and calls an LLM.é    )Ú
deprecated)ÚBasePromptTemplate)Ú
ConfigDictÚFieldÚmodel_validator)ÚSelfÚoverride)Ú
BaseMemory)ÚPROMPT)ÚLLMChain)ÚConversationBufferMemoryz0.2.7zlangchain.agents.create_agentz2.0.0z{Build a conversational agent with `langchain.agents.create_agent` and persist message history via a LangGraph checkpointer.)ÚsinceÚalternativeÚremovalÚaddendumc                   ó  — 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edefd„¦   «         ¦   «         Zedee         fd„¦   «         Z ed¬¦  «        defd„¦   «         ZdS )Ú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`:

        ```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]


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

        chain = RunnableWithMessageHistory(model, 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:

        ```python
        from langchain_classic.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]


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

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

    Example:
        ```python
        from langchain_classic.chains import ConversationChain
        from langchain_openai import OpenAI

        conversation = ConversationChain(llm=OpenAI())
        ```
    )Údefault_factoryÚmemoryÚpromptÚinputÚ	input_keyÚresponseÚ
output_keyTÚforbid)Úarbitrary_types_allowedÚextraÚreturnc                 ó   — dS )NF© )Úclss    úh/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_classic/chains/conversation/base.pyÚis_lc_serializablez$ConversationChain.is_lc_serializable{   s	   € ð ˆuó    c                 ó   — | j         gS )z5Use this since so some prompt vars come from history.)r   )Úselfs    r"   Ú
input_keyszConversationChain.input_keys€   s   € ð ”ÐÐr$   Úafter)Úmodec                 óþ   — | j         j        }| j        }||v rd|› d|› d�}t          |¦  «        ‚| j        j        }g |¢|‘}t          |¦  «        t          |¦  «        k    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Ô2ˆØ”Nˆ	Ø˜Ð#Ð#ðM ð Mð MØðMð Mð Mð õ ˜S‘/”/Ð!Øœ;Ô6ÐØ1˜+Ð1 yÐ1ˆÝˆ}ÑÔ¥Ð%5Ñ!6Ô!6Ò6Ð6ðDØ#ðDð DØ/:ðDð Dà(ðDð Dð Dð õ
 ˜S‘/”/Ð!Øˆr$   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r
   Ú__annotations__r   r   r   r   Ústrr   r   Úmodel_configÚclassmethodr	   Úboolr#   ÚpropertyÚlistr'   r   r   r3   r    r$   r"   r   r      s'  € € € € € € ðTð Tðl ˜Ð/GÐHÑHÔH€FˆJÐHÐHÑHØØ!'€FÐÐ'Ð'Ñ'Ø-à€IˆsÐÐÑØ €J�Ð Ð Ñ à�:Ø $Øðñ ô €Lð
 Øð 4ð ð ð ñ „Xñ „[ðð ð ˜D œIð  ð  ð  ñ „Xð ð €_˜'Ð"Ñ"Ô"ð°ð ð ð ñ #Ô"ðð ð r$   r   N)r7   Úlangchain_core._apir   Úlangchain_core.promptsr   Úpydanticr   r   r   Útyping_extensionsr   r	   Úlangchain_classic.base_memoryr
   Ú,langchain_classic.chains.conversation.promptr   Úlangchain_classic.chains.llmr   Úlangchain_classic.memory.bufferr   r   r    r$   r"   ú<module>rG      s  ðØ <Ð <à *Ð *Ð *Ð *Ð *Ð *Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø DÐ DÐ DÐ DÐ DÐ Dð €Ø
Ø/Øð	@ðñ ô ðBð Bð Bð Bð B˜ñ Bô Bñô ðBð Bð Br$   