o
    Zh	                     @  s<  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 deZ+ededdG dd deZ,ededdG d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                   @  s*   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__ r'   r'   Q/var/www/html/lang_env/lib/python3.10/site-packages/langchain/agents/mrkl/base.pyr      s
   
 r   z0.1.0z1.0)messageZremovalc                      s   e Zd ZU dZeedZded< ed'dd	Z	e
d(ddZe
d(ddZe
d(ddZeeeedfd)ddZeddeeedfd*d"d#Zed+ fd%d&Z  ZS ),ZeroShotAgentzcAgent for the MRKL chain.

    Parameters:
        output_parser: Output parser for the agent.
    )default_factoryr   output_parserkwargsr   returnc                 K  s   t  S )Nr   )clsr-   r'   r'   r(   _get_default_output_parser4   s   z(ZeroShotAgent._get_default_output_parserr   c                 C  s   t jS )z Return Identifier of agent type.)r   ZZERO_SHOT_REACT_DESCRIPTIONselfr'   r'   r(   _agent_type8   s   zZeroShotAgent._agent_typec                 C     dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r'   r1   r'   r'   r(   observation_prefix=      z ZeroShotAgent.observation_prefixc                 C  r4   )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r'   r1   r'   r'   r(   
llm_prefixF   r6   zZeroShotAgent.llm_prefixNtoolsSequence[BaseTool]prefixsuffixformat_instructionsinput_variablesOptional[List[str]]r   c           	      C  sX   t t|}ddd |D }|j|d}d||||g}|r't||dS t|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                 S     g | ]}|j qS r'   name.0toolr'   r'   r(   
<listcomp>h       z/ZeroShotAgent.create_prompt.<locals>.<listcomp>)
tool_namesz

)templater=   )r   listjoinformatr   Zfrom_template)	r/   r8   r:   r;   r<   r=   Ztool_stringsrG   rH   r'   r'   r(   create_promptO   s   
zZeroShotAgent.create_promptllmr   callback_managerOptional[BaseCallbackManager]Optional[AgentOutputParser]r   c	                 K  s\   |  | | j|||||d}
t||
|d}d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.
        )r:   r;   r<   r=   )rM   promptrN   c                 S  r?   r'   r@   rB   r'   r'   r(   rE      rF   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>)	llm_chainZallowed_toolsr,   Nr'   )_validate_toolsrL   r   r0   )r/   rM   r8   rN   r,   r:   r;   r<   r=   r-   rQ   rR   rG   Z_output_parserr'   r'   r(   from_llm_and_toolso   s,   
z ZeroShotAgent.from_llm_and_toolsNonec                   s`   t | j| t|dkrtd| j d|D ]}|jd u r'td|j dq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descriptionrA   superrS   )r/   r8   rD   	__class__r'   r(   rS      s   
zZeroShotAgent._validate_tools)r-   r   r.   r   )r.   r   )r8   r9   r:   r   r;   r   r<   r   r=   r>   r.   r   )rM   r   r8   r9   rN   rO   r,   rP   r:   r   r;   r   r<   r   r=   r>   r-   r   r.   r   )r8   r9   r.   rU   )r"   r#   r$   r%   r   r   r,   r&   classmethodr0   propertyr3   r5   r7   r   r   r   rL   rT   rS   __classcell__r'   r'   rZ   r(   r*   &   s6   
 0r*   c                   @  s   e Zd ZdZedd
dZdS )	MRKLChainz&Chain that implements the MRKL system.rM   r   chainsList[ChainConfig]r-   r   r.   r   c                 K  s.   d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                 S  s    g | ]}t |j|j|jd qS ))rA   funcrX   )r   r   r    r!   )rC   cr'   r'   r(   rE      s    z)MRKLChain.from_chains.<locals>.<listcomp>)agentr8   Nr'   )r*   rT   )r/   rM   r`   r-   r8   rd   r'   r'   r(   from_chains   s
   zMRKLChain.from_chainsN)rM   r   r`   ra   r-   r   r.   r   )r"   r#   r$   r%   r\   re   r'   r'   r'   r(   r_      s    r_   N).r%   
__future__r   typingr   r   r   r   r   r   Zlangchain_core._apir	   Zlangchain_core.callbacksr
   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr   Zlangchain_core.toolsr   r   Zlangchain_core.tools.renderr   Zpydanticr   Zlangchain._api.deprecationr   Zlangchain.agents.agentr   r   r   Zlangchain.agents.agent_typesr   Z#langchain.agents.mrkl.output_parserr   Zlangchain.agents.mrkl.promptr   r   r   Zlangchain.agents.utilsr   Zlangchain.chainsr   r   r*   r_   r'   r'   r'   r(   <module>   s>      