
    3fi                     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*y!)"    )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1.0)alternativeremovalc                       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y)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/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/agents/xml/base.py
input_keyszXMLAgent.input_keys.   s     y    c                  d    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   s3     )667IJ4B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           	      4   d}|D ](  \  }}|d|j                    d|j                   d| dz  }* d}| j                  D ]!  }||j                   d|j                   dz  }# |||d   d	d
gd}	| j                  |	|      }
|
| 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 	FK%89J9J8K,[MIC	
 JJ 	:D		{"T%5%5$6b99E	: #&w$&78	
 >>&I>>1122r(   c           	      d  K   d}|D ](  \  }}|d|j                    d|j                   d| dz  }* d}| j                  D ]!  }||j                   d|j                   dz  }# |||d   d	d
gd}	| j
                  j                  |	|       d {   }
|
| j
                  j                     S 7 wr3   )r9   r:   r   r;   r<   r    acallr=   r>   s              r&   aplanzXMLAgent.aplanY   s      #5 	FK%89J9J8K,[MIC	
 JJ 	:D		{"T%5%5$6b99E	: #&w$&78	
 --f	-JJ1122 Ks   BB0B.B0)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    
 2 
 
 &'; & &   $3 {C'7!893 3 	3
 
{	"3 30   $3 {C'7!893 3 	3
 
{	"3 3r(   r   T)stop_sequencellmr   prompttools_rendererrR   r!   c                ^   ddhj                  |j                  t        |j                        z         }|rd| }t	        |      |j                   |t        |                  }|r|du rdgn|}| j                  |      }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!4H2I'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 3L>Bo^^T%[)  F $1T$9 }d+ 	""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 B ) N & @ A H B 1 G!3UCY3$ Y3 DY3@ %<	y '+y	yHy y "	y $s)#y yr(   