o
    Jjg                     @  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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dddG dd deZ*ed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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&   R/var/www/html/zoom/venv/lib/python3.10/site-packages/langchain/agents/mrkl/base.pyr      s
   
 r   z0.1.0create_react_agentz1.0)alternative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_parser/   s   z(ZeroShotAgent._get_default_output_parserr   c                 C  s   t jS )z Return Identifier of agent type.)r   ZERO_SHOT_REACT_DESCRIPTIONselfr&   r&   r'   _agent_type3   s   zZeroShotAgent._agent_typec                 C     dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r&   r3   r&   r&   r'   observation_prefix8      z ZeroShotAgent.observation_prefixc                 C  r6   )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r&   r3   r&   r&   r'   
llm_prefixA   r8   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>c       z/ZeroShotAgent.create_prompt.<locals>.<listcomp>)
tool_namesz

)templater?   )r   listjoinformatr   from_template)	r0   r:   r<   r=   r>   r?   tool_stringsrI   rJ   r&   r&   r'   create_promptJ   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?   )rQ   promptrR   c                 S  rA   r&   rB   rD   r&   r&   r'   rG      rH   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>)	llm_chainallowed_toolsr-   Nr&   )_validate_toolsrP   r   r1   )r0   rQ   r:   rR   r-   r<   r=   r>   r?   r.   rU   rV   rI   _output_parserr&   r&   r'   from_llm_and_toolsj   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descriptionrC   superrX   )r0   r:   rF   	__class__r&   r'   rX      s   
zZeroShotAgent._validate_tools)r.   r   r/   r   )r/   r   )r:   r;   r<   r   r=   r   r>   r   r?   r@   r/   r   )rQ   r   r:   r;   rR   rS   r-   rT   r<   r   r=   r   r>   r   r?   r@   r.   r   r/   r   )r:   r;   r/   r[   )r!   r"   r#   r$   r   r   r-   r%   classmethodr1   propertyr5   r7   r9   r   r   r   rP   rZ   rX   __classcell__r&   r&   r`   r'   r+   %   s6   
 0r+   )r*   c                   @  s   e Zd ZdZedd
dZdS )	MRKLChainz&Chain that implements the MRKL system.rQ   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 ))rC   funcr^   )r   r   r   r    )rE   cr&   r&   r'   rG      s    z)MRKLChain.from_chains.<locals>.<listcomp>)agentr:   Nr&   )r+   rZ   )r0   rQ   rf   r.   r:   rj   r&   r&   r'   from_chains   s
   zMRKLChain.from_chainsN)rQ   r   rf   rg   r.   r   r/   r   )r!   r"   r#   r$   rb   rk   r&   r&   r&   r'   re      s    re   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.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+   re   r&   r&   r&   r'   <module>   s,      
