
    @h                     J   S SK Jr  S SKJrJr  S SKJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJrJr  S S	KJrJr  S S
KJr  S SKJrJr  S SKJr  S SKJr  S SKJ r   S SK!J"r"  S SK#J$r$  \" SSSS9 " S S\5      5       r%\4SS.S\S\\   S\S\S\\&\'\(   4   S\4S jjjr)g )!    )Sequence)AnyUnion)
deprecated)AgentActionAgentFinish)	Callbacks)BaseLanguageModel)BasePromptTemplate)AIMessagePromptTemplateChatPromptTemplate)RunnableRunnablePassthrough)BaseTool)ToolsRendererrender_text_description)BaseSingleActionAgent
format_xmlXMLAgentOutputParser)agent_instructions)LLMChainz0.1.0create_xml_agentz1.0)alternativeremovalc                       \ rS rSr% Sr\\   \S'    \\S'    \	S\\
   4S j5       r\S\4S j5       r\S\4S j5       r SS
\\\\
4      S\S\S\\\4   4S jjr SS
\\\\
4      S\S\S\\\4   4S jjrSrg	)XMLAgent   a'  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:

    .. code-block:: python

        from langchain.agents import XMLAgent
        from langchain

        tools = ...
        model =


tools	llm_chainreturnc                     S/$ )Ninput )selfs    Q/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/agents/xml/base.py
input_keysXMLAgent.input_keys/   s
    y    c                  h    [         R                  " [        5      n U [        R                  " S5      -   $ )Nz{intermediate_steps})r   from_templater   r   )base_prompts    r'   get_default_promptXMLAgent.get_default_prompt3   s1    (667IJ4BB"
 
 	
r*   c                      [        5       $ Nr   r%   r*   r'   get_default_output_parser"XMLAgent.get_default_output_parser:   s    #%%r*   Nintermediate_steps	callbackskwargsc           	      .   SnU H(  u  pVUSUR                    SUR                   SU S3-  nM*     SnU R                   H!  nXxR                   SUR                   S3-  nM#     UUUS   S	S
/S.n	U R                  XS9n
XR
                  R                     $ N z<tool>z</tool><tool_input>z</tool_input><observation>z</observation>z: 
r$   </tool_input>z</final_answer>)r4   r    questionstop)r5   )tool
tool_inputr    namedescriptionr!   
output_keyr&   r4   r5   r6   logactionobservationr    r>   inputsresponses              r'   planXMLAgent.plan>   s     #5F%89J9J8K,[MIC $6
 JJD		{"T%5%5$6b99E  #&w$&78	
 >>&>>1122r*   c           	      ^  #    SnU H(  u  pVUSUR                    SUR                   SU S3-  nM*     SnU R                   H!  nXxR                   SUR                   S3-  nM#     UUUS   S	S
/S.n	U R
                  R                  XS9I S h  vN n
XR
                  R                     $  N7fr8   )r>   r?   r    r@   rA   r!   acallrB   rC   s              r'   aplanXMLAgent.aplanV   s      #5F%89J9J8K,[MIC $6
 JJD		{"T%5%5$6b99E  #&w$&78	
 --f-JJ1122 Ks   BB-B+B-r%   r1   )__name__
__module____qualname____firstlineno____doc__listr   __annotations__r   propertystrr(   staticmethodr   r.   r   r2   tupler   r	   r   r   r   rI   rM   __static_attributes__r%   r*   r'   r   r      s   & >1)DI   
 2 
 
 &'; & &  $3 {C'7!893 3 	3
 
{K'	(36  $3 {C'7!893 3 	3
 
{K'	(3 3r*   r   T)stop_sequencellmr    prompttools_rendererr[   r"   c                d   SS1R                  UR                  [        UR                  5      -   5      nU(       a  SU 3n[	        U5      eUR                  U" [        U5      5      S9nU(       a  USL a  S/OUnU R                  US9nOU n[        R                  " S S	9U-  U-  [        5       -  $ )
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. Default is `render_text_description`.
    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.

        Default is True. 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:

    .. code-block:: python

        from langchain import hub
        from langchain_community.chat_models import ChatAnthropic
        from langchain.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:

    .. code-block:: 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    Tr;   )r=   c                     [        U S   5      $ )Nr4   r   )xs    r'   <lambda>"create_xml_agent.<locals>.<lambda>   s    z!4H2I'Jr*   )r`   )

differenceinput_variablesrT   partial_variables
ValueErrorpartialbindr   assignr   )	r\   r    r]   r^   r[   missing_varsmsgr=   llm_with_stops	            r'   r   r   o   s    @ /0;;f&>&>!??L 3L>Bo^^T%[)  F $1T$9 }d+ 	""J	
 	 		
 
 	!r*   N)*collections.abcr   typingr   r   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   langchain.agents.agentr   "langchain.agents.format_scratchpadr   langchain.agents.output_parsersr   langchain.agents.xml.promptr   langchain.chains.llmr   r   boolrT   rW   r   r%   r*   r'   <module>r      s    $  * : . < : S B ) N 8 9 @ : ) G!3UCV3$ V3 DV3z %<	x -1x	xHx x "	x tCy)x xr*   