a
    ag	                     @  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'   h/var/www/html/cobodadashboardai.evdpl.com/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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 )#ZeroShotAgentzcAgent for the MRKL chain.

    Parameters:
        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_parser4   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_type8   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_prefixF   s    zZeroShotAgent.llm_prefixNzSequence[BaseTool]zOptional[List[str]]r   )toolsprefixsuffixformat_instructionsinput_variablesr.   c           	      C  sX   t t|}ddd |D }|j|d}d||||g}|rNt||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>h       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_promptO   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_toolso   s,    
z ZeroShotAgent.from_llm_and_toolsNone)r6   r.   c                   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descriptionr<   superrL   )r/   r6   r?   	__class__r'   r(   rL      s    
zZeroShotAgent._validate_tools)r"   r#   r$   r%   r   r   r,   r&   classmethodr0   propertyr3   r4   r5   r   r   r   rG   rM   rL   __classcell__r'   r'   rS   r(   r*   &   s4   
$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   rY   r-   r6   r\   r'   r'   r(   from_chains   s
    zMRKLChain.from_chainsN)r"   r#   r$   r%   rU   r]   r'   r'   r'   r(   rX      s   rX   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*   rX   r'   r'   r'   r(   <module>   s<     