
    hW                    n   d Z ddlmZ ddlmZ ddl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)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                  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/eduruby.in/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_parser5   s    !!r)   c                "    t         j                  S )z Return Identifier of agent type.)r   ZERO_SHOT_REACT_DESCRIPTIONselfs    r*   _agent_typezZeroShotAgent._agent_type9   s     444r)   c                     y)z]Prefix to append the observation with.

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

        Returns:
            "Thought: "
        zThought:r(   r7   s    r*   
llm_prefixzZeroShotAgent.llm_prefixG   s     r)   Nc                   t        t        |            }dj                  |D cg c]  }|j                   c}      }|j	                  |      }| d| d| d| }	|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)
r2   toolsprefixsuffixformat_instructionsrA   tool_stringstoolr?   r@   s
             r*   create_promptzZeroShotAgent.create_promptP   s    0 /tE{;YYe<d		<=
188J8OXT,t4G3HVHU!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.
        )rH   rI   rJ   rA   )llmpromptcallback_manager)	llm_chainallowed_toolsr0   r(   )_validate_toolsrM   r   rD   r4   )r2   rO   rG   rQ   r0   rH   rI   rJ   rA   r3   rP   rR   rL   r?   _output_parsers                  r*   from_llm_and_toolsz ZeroShotAgent.from_llm_and_toolsp   s    6 	E""" 3+ # 
 -
	
 -22Ddii2
2&J#*H*H*J 
$(
 	
 	
 3s   A/c                    t        | j                  |       t        |      dk(  rd| j                   d}t        |      |D ])  }|j                  d|j
                   d}t        |       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descriptionrD   superrT   )r2   rG   msgrL   	__class__s       r*   rT   zZeroShotAgent._validate_tools   s    #CLL%8u:?#CLL>1VW  S/! 	&D'!$)) -= >  !o%	& 	&r)   )r3   r   returnr   )r^   r   )rG   Sequence[BaseTool]rH   r   rI   r   rJ   r   rA   Optional[list[str]]r^   r   )rO   r   rG   r_   rQ   zOptional[BaseCallbackManager]r0   zOptional[AgentOutputParser]rH   r   rI   r   rJ   r   rA   r`   r3   r   r^   r   )rG   r_   r^   None)r#   r$   r%   r&   r   r   r0   r'   classmethodr4   propertyr9   r;   r=   r   r   r   rM   rV   rT   __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.
        )rD   funcrZ   )agentrG   r(   )r   r    r!   r"   r.   rV   )r2   rO   chainsr3   crG   ri   s          r*   from_chainszMRKLChain.from_chains   sm    4 
  ]]XX00
 
 00e<6e6v66
s   3AN)rO   r   rj   zlist[ChainConfig]r3   r   r^   r   )r#   r$   r%   r&   rb   rl   r(   r)   r*   rf   rf      s?     177 "7 	7
 
7 7r)   rf   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   r.   rf   r(   r)   r*   <module>r~      s    U " $ 6 6 * 8 < 1 / ?  @ J J 2 @ L L > %*  %
D'E D'
D'N %
 7  7
 7r)   