
    ih	                    j   d Z ddlmZ ddlmZmZmZmZ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mZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z*  G d de      Z+ eded       G d de             Z, eded       G d de             Z-y)zOAttempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf.    )annotations)AnyCallableList
NamedTupleOptionalSequence)
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                  0    e Zd ZU dZded<   ded<   ded<   y)ChainConfigzConfiguration 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_descriptionN)__name__
__module____qualname____doc____annotations__     X/var/www/html/dev/engine/venv/lib/python3.12/site-packages/langchain/agents/mrkl/base.pyr   r      s     r*   r   z0.1.0z1.0)messageremovalc                      e Zd ZU dZ ee      Zded<   edd       Z	e
dd       Ze
dd       Ze
dd       Zeeeed	f	 	 	 	 	 	 	 	 	 	 	 dd
       Zed	d	eeed	f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zed fd       Z xZS )ZeroShotAgentzcAgent for the MRKL chain.

    Parameters:
        output_parser: Output parser for the agent.
    )default_factoryr   output_parserc                    t               S )Nr   )clskwargss     r+   _get_default_output_parserz(ZeroShotAgent._get_default_output_parser4   s    !!r*   c                "    t         j                  S )z Return Identifier of agent type.)r   ZERO_SHOT_REACT_DESCRIPTIONselfs    r+   _agent_typezZeroShotAgent._agent_type8   s     444r*   c                     y)z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r)   r8   s    r+   observation_prefixz ZeroShotAgent.observation_prefix=   s     r*   c                     y)zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r)   r8   s    r+   
llm_prefixzZeroShotAgent.llm_prefixF   s     r*   Nc                   t        t        |            }dj                  |D cg c]  }|j                   c}      }|j	                  |      }dj                  ||||g      }	|rt        |	|      S t        j                  |	      S c c}w )a  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)
r3   toolsprefixsuffixformat_instructionsrB   tool_stringstoolr@   rA   s
             r+   create_promptzZeroShotAgent.create_promptO   s    0 /tE{;YYe<d		<=
188J8O;;6I6RS!8_UU++H55  =s   B	c	                    | j                  |       | j                  |||||      }
t        ||
|      }|D cg c]  }|j                   }}|xs | j	                         } | d|||d|	S c c}w )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.
        )rI   rJ   rK   rB   )llmpromptcallback_manager)	llm_chainallowed_toolsr1   r)   )_validate_toolsrN   r   rE   r5   )r3   rP   rH   rR   r1   rI   rJ   rK   rB   r4   rQ   rS   rM   r@   _output_parsers                  r+   from_llm_and_toolsz ZeroShotAgent.from_llm_and_toolso   s    6 	E""" 3+ # 
 -
	
 -22Ddii2
2&J#*H*H*J 
$(
 	
 	
 3s   A/c                    t        | j                  |       t        |      dk(  rt        d| j                   d      |D ]'  }|j                  t        d|j
                   d       t        |   |       y )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descriptionrE   superrU   )r3   rH   rM   	__class__s      r+   rU   zZeroShotAgent._validate_tools   s    #CLL%8u:?#CLL>1VW   	D' !$)) -= > 	 	&r*   )r4   r   returnr   )r^   r    )rH   Sequence[BaseTool]rI   r    rJ   r    rK   r    rB   Optional[List[str]]r^   r   )rP   r   rH   r_   rR   zOptional[BaseCallbackManager]r1   zOptional[AgentOutputParser]rI   r    rJ   r    rK   r    rB   r`   r4   r   r^   r   )rH   r_   r^   None)r$   r%   r&   r'   r   r   r1   r(   classmethodr5   propertyr:   r<   r>   r   r   r   rN   rW   rU   __classcell__)r]   s   @r+   r/   r/   &   sd    (-=M'NM$N" " 5 5      #6/36!6 6 	6
 !6 -6 
6 6> 
 ;?59#6/3.
.
 ".
 8	.

 3.
 .
 .
 !.
 -.
 .
 
.
 .
` ' 'r*   r/   c                  2    e Zd ZdZe	 	 	 	 	 	 	 	 dd       Zy)	MRKLChainz&Chain that implements the MRKL system.c                    |D cg c].  }t        |j                  |j                  |j                        0 }}t        j                  ||      } | d||d|S c c}w )a  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.
        )rE   funcr[   )agentrH   r)   )r   r!   r"   r#   r/   rW   )r3   rP   chainsr4   crH   ri   s          r+   from_chainszMRKLChain.from_chains   sm    . 
  ]]XX00
 
 00e<6e6v66
s   3AN)rP   r   rj   zList[ChainConfig]r4   r   r^   r   )r$   r%   r&   r'   rb   rl   r)   r*   r+   rf   rf      s9     17#7->7JM7	7 7r*   rf   N).r'   
__future__r   typingr   r   r   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   r/   rf   r)   r*   r+   <module>r}      s    U " F F * 8 < 1 / ?  @ J J 2 @ L L > %*  %
B'E B'
B'J %
7 7
7r*   