
    m
i                       d Z ddlmZ ddlmZ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 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 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.dS )zOAttempt to implement MRKL systems as described in arxiv.org/pdf/2205.00445.pdf.    )annotations)CallableSequence)Any
NamedTuple)
deprecated)BaseCallbackManager)BaseLanguageModel)PromptTemplate)BaseToolTool)render_text_description)Field)override)AGENT_DEPRECATION_WARNING)AgentAgentExecutorAgentOutputParser)	AgentTypeMRKLOutputParser)FORMAT_INSTRUCTIONSPREFIXSUFFIX)validate_tools_single_input)LLMChainc                  2    e Zd ZU dZded<   ded<   ded<   dS )ChainConfigzConfiguration for a chain to use in MRKL system.

    Args:
        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__     C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langchain_classic/agents/mrkl/base.pyr   r      sB           r)   r   z0.1.0z1.0)messageremovalc                      e Zd ZU dZ ee          Zded<   e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 )%ZeroShotAgentz]Agent for the MRKL chain.

    Args:
        output_parser: Output parser for the agent.
    )default_factoryr   output_parserkwargsr   returnc                    t                      S )Nr   )clsr1   s     r*   _get_default_output_parserz(ZeroShotAgent._get_default_output_parser6   s      !!!r)   r   c                    t           j        S )z Return Identifier of agent type.)r   ZERO_SHOT_REACT_DESCRIPTIONselfs    r*   _agent_typezZeroShotAgent._agent_type;   s     44r)   c                    dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r(   r8   s    r*   observation_prefixz ZeroShotAgent.observation_prefix@   s	     r)   c                    dS )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r(   r8   s    r*   
llm_prefixzZeroShotAgent.llm_prefixI   s	     zr)   NtoolsSequence[BaseTool]prefixsuffixformat_instructionsinput_variableslist[str] | Noner   c                   t          t          |                    }d                    d |D                       }|                    |          }| d| d| d| }|rt	          ||          S t	          j        |          S )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.
            suffix: String to put after the list of tools.
            format_instructions: Instructions on how to use the tools.
            input_variables: List of input variables the final prompt will expect.


        Returns:
            A PromptTemplate with the template assembled from the pieces here.
        z, c                    g | ]	}|j         
S r(   name.0tools     r*   
<listcomp>z/ZeroShotAgent.create_prompt.<locals>.<listcomp>j   s    <<<d	<<<r)   )
tool_namesz

)templaterD   )r   listjoinformatr   from_template)	r4   r?   rA   rB   rC   rD   tool_stringsrN   rO   s	            r*   create_promptzZeroShotAgent.create_promptR   s    . /tE{{;;YY<<e<<<==
188J8OOUU,UU4GUUVUU 	V!8_UUUU+H555r)   llmr
   callback_managerBaseCallbackManager | NoneAgentOutputParser | Noner   c	                    |                      |           |                     |||||          }
t          ||
|          }d |D             }|p|                                 } | d|||d|	S )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.
            output_parser: The output parser to use.
            prefix: The prefix to use.
            suffix: The suffix to use.
            format_instructions: The format instructions to use.
            input_variables: The input variables to use.
            kwargs: Additional parameters to pass to the agent.
        )rA   rB   rC   rD   )rV   promptrW   c                    g | ]	}|j         
S r(   rH   rJ   s     r*   rM   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>   s    222Ddi222r)   )	llm_chainallowed_toolsr0   r(   )_validate_toolsrU   r   r5   )r4   rV   r?   rW   r0   rA   rB   rC   rD   r1   r[   r]   rN   _output_parsers                 r*   from_llm_and_toolsz ZeroShotAgent.from_llm_and_toolsq   s    4 	E""""" 3+ # 
 
 -
 
 
	
 32E222
&J#*H*H*J*Js 
$(
 
 	
 
 	
r)   Nonec                   t          | j        |           t          |          dk    rd| j         d}t          |          |D ]#}|j        d|j         d}t          |          $t                                          |           d S )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descriptionrI   superr_   )r4   r?   msgrL   	__class__s       r*   r_   zZeroShotAgent._validate_tools   s    #CL%888u::??WCLWWW  S//! 	& 	&D'>$) > > >  !oo% ( 	&&&&&r)   )r1   r   r2   r   )r2   r   )r?   r@   rA   r   rB   r   rC   r   rD   rE   r2   r   )rV   r
   r?   r@   rW   rX   r0   rY   rA   r   rB   r   rC   r   rD   rE   r1   r   r2   r   )r?   r@   r2   rb   )r#   r$   r%   r&   r   r   r0   r'   classmethodr   r5   propertyr:   r<   r>   r   r   r   rU   ra   r_   __classcell__)ri   s   @r*   r.   r.   (   sk          (-u=M'N'N'NMNNNN" " " X [" 5 5 5 X5    X    X  #6,06 6 6 6 [6< 
 8<26#6,0-
 -
 -
 -
 [-
^ ' ' ' ' ' [' ' ' ' 'r)   r.   c                  *    e Zd ZdZedd
            ZdS )	MRKLChainz&Chain that implements the MRKL system.rV   r
   chainslist[ChainConfig]r1   r   r2   r   c                f    d |D             }t                               ||          } | d||d|S )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.
        c                P    g | ]#}t          |j        |j        |j                   $S ))rI   funcrf   )r   r    r!   r"   )rK   cs     r*   rM   z)MRKLChain.from_chains.<locals>.<listcomp>   sI     
 
 
  ]X0  
 
 
r)   )agentr?   r(   )r.   ra   )r4   rV   ro   r1   r?   ru   s         r*   from_chainszMRKLChain.from_chains   sV    (
 
 
 
 
 00e<<s6e66v666r)   N)rV   r
   ro   rp   r1   r   r2   r   )r#   r$   r%   r&   rj   rv   r(   r)   r*   rn   rn      s:         107 7 7 [7 7 7r)   rn   N)/r&   
__future__r   collections.abcr   r   typingr   r   langchain_core._apir   langchain_core.callbacksr	   langchain_core.language_modelsr
   langchain_core.promptsr   langchain_core.toolsr   r   langchain_core.tools.renderr   pydanticr   typing_extensionsr   "langchain_classic._api.deprecationr   langchain_classic.agents.agentr   r   r   $langchain_classic.agents.agent_typesr   +langchain_classic.agents.mrkl.output_parserr   $langchain_classic.agents.mrkl.promptr   r   r   langchain_classic.agents.utilsr   langchain_classic.chainsr   r   r.   rn   r(   r)   r*   <module>r      s\   U U " " " " " " . . . . . . . . " " " " " " " " * * * * * * 8 8 8 8 8 8 < < < < < < 1 1 1 1 1 1 / / / / / / / / ? ? ? ? ? ?       & & & & & & H H H H H H R R R R R R R R R R : : : : : : H H H H H H T T T T T T T T T T F F F F F F - - - - - -    *    %  
C' C' C' C' C'E C' C' 
C'L %  
 7  7  7  7  7  7  7 
 7  7  7r)   