
    KKi                     N   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
  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K$J%r%  \" SSSS9 " S S\5      5       r&\4SS.S\S\\   S\S\S\'\(\)   -  S\4S  jjjr*g!)"    )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                      \ rS rSr% Sr\\   \S'    \\S'    \	\
S\\   4S j5       5       r\S\4S j5       r\S\4S j5       r\
 SS
\\\\4      S\S\S\\-  4S jj5       r\
 SS
\\\\4      S\S\S\\-  4S jj5       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:
    ```python
    from langchain_classic.agents import XMLAgent
    from langchain

    tools = ...
    model =

    ```
tools	llm_chainreturnc                     S/$ )Ninput )selfs    c/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain_classic/agents/xml/base.py
input_keysXMLAgent.input_keys.   s     y    c                  h    [         R                  " [        5      n U [        R                  " S5      -   $ )z,Return the default prompt for the XML agent.z{intermediate_steps})r   from_templater   r   )base_prompts    r'   get_default_promptXMLAgent.get_default_prompt3   s3     )667IJ4BB"
 
 	
r*   c                      [        5       $ )zReturn an XMLAgentOutputParser.r   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>)r3   r    questionstop)r4   )tool
tool_inputr    namedescriptionr!   
output_keyr&   r3   r4   r5   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r7   )r=   r>   r    r?   r@   r!   acallrA   rB   s              r'   aplanXMLAgent.aplanY   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%   )N)__name__
__module____qualname____firstlineno____doc__listr   __annotations__r   propertyr   strr(   staticmethodr   r.   r   r1   tupler   r   r   r   rH   rL   __static_attributes__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_rendererrZ   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.
    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    Tr:   )r<   c                     [        U S   5      $ )Nr3   r   )xs    r'   <lambda>"create_xml_agent.<locals>.<lambda>   s    z!4H2I'Jr*   )r_   )

differenceinput_variablesrS   partial_variables
ValueErrorpartialbindr   assignr   )	r[   r    r\   r]   rZ   missing_varsmsgr<   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   boolrS   rV   r   r%   r*   r'   <module>r      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*   