HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/langchain/agents/mrkl/__pycache__/base.cpython-310.pyc
o

���g	�@s<dZddlmZddlmZmZmZmZmZm	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZmZdd	lmZdd
lmZddlmZddlmZmZmZdd
lm Z ddl!m"Z"ddl#m$Z$m%Z%m&Z&ddl'm(Z(ddl)m*Z*Gdd�de�Z+ededd�Gdd�de��Z,ededd�Gdd�de��Z-dS)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*eZdZUdZded<ded<ded<dS)�ChainConfigz�Configuration 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'�E/usr/local/lib/python3.10/dist-packages/langchain/agents/mrkl/base.pyrs

rz0.1.0z1.0)�message�removalcs�eZdZUdZeed�Zded<ed'dd	��Z	e
d(dd��Ze
d(d
d��Ze
d(dd��Z
eeeedfd)dd��Zeddeeedfd*d"d#��Zed+�fd%d&��Z�ZS),�
ZeroShotAgentzcAgent for the MRKL chain.

    Parameters:
        output_parser: Output parser for the agent.
    )�default_factoryr�
output_parser�kwargsr�returncKst�S)Nr)�clsr.r'r'r(�_get_default_output_parser4sz(ZeroShotAgent._get_default_output_parserrcCstjS)z Return Identifier of agent type.)r�ZERO_SHOT_REACT_DESCRIPTION��selfr'r'r(�_agent_type8szZeroShotAgent._agent_typecC�dS)z]Prefix to append the observation with.

        Returns:
            "Observation: "
        z
Observation: r'r3r'r'r(�observation_prefix=�z ZeroShotAgent.observation_prefixcCr6)zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r'r3r'r'r(�
llm_prefixFr8zZeroShotAgent.llm_prefixN�tools�Sequence[BaseTool]�prefix�suffix�format_instructions�input_variables�Optional[List[str]]rc	CsXtt|��}d�dd�|D��}|j|d�}d�||||g�}|r't||d�St�|�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, cS�g|]}|j�qSr'��name��.0�toolr'r'r(�
<listcomp>h�z/ZeroShotAgent.create_prompt.<locals>.<listcomp>)�
tool_namesz

)�templater?)r�list�join�formatr�
from_template)	r0r:r<r=r>r?�tool_stringsrIrJr'r'r(�
create_promptOs
zZeroShotAgent.create_prompt�llmr�callback_manager�Optional[BaseCallbackManager]�Optional[AgentOutputParser]rc	Ks\|�|�|j|||||d�}
t||
|d�}dd�|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?)rQ�promptrRcSrAr'rBrDr'r'r(rG�rHz4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>)�	llm_chain�
allowed_toolsr-Nr')�_validate_toolsrPrr1)r0rQr:rRr-r<r=r>r?r.rUrVrI�_output_parserr'r'r(�from_llm_and_toolsos,
����z ZeroShotAgent.from_llm_and_tools�Nonecs`t|j|�t|�dkrtd|j�d���|D]}|jdur'td|j�d���qt��|�dS)NrzGot no tools for z%. At least one tool must be provided.zGot a tool zN without a description. For this agent, a description must always be provided.)rr"�len�
ValueError�descriptionrC�superrX)r0r:rF��	__class__r'r(rX�s�
��zZeroShotAgent._validate_tools)r.rr/r)r/r)r:r;r<rr=rr>rr?r@r/r)rQrr:r;rRrSr-rTr<rr=rr>rr?r@r.rr/r)r:r;r/r[)r"r#r$r%rrr-r&�classmethodr1�propertyr5r7r9rrrrPrZrX�
__classcell__r'r'r`r(r+&s6
��0r+c@seZdZdZed
d
d��ZdS)�	MRKLChainz&Chain that implements the MRKL system.rQr�chains�List[ChainConfig]r.rr/rcKs.dd�|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.
        cSs g|]}t|j|j|jd��qS))rC�funcr^)rrr r!)rE�cr'r'r(rG�s���z)MRKLChain.from_chains.<locals>.<listcomp>)�agentr:Nr')r+rZ)r0rQrfr.r:rjr'r'r(�from_chains�s
�zMRKLChain.from_chainsN)rQrrfrgr.rr/r)r"r#r$r%rbrkr'r'r'r(re�sreN).r%�
__future__r�typingrrrrrr�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.agentrrr�langchain.agents.agent_typesr�#langchain.agents.mrkl.output_parserr�langchain.agents.mrkl.promptrrr�langchain.agents.utilsr�langchain.chainsrrr+rer'r'r'r(�<module>s> ��