a
    •˜ëj�  ã                   @  sL  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 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)ÚSequence)ÚAnyÚCallableÚ
NamedTupleÚOptional)Ú
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                   @  s*   e Zd ZU dZded< ded< ded< dS )ÚChainConfigzÉConfiguration 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__© r'   r'   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/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e	dddœdd„ƒƒZ
ed	d
œdd„ƒZed	d
œdd„ƒZed	d
œdd„ƒZeeeedfdd	d	d	dddœdd„ƒZeddeeedfddddd	d	d	ddddœ
dd„ƒZeddd œ‡ fd!d"„ƒZ‡  ZS )#ÚZeroShotAgentz]Agent for the MRKL chain.

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

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

        Returns:
            "Thought: "
        zThought:r'   r1   r'   r'   r(   Ú
llm_prefixI   s    zZeroShotAgent.llm_prefixNzSequence[BaseTool]zOptional[list[str]]r   )ÚtoolsÚprefixÚsuffixÚformat_instructionsÚinput_variablesr.   c           	      C  s`   t t|ƒƒ}d dd„ |D ƒ¡}|j|d�}|› d|› d|› d|› �}|rVt||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  s   g | ]
}|j ‘qS r'   ©Úname©Ú.0Útoolr'   r'   r(   Ú
<listcomp>k   ó    z/ZeroShotAgent.create_prompt.<locals>.<listcomp>)Ú
tool_namesz

)Útemplater:   )r   ÚlistÚjoinÚformatr   Zfrom_template)	r/   r6   r7   r8   r9   r:   Ztool_stringsrB   rC   r'   r'   r(   Úcreate_promptR   s    zZeroShotAgent.create_promptr
   zOptional[BaseCallbackManager]zOptional[AgentOutputParser]r   )
Úllmr6   Úcallback_managerr,   r7   r8   r9   r:   r-   r.   c	                 K  s\   |   |¡ | j|||||d�}
t||
|d�}dd„ |D ƒ}|pD|  ¡ }| f |||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.
        )r7   r8   r9   r:   )rH   ÚpromptrI   c                 S  s   g | ]
}|j ‘qS r'   r;   r=   r'   r'   r(   r@   š   rA   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>)Ú	llm_chainZallowed_toolsr,   )Ú_validate_toolsrG   r   r0   )r/   rH   r6   rI   r,   r7   r8   r9   r:   r-   rJ   rK   rB   Z_output_parserr'   r'   r(   Úfrom_llm_and_toolsr   s,    
ûýýüz ZeroShotAgent.from_llm_and_toolsÚNone)r6   r.   c                   sh   t | j|ƒ t|ƒdkr.d| j› d�}t|ƒ‚|D ]$}|jd u r2d|j› d�}t|ƒ‚q2tƒ  |¡ 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Údescriptionr<   ÚsuperrL   )r/   r6   Úmsgr?   ©Ú	__class__r'   r(   rL   £   s    ÿ
ÿ
zZeroShotAgent._validate_tools)r"   r#   r$   r%   r   r   r,   r&   Úclassmethodr   r0   Úpropertyr3   r4   r5   r   r   r   rG   rM   rL   Ú__classcell__r'   r'   rT   r(   r*   (   s6   
ú÷$0r*   c                   @  s(   e Zd ZdZedddddœdd„ƒZd	S )
Ú	MRKLChainz&Chain that implements the MRKL system.r
   zlist[ChainConfig]r   r   )rH   Úchainsr-   r.   c                 K  s.   dd„ |D ƒ}t  ||¡}| f ||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 ))r<   ÚfuncrQ   )r   r   r    r!   )r>   Úcr'   r'   r(   r@   Ñ   s   ûýz)MRKLChain.from_chains.<locals>.<listcomp>)Úagentr6   )r*   rM   )r/   rH   rZ   r-   r6   r]   r'   r'   r(   Úfrom_chains½   s
    úzMRKLChain.from_chainsN)r"   r#   r$   r%   rV   r^   r'   r'   r'   r(   rY   µ   s   rY   N)0r%   Ú
__future__r   Úcollections.abcr   Útypingr   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typing_extensionsr   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*   rY   r'   r'   r'   r(   Ú<module>   s@   ý 	ý