
    @hW                    b   S r SSKJr  SSKJr  SSKJrJr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Jr  SS
KJr  SSKJr  SSKJr  SSKJrJrJr  SSKJ r   SSK!J"r"  SSK#J$r$J%r%J&r&  SSK'J(r(  SSK)J*r*   " S S\5      r+\" S\SS9 " S S\5      5       r,\" S\SS9 " S S\5      5       r-g)zOAttempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf.    )annotations)Sequence)AnyCallable
NamedTupleOptional)
deprecated)BaseCallbackManager)BaseLanguageModel)PromptTemplate)BaseToolTool)render_text_description)Field)AGENT_DEPRECATION_WARNING)AgentAgentExecutorAgentOutputParser)	AgentTypeMRKLOutputParser)FORMAT_INSTRUCTIONSPREFIXSUFFIX)validate_tools_single_input)LLMChainc                  8    \ rS rSr% SrS\S'   S\S'   S\S'   Srg	)
ChainConfig   zConfiguration for a chain to use in MRKL system.

Parameters:
    action_name: Name of the action.
    action: Action function to call.
    action_description: Description of the action.
straction_namer   actionaction_description N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r$       R/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/agents/mrkl/base.pyr   r      s     r,   r   z0.1.0z1.0)messageremovalc                  "  ^  \ rS rSr% Sr\" \S9rS\S'   \	SS j5       r
\SS j5       r\SS j5       r\SS	 j5       r\	\\\S
4           SS jj5       r\	S
S
\\\S
4                   SS jj5       r\	SU 4S jj5       rSrU =r$ )ZeroShotAgent'   zWAgent for the MRKL chain.

Parameters:
    output_parser: Output parser for the agent.
)default_factoryr   output_parserc                    [        5       $ )Nr   )clskwargss     r-   _get_default_output_parser(ZeroShotAgent._get_default_output_parser5   s    !!r,   c                "    [         R                  $ )z Return Identifier of agent type.)r   ZERO_SHOT_REACT_DESCRIPTIONselfs    r-   _agent_typeZeroShotAgent._agent_type9   s     444r,   c                    g)zEPrefix to append the observation with.

Returns:
    "Observation: "
zObservation: r$   r<   s    r-   observation_prefix ZeroShotAgent.observation_prefix>   s     r,   c                    g)z>Prefix to append the llm call with.

Returns:
    "Thought: "
zThought:r$   r<   s    r-   
llm_prefixZeroShotAgent.llm_prefixG   s     r,   Nc                   [        [        U5      5      nSR                  U Vs/ sH  owR                  PM     sn5      nUR	                  US9nU SU SU SU 3n	U(       a	  [        XS9$ [
        R                  " U	5      $ s  snf )aA  Create prompt in the style of the zero shot agent.

Args:
    tools: List of tools the agent will have access to, used to format the
        prompt.
    prefix: String to put before the list of tools. Defaults to PREFIX.
    suffix: String to put after the list of tools. Defaults to SUFFIX.
    format_instructions: Instructions on how to use the tools.
        Defaults to FORMAT_INSTRUCTIONS
    input_variables: List of input variables the final prompt will expect.
        Defaults to None.

Returns:
    A PromptTemplate with the template assembled from the pieces here.
z, )
tool_namesz

)templateinput_variables)r   listjoinnameformatr   from_template)
r6   toolsprefixsuffixformat_instructionsrI   tool_stringstoolrG   rH   s
             r-   create_promptZeroShotAgent.create_promptP   s    0 /tE{;YYe<ed		e<=
188J8OXT,t4G3HVHU!8UU++H55  =s   Bc	                    U R                  U5        U R                  UUUUUS9n
[        UU
US9nU Vs/ sH  oR                  PM     nnU=(       d    U R	                  5       nU " SUUUS.U	D6$ s  snf )a?  Construct an agent from an LLM and tools.

Args:
    llm: The LLM to use as the agent LLM.
    tools: The tools to use.
    callback_manager: The callback manager to use. Defaults to None.
    output_parser: The output parser to use. Defaults to None.
    prefix: The prefix to use. Defaults to PREFIX.
    suffix: The suffix to use. Defaults to SUFFIX.
    format_instructions: The format instructions to use.
        Defaults to FORMAT_INSTRUCTIONS.
    input_variables: The input variables to use. Defaults to None.
    kwargs: Additional parameters to pass to the agent.
)rP   rQ   rR   rI   )llmpromptcallback_manager)	llm_chainallowed_toolsr4   r$   )_validate_toolsrU   r   rL   r8   )r6   rX   rO   rZ   r4   rP   rQ   rR   rI   r7   rY   r[   rT   rG   _output_parsers                  r-   from_llm_and_tools ZeroShotAgent.from_llm_and_toolsp   s    6 	E""" 3+ # 
 -
	
 -22EDiiE
2&J#*H*H*J 
$(
 	
 	
 3s   A/c                  > [        U R                  U5        [        U5      S:X  a  SU R                   S3n[        U5      eU H+  nUR                  b  M  SUR
                   S3n[        U5      e   [        TU ]  U5        g )Nr   zGot no tools for z%. At least one tool must be provided.zGot a tool zN without a description. For this agent, a description must always be provided.)r   r%   len
ValueErrordescriptionrL   superr]   )r6   rO   msgrT   	__class__s       r-   r]   ZeroShotAgent._validate_tools   s    #CLL%8u:?#CLL>1VW  S/!D'!$)) -= >  !o%  	&r,   r$   )r7   r   returnr   )ri   r    )rO   Sequence[BaseTool]rP   r    rQ   r    rR   r    rI   Optional[list[str]]ri   r   )rX   r   rO   rj   rZ   zOptional[BaseCallbackManager]r4   zOptional[AgentOutputParser]rP   r    rQ   r    rR   r    rI   rk   r7   r   ri   r   )rO   rj   ri   None)r%   r&   r'   r(   r)   r   r   r4   r*   classmethodr8   propertyr>   rA   rD   r   r   r   rU   r_   r]   r+   __classcell__)rg   s   @r-   r1   r1   '   sd    (-=M'NM$N" " 5 5      #6/36!6 6 	6
 !6 -6 
6 6> 
 ;?59#6/3.
.
 ".
 8	.

 3.
 .
 .
 !.
 -.
 .
 
.
 .
` ' 'r,   r1   c                  <    \ rS rSrSr\        SS j5       rSrg)	MRKLChain   z&Chain that implements the MRKL system.c           	         U Vs/ sH,  n[        UR                  UR                  UR                  S9PM.     nn[        R                  X5      nU " SXeS.UD6$ s  snf )aH  User-friendly way to initialize the MRKL chain.

This is intended to be an easy way to get up and running with the
MRKL chain.

Args:
    llm: The LLM to use as the agent LLM.
    chains: The chains the MRKL system has access to.
    **kwargs: parameters to be passed to initialization.

Returns:
    An initialized MRKL chain.
)rL   funcrd   )agentrO   r$   )r   r!   r"   r#   r1   r_   )r6   rX   chainsr7   crO   ru   s          r-   from_chainsMRKLChain.from_chains   sn    4 
  ]]XX00
  	 
 00<66v66
s   2Ar$   N)rX   r   rv   zlist[ChainConfig]r7   r   ri   r   )r%   r&   r'   r(   r)   rm   rx   r+   r$   r,   r-   rq   rq      s?     177 "7 	7
 
7 7r,   rq   N).r)   
__future__r   collections.abcr   typingr   r   r   r   langchain_core._apir	   langchain_core.callbacksr
   langchain_core.language_modelsr   langchain_core.promptsr   langchain_core.toolsr   r   langchain_core.tools.renderr   pydanticr   langchain._api.deprecationr   langchain.agents.agentr   r   r   langchain.agents.agent_typesr   #langchain.agents.mrkl.output_parserr   langchain.agents.mrkl.promptr   r   r   langchain.agents.utilsr   langchain.chainsr   r   r1   rq   r$   r,   r-   <module>r      s    U " $ 6 6 * 8 < 1 / ?  @ J J 2 @ L L > %*  %
D'E D'
D'N %
 7  7
 7r,   