
    Ng	                       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-dS )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                  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.

    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__     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/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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_parserkwargsr   returnc                    t                      S )Nr   )clsr2   s     r+   _get_default_output_parserz(ZeroShotAgent._get_default_output_parser4   s    !!!r*   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4r*   c                    dS )z]Prefix to append the observation with.

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

        Returns:
            "Thought: "
        zThought:r)   r9   s    r+   
llm_prefixzZeroShotAgent.llm_prefixF   s	     zr*   NtoolsSequence[BaseTool]prefixsuffixformat_instructionsinput_variablesOptional[List[str]]r   c                $   t          t          |                    }d                    d |D                       }|                    |          }d                    ||||g          }|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. 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, c                    g | ]	}|j         
S r)   name.0tools     r+   
<listcomp>z/ZeroShotAgent.create_prompt.<locals>.<listcomp>h   s    <<<d	<<<r*   )
tool_namesz

)templaterE   )r   listjoinformatr   from_template)	r5   r@   rB   rC   rD   rE   tool_stringsrO   rP   s	            r+   create_promptzZeroShotAgent.create_promptO   s    0 /tE{{;;YY<<e<<<==
188J8OO;;6I6RSS 	V!8_UUUU+H555r*   llmr   callback_managerOptional[BaseCallbackManager]Optional[AgentOutputParser]r   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. 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.
        )rB   rC   rD   rE   )rW   promptrX   c                    g | ]	}|j         
S r)   rI   rK   s     r+   rN   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>   s    222Ddi222r*   )	llm_chainallowed_toolsr1   r)   )_validate_toolsrV   r   r6   )r5   rW   r@   rX   r1   rB   rC   rD   rE   r2   r\   r^   rO   _output_parsers                 r+   from_llm_and_toolsz ZeroShotAgent.from_llm_and_toolso   s    6 	E""""" 3+ # 
 
 -
 
 
	
 32E222
&J#*H*H*J*Js 
$(
 
 	
 
 	
r*   Nonec                   t          | j        |           t          |          dk    rt          d| j         d          |D ]!}|j        t          d|j         d          "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descriptionrJ   superr`   )r5   r@   rM   	__class__s      r+   r`   zZeroShotAgent._validate_tools   s    #CL%888u::??WCLWWW    	 	D' >$) > > >   (
 	&&&&&r*   )r2   r   r3   r   )r3   r    )r@   rA   rB   r    rC   r    rD   r    rE   rF   r3   r   )rW   r   r@   rA   rX   rY   r1   rZ   rB   r    rC   r    rD   r    rE   rF   r2   r   r3   r   )r@   rA   r3   rc   )r$   r%   r&   r'   r   r   r1   r(   classmethodr6   propertyr;   r=   r?   r   r   r   rV   rb   r`   __classcell__)ri   s   @r+   r/   r/   &   sa          (-u=M'N'N'NMNNNN" " " [" 5 5 5 X5    X    X  #6/36 6 6 6 [6> 
 ;?59#6/3.
 .
 .
 .
 [.
` ' ' ' ' ' [' ' ' ' 'r*   r/   c                  *    e Zd ZdZedd
            ZdS )	MRKLChainz&Chain that implements the MRKL system.rW   r   chainsList[ChainConfig]r2   r   r3   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 ))rJ   funcrg   )r   r!   r"   r#   )rL   cs     r+   rN   z)MRKLChain.from_chains.<locals>.<listcomp>   sI     
 
 
  ]X0  
 
 
r*   )agentr@   r)   )r/   rb   )r5   rW   ro   r2   r@   ru   s         r+   from_chainszMRKLChain.from_chains   sV    "
 
 
 
 
 00e<<s6e66v666r*   N)rW   r   ro   rp   r2   r   r3   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   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/   rn   r)   r*   r+   <module>r      sJ   U U " " " " " " F F F F F F F F F F F F F F F F * * * * * * 8 8 8 8 8 8 < < < < < < 1 1 1 1 1 1 / / / / / / / / ? ? ? ? ? ?       @ @ @ @ @ @ J J J J J J J J J J 2 2 2 2 2 2 @ @ @ @ @ @ L L L L L L L L L L > > > > > > % % % % % %    *    %  
B' B' B' B' B'E B' B' 
B'J %  
7 7 7 7 7 7 7 
7 7 7r*   