o
    {qiW                     @  s@  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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)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&   a/home/app/PaddleOCR-VL/.venv_paddleocr/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_parser5   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_type9   s   zZeroShotAgent._agent_typec                 C     dS )z]Prefix to append the observation with.

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

        Returns:
            "Thought: "
        zThought:r&   r0   r&   r&   r'   
llm_prefixG   r5   zZeroShotAgent.llm_prefixNtoolsSequence[BaseTool]prefixsuffixformat_instructionsinput_variablesOptional[list[str]]r   c           	      C  s`   t t|}ddd |D }|j|d}| d| d| d| }|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>i       z/ZeroShotAgent.create_prompt.<locals>.<listcomp>)
tool_namesz

)templater<   )r   listjoinformatr   Zfrom_template)	r.   r7   r9   r:   r;   r<   Ztool_stringsrF   rG   r&   r&   r'   create_promptP   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.
        )r9   r:   r;   r<   )rL   promptrM   c                 S  r>   r&   r?   rA   r&   r&   r'   rD      rE   z4ZeroShotAgent.from_llm_and_tools.<locals>.<listcomp>)	llm_chainZallowed_toolsr+   Nr&   )_validate_toolsrK   r   r/   )r.   rL   r7   rM   r+   r9   r:   r;   r<   r,   rP   rQ   rF   Z_output_parserr&   r&   r'   from_llm_and_toolsp   s,   
z ZeroShotAgent.from_llm_and_toolsNonec                   sh   t | j| t|dkrd| j d}t||D ]}|jd u r+d|j d}t|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@   superrR   )r.   r7   msgrC   	__class__r&   r'   rR      s   
zZeroShotAgent._validate_tools)r,   r   r-   r   )r-   r   )r7   r8   r9   r   r:   r   r;   r   r<   r=   r-   r   )rL   r
   r7   r8   rM   rN   r+   rO   r9   r   r:   r   r;   r   r<   r=   r,   r   r-   r   )r7   r8   r-   rT   )r!   r"   r#   r$   r   r   r+   r%   classmethodr/   propertyr2   r4   r6   r   r   r   rK   rS   rR   __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.rL   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 ))r@   funcrW   )r   r   r   r    )rB   cr&   r&   r'   rD      s    z)MRKLChain.from_chains.<locals>.<listcomp>)agentr7   Nr&   )r)   rS   )r.   rL   r`   r,   r7   rd   r&   r&   r'   from_chains   s
   zMRKLChain.from_chainsN)rL   r
   r`   ra   r,   r   r-   r   )r!   r"   r#   r$   r\   re   r&   r&   r&   r'   r_      s    r_   N).r$   
__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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@     