§
    ™Štjó  ã                  óš   — 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
 d dl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efddœdd„ZdS )é    )Úannotations)ÚSequence)ÚBaseLanguageModel)ÚBasePromptTemplate)ÚRunnableÚRunnablePassthrough)ÚBaseTool)ÚToolsRendererÚrender_text_description)ÚAgentOutputParser©Úformat_log_to_str)ÚReActSingleInputOutputParserNT)Ústop_sequenceÚllmr   ÚtoolsúSequence[BaseTool]Úpromptr   Úoutput_parserúAgentOutputParser | NoneÚtools_rendererr
   r   úbool | list[str]Úreturnr   c               óÊ  — h d£                      |j        t          |j        ¦  «        z   ¦  «        }|rd|› �}t	          |¦  «        ‚|                      |t          |¦  «        ¦  «        d                     d„ |D ¦   «         ¦  «        ¬¦  «        }|r |du rdgn|}|                      |¬¦  «        }	n| }	|pt          ¦   «         }t          j
        d	„ ¬
¦  «        |z  |	z  |z  S )a
  Create an agent that uses ReAct prompting.

    Based on paper "ReAct: Synergizing Reasoning and Acting in Language Models"
    (https://arxiv.org/abs/2210.03629)

    !!! warning

        This implementation is based on the foundational ReAct paper but is older and
        not well-suited for production applications.

        For a more robust and feature-rich implementation, we recommend using the
        `create_agent` function from the `langchain` library.

        See the
        [reference doc](https://reference.langchain.com/python/langchain/agents/)
        for more information.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use. See Prompt section below for more.
        output_parser: AgentOutputParser for parse the LLM output.
        tools_renderer: This controls how the tools are converted into a string and
            then passed into the LLM.
        stop_sequence: bool or list of str.
            If `True`, adds a stop token of "Observation:" to avoid hallucinates.
            If `False`, does not add a stop token.
            If a list of str, uses the provided list as the stop tokens.

            You may to set this to False if the LLM you are using
            does not support stop sequences.

    Returns:
        A Runnable sequence representing an agent. It takes as input all the same input
        variables as the prompt passed in does. It returns as output either an
        AgentAction or AgentFinish.

    Examples:
        ```python
        from langchain_classic import hub
        from langchain_openai import OpenAI
        from langchain_classic.agents import AgentExecutor, create_react_agent

        prompt = hub.pull("hwchase17/react")
        model = OpenAI()
        tools = ...

        agent = create_react_agent(model, tools, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=tools)

        agent_executor.invoke({"input": "hi"})

        # Use with chat history
        from langchain_core.messages import AIMessage, HumanMessage

        agent_executor.invoke(
            {
                "input": "what's my name?",
                # Notice that chat_history is a string
                # since this prompt is aimed at LLMs, not chat models
                "chat_history": "Human: My name is Bob\nAI: Hello Bob!",
            }
        )
        ```

    Prompt:

        The prompt must have input keys:
            * `tools`: contains descriptions and arguments for each tool.
            * `tool_names`: contains all tool names.
            * `agent_scratchpad`: contains previous agent actions and tool outputs as a
                string.

        Here's an example:

        ```python
        from langchain_core.prompts import PromptTemplate

        template = '''Answer the following questions as best you can. You have access to the following tools:

        {tools}

        Use the following format:

        Question: the input question you must answer
        Thought: you should always think about what to do
        Action: the action to take, should be one of [{tool_names}]
        Action Input: the input to the action
        Observation: the result of the action
        ... (this Thought/Action/Action Input/Observation can repeat N times)
        Thought: I now know the final answer
        Final Answer: the final answer to the original input question

        Begin!

        Question: {input}
        Thought:{agent_scratchpad}'''

        prompt = PromptTemplate.from_template(template)
        ```
    >   r   Ú
tool_namesÚagent_scratchpadz#Prompt missing required variables: z, c                ó   — g | ]	}|j         ‘Œ
S © )Úname)Ú.0Úts     úb/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_classic/agents/react/agent.pyú
<listcomp>z&create_react_agent.<locals>.<listcomp>‡   s   € Ð4Ð4Ð4¨˜aœfÐ4Ð4Ð4ó    )r   r   Tz
Observation)Ústopc                ó,   — t          | d         ¦  «        S )NÚintermediate_stepsr   )Úxs    r"   ú<lambda>z$create_react_agent.<locals>.<lambda>‘   s   € Õ'8¸Ð;OÔ9PÑ'QÔ'Q€ r$   )r   )Ú
differenceÚinput_variablesÚlistÚpartial_variablesÚ
ValueErrorÚpartialÚjoinÚbindr   r   Úassign)
r   r   r   r   r   r   Úmissing_varsÚmsgr%   Úllm_with_stops
             r"   Úcreate_react_agentr6      s,  € ð\ ?Ð>Ð>×IÒIØÔ¥ fÔ&>Ñ!?Ô!?Ñ?ñô €Lð ð ØB°LÐBÐBˆÝ˜‰oŒoÐà�^Š^Øˆn�T %™[œ[Ñ)Ô)Ø—9’9Ð4Ð4¨eÐ4Ñ4Ô4Ñ5Ô5ð ñ ô €Fð ð Ø$1°TÐ$9Ð$9�Ð Ð ¸}ˆØŸš d˜Ñ+Ô+ˆˆàˆØ!ÐCÕ%AÑ%CÔ%C€MåÔ"ØQÐQð	
ñ 	
ô 	
ð ñ	ð ñ		ð
 ñ	ðr$   )r   r   r   r   r   r   r   r   r   r
   r   r   r   r   )Ú
__future__r   Úcollections.abcr   Úlangchain_core.language_modelsr   Úlangchain_core.promptsr   Úlangchain_core.runnablesr   r   Úlangchain_core.toolsr	   Úlangchain_core.tools.renderr
   r   Úlangchain_classic.agentsr   Ú*langchain_classic.agents.format_scratchpadr   Ú'langchain_classic.agents.output_parsersr   r6   r   r$   r"   ú<module>rA      s  ðØ "Ð "Ð "Ð "Ð "Ð "à $Ð $Ð $Ð $Ð $Ð $à <Ð <Ð <Ð <Ð <Ð <Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ )Ð )Ð )Ð )Ð )Ð )Ø NÐ NÐ NÐ NÐ NÐ NÐ NÐ Nà 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø HÐ HÐ HÐ HÐ HÐ HØ PÐ PÐ PÐ PÐ PÐ Pð /3Ø$;ðFð '+ðFð Fð Fð Fð Fð Fð Fð Fr$   