§
    ™Štjç  ã                   ón  — 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 d dlmZ d dlmZ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 dl m!Z! d dl"m#Z# d dl$m%Z%  eddd¬¦  «         G d„ de¦  «        ¦   «         Z&efddœdedee         dedede'e(e)         z  defd „Z*d!S )"é    )ÚSequence)ÚAny)Ú
deprecated)ÚAgentActionÚAgentFinish)Ú	Callbacks)ÚBaseLanguageModel)ÚBasePromptTemplate)ÚAIMessagePromptTemplateÚChatPromptTemplate)ÚRunnableÚRunnablePassthrough)ÚBaseTool)ÚToolsRendererÚrender_text_description)Úoverride)ÚBaseSingleActionAgent©Ú
format_xml©ÚXMLAgentOutputParser)Úagent_instructions)ÚLLMChainz0.1.0Úcreate_xml_agentz2.0.0)ÚalternativeÚremovalc                   óD  — e Zd ZU dZee         ed<   	 eed<   	 ee	dee
         fd„¦   «         ¦   «         Zedefd„¦   «         Zedefd„¦   «         Ze		 dd	eeee
f                  d
ededeez  fd„¦   «         Ze		 dd	eeee
f                  d
ededeez  fd„¦   «         ZdS )ÚXMLAgentaC  Agent that uses XML tags.

    Args:
        tools: list of tools the agent can choose from
        llm_chain: The LLMChain to call to predict the next action

    Examples:
        ```python
        from langchain_classic.agents import XMLAgent
        from langchain

        tools = ...
        model =

        ```
    ÚtoolsÚ	llm_chainÚreturnc                 ó   — dgS )NÚinput© )Úselfs    ú_/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_classic/agents/xml/base.pyÚ
input_keyszXMLAgent.input_keys.   s   € ð ˆyÐó    c                  ób   — t          j        t          ¦  «        } | t          j        d¦  «        z   S )z,Return the default prompt for the XML agent.z{intermediate_steps})r   Úfrom_templater   r   )Úbase_prompts    r&   Úget_default_promptzXMLAgent.get_default_prompt3   s5   € õ )Ô6Õ7IÑJÔJˆØÕ4ÔBØ"ñ
ô 
ñ 
ð 	
r(   c                  ó   — t          ¦   «         S )zReturn an XMLAgentOutputParser.r   r$   r(   r&   Úget_default_output_parserz"XMLAgent.get_default_output_parser;   s   € õ $Ñ%Ô%Ð%r(   NÚintermediate_stepsÚ	callbacksÚkwargsc           	      óú   — d}|D ]\  }}|d|j         › d|j        › d|› d�z  }Œd}| j        D ]}||j        › d|j        › d�z  }Œ|||d         d	d
gdœ}	|                      |	|¬¦  «        }
|
| j        j                 S ©NÚ z<tool>z</tool><tool_input>z</tool_input><observation>z</observation>z: ú
r#   ú</tool_input>z</final_answer>)r/   r   ÚquestionÚstop)r0   )ÚtoolÚ
tool_inputr   ÚnameÚdescriptionr    Ú
output_key©r%   r/   r0   r1   ÚlogÚactionÚobservationr   r9   ÚinputsÚresponses              r&   ÚplanzXMLAgent.plan@   sè   € ð ˆØ#5ð 	ð 	ÑˆF�KØðI˜œð Ið I¸Ô9Jð Ið IØ-8ðIð Ið IñˆCˆCð ˆØ”Jð 	:ð 	:ˆDØ˜œ	Ð9Ð9 TÔ%5Ð9Ð9Ð9Ñ9ˆEˆEà"%ØØ˜wœØ$Ð&7Ð8ð	
ð 
ˆð —>’> &°I�>Ñ>Ô>ˆØ˜œÔ1Ô2Ð2r(   c           	   ‹   ó  K  — d}|D ]\  }}|d|j         › d|j        › d|› d�z  }Œd}| j        D ]}||j        › d|j        › d�z  }Œ|||d         d	d
gdœ}	| j                             |	|¬¦  «        ƒ d {V —†}
|
| j        j                 S r3   )r9   r:   r   r;   r<   r    Úacallr=   r>   s              r&   ÚaplanzXMLAgent.aplanY   s  è è € ð ˆØ#5ð 	ð 	ÑˆF�KØðI˜œð Ið I¸Ô9Jð Ið IØ-8ðIð Ið IñˆCˆCð ˆØ”Jð 	:ð 	:ˆDØ˜œ	Ð9Ð9 TÔ%5Ð9Ð9Ð9Ñ9ˆEˆEà"%ØØ˜wœØ$Ð&7Ð8ð	
ð 
ˆð œ×-Ò-¨fÀ	Ð-ÑJÔJÐJÐJÐJÐJÐJÐJˆØ˜œÔ1Ô2Ð2r(   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úlistr   Ú__annotations__r   Úpropertyr   Ústrr'   Ústaticmethodr   r,   r   r.   Útupler   r   r   r   rD   rG   r$   r(   r&   r   r      s�  € € € € € € ðð ð" �Œ>ÐÐÑØ1ØÐÐÑØ)àØð˜D œIð ð ð ñ „Xñ „Xðð ð
Ð 2ð 
ð 
ð 
ñ „\ð
ð ð&Ð';ð &ð &ð &ñ „\ð&ð ð  $ð3ð 3à   {°CÐ'7Ô!8Ô9ð3ð ð3ð ð	3ð
 
�{Ñ	"ð3ð 3ð 3ñ „Xð3ð0 ð  $ð3ð 3à   {°CÐ'7Ô!8Ô9ð3ð ð3ð ð	3ð
 
�{Ñ	"ð3ð 3ð 3ñ „Xð3ð 3ð 3r(   r   T)Ústop_sequenceÚllmr   ÚpromptÚtools_rendererrR   r!   c                ó†  — ddh                      |j        t          |j        ¦  «        z   ¦  «        }|rd|› �}t	          |¦  «        ‚|                      |t          |¦  «        ¦  «        ¬¦  «        }|r |du rdgn|}|                      |¬¦  «        }n| }t          j        d„ ¬	¦  «        |z  |z  t          ¦   «         z  S )
aÐ  Create an agent that uses XML to format its logic.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use, must have input keys
            `tools`: contains descriptions for each tool.
            `agent_scratchpad`: contains previous agent actions and tool outputs.
        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 "</tool_input>" 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.

    Example:
        ```python
        from langchain_classic import hub
        from langchain_anthropic import ChatAnthropic
        from langchain_classic.agents import AgentExecutor, create_xml_agent

        prompt = hub.pull("hwchase17/xml-agent-convo")
        model = ChatAnthropic(model="claude-3-haiku-20240307")
        tools = ...

        agent = create_xml_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 for each tool.
            * `agent_scratchpad`: contains previous agent actions and tool outputs as
              an XML string.

        Here's an example:

        ```python
        from langchain_core.prompts import PromptTemplate

        template = '''You are a helpful assistant. Help the user answer any questions.

        You have access to the following tools:

        {tools}

        In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>
        For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:

        <tool>search</tool><tool_input>weather in SF</tool_input>
        <observation>64 degrees</observation>

        When you are done, respond with a final answer between <final_answer></final_answer>. For example:

        <final_answer>The weather in SF is 64 degrees</final_answer>

        Begin!

        Previous Conversation:
        {chat_history}

        Question: {input}
        {agent_scratchpad}'''
        prompt = PromptTemplate.from_template(template)
        ```
    r   Úagent_scratchpadz#Prompt missing required variables: )r   Tr6   )r8   c                 ó,   — t          | d         ¦  «        S )Nr/   r   )Úxs    r&   ú<lambda>z"create_xml_agent.<locals>.<lambda>ç   s   € ¥z°!Ð4HÔ2IÑ'JÔ'J€ r(   )rW   )
Ú
differenceÚinput_variablesrL   Úpartial_variablesÚ
ValueErrorÚpartialÚbindr   Úassignr   )	rS   r   rT   rU   rR   Úmissing_varsÚmsgr8   Úllm_with_stops	            r&   r   r   s   s  € ðB Ð/Ð0×;Ò;ØÔ¥ fÔ&>Ñ!?Ô!?Ñ?ñô €Lð ð ØB°LÐBÐBˆÝ˜‰oŒoÐà�^Š^Øˆn�T %™[œ[Ñ)Ô)ð ñ ô €Fð ð Ø$1°TÐ$9Ð$9�Ð Ð ¸}ˆØŸš d˜Ñ+Ô+ˆˆàˆõ 	Ô"ØJÐJð	
ñ 	
ô 	
ð ñ	ð ñ		õ
 Ñ
 Ô
 ñ	!ðr(   N)+Úcollections.abcr   Útypingr   Úlangchain_core._apir   Úlangchain_core.agentsr   r   Úlangchain_core.callbacksr   Úlangchain_core.language_modelsr	   Úlangchain_core.prompts.baser
   Úlangchain_core.prompts.chatr   r   Úlangchain_core.runnablesr   r   Úlangchain_core.toolsr   Úlangchain_core.tools.renderr   r   Útyping_extensionsr   Úlangchain_classic.agents.agentr   Ú*langchain_classic.agents.format_scratchpadr   Ú'langchain_classic.agents.output_parsersr   Ú#langchain_classic.agents.xml.promptr   Úlangchain_classic.chains.llmr   r   ÚboolrL   rO   r   r$   r(   r&   ú<module>rw      s/  ðØ $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð à *Ð *Ð *Ð *Ð *Ð *Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø .Ð .Ð .Ð .Ð .Ð .Ø <Ð <Ð <Ð <Ð <Ð <Ø :Ð :Ð :Ð :Ð :Ð :Ø SÐ SÐ SÐ SÐ SÐ SÐ SÐ SØ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ )Ð )Ð )Ð )Ð )Ð )Ø NÐ NÐ NÐ NÐ NÐ NÐ NÐ NØ &Ð &Ð &Ð &Ð &Ð &à @Ð @Ð @Ð @Ð @Ð @Ø AÐ AÐ AÐ AÐ AÐ AØ HÐ HÐ HÐ HÐ HÐ HØ BÐ BÐ BÐ BÐ BÐ BØ 1Ð 1Ð 1Ð 1Ð 1Ð 1ð €ˆGÐ!3¸WÐEÑEÔEðY3ð Y3ð Y3ð Y3ð Y3Ð$ñ Y3ô Y3ñ FÔEðY3ð@ %<ð	yð '+ðyð yð yØ	ðyà�HÔðyð ðyð "ð	yð ˜$˜sœ)Ñ#ðyð ðyð yð yð yð yð yr(   