o
    {qi                     @  s   d 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 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$ e	deddG dd deZ%dS )zDAn agent designed to hold a conversation in addition to using tools.    )annotations)Sequence)AnyOptional)
deprecated)BaseCallbackManager)BaseLanguageModel)PromptTemplate)BaseTool)Field)AGENT_DEPRECATION_WARNING)AgentAgentOutputParser)	AgentTypeConvoOutputParser)FORMAT_INSTRUCTIONSPREFIXSUFFIX)validate_tools_single_input)LLMChainz0.1.0z1.0)messageZremovalc                	      s   e Zd ZU dZdZded< 	 eedZded< 	 e		d+d,ddZ
ed-ddZed-ddZed-ddZe	eeedddfd.ddZe	d/ fd!d"Ze	ddeeedddfd0d)d*Z  ZS )1ConversationalAgentz>An agent that holds a conversation in addition to using tools.AIstr	ai_prefix)default_factoryr   output_parserkwargsr   returnc                 K  s
   t |dS )Nr   r   )clsr   r    r"   k/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/langchain/agents/conversational/base.py_get_default_output_parser%   s   
z.ConversationalAgent._get_default_output_parserc                 C  s   t jS )z Return Identifier of agent type.)r   Z CONVERSATIONAL_REACT_DESCRIPTIONselfr"   r"   r#   _agent_type-   s   zConversationalAgent._agent_typec                 C     dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r"   r%   r"   r"   r#   observation_prefix2      z&ConversationalAgent.observation_prefixc                 C  r(   )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r"   r%   r"   r"   r#   
llm_prefix;   r*   zConversationalAgent.llm_prefixZHumanNtoolsSequence[BaseTool]prefixsuffixformat_instructionshuman_prefixinput_variablesOptional[list[str]]r	   c                 C  sn   d dd |D }d dd |D }	|j|	||d}| d| d| d| }
|du r1g d	}t|
|d
S )au  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
            ai_prefix: String to use before AI output. Defaults to "AI".
            human_prefix: String to use before human output.
                Defaults to "Human".
            input_variables: List of input variables the final prompt will expect.
                Defaults to ["input", "chat_history", "agent_scratchpad"].

        Returns:
            A PromptTemplate with the template assembled from the pieces here.
        
c                 S  s    g | ]}d |j  d|j qS )z> z: )namedescription.0Ztoolr"   r"   r#   
<listcomp>b   s     z5ConversationalAgent.create_prompt.<locals>.<listcomp>z, c                 S     g | ]}|j qS r"   r5   r7   r"   r"   r#   r9   d       )
tool_namesr   r1   z

N)inputZchat_historyZagent_scratchpad)templater2   )joinformatr	   )r!   r,   r.   r/   r0   r   r1   r2   Ztool_stringsr=   r?   r"   r"   r#   create_promptD   s   z!ConversationalAgent.create_promptNonec                   s   t  | t| j| d S )N)super_validate_toolsr   __name__)r!   r,   	__class__r"   r#   rE   o   s   z#ConversationalAgent._validate_toolsllmr   callback_managerOptional[BaseCallbackManager]Optional[AgentOutputParser]r   c              	   K  sf   |  | | j|||	||||
d}t|||d}dd |D }|p&| j|d}| d||||d|S )	a  Construct an agent from an LLM and tools.

        Args:
            llm: The language model to use.
            tools: A list of tools to use.
            callback_manager: The callback manager to use. Default is None.
            output_parser: The output parser to use. Default is None.
            prefix: The prefix to use in the prompt. Default is PREFIX.
            suffix: The suffix to use in the prompt. Default is SUFFIX.
            format_instructions: The format instructions to use.
                Default is FORMAT_INSTRUCTIONS.
            ai_prefix: The prefix to use before AI output. Default is "AI".
            human_prefix: The prefix to use before human output.
                Default is "Human".
            input_variables: The input variables to use. Default is None.
            **kwargs: Any additional keyword arguments to pass to the agent.

        Returns:
            An agent.
        )r   r1   r.   r/   r0   r2   )rI   promptrJ   c                 S  r:   r"   r;   r7   r"   r"   r#   r9      r<   z:ConversationalAgent.from_llm_and_tools.<locals>.<listcomp>r    )	llm_chainZallowed_toolsr   r   Nr"   )rE   rB   r   r$   )r!   rI   r,   rJ   r   r.   r/   r0   r   r1   r2   r   rM   rN   r=   Z_output_parserr"   r"   r#   from_llm_and_toolst   s6   
#	z&ConversationalAgent.from_llm_and_tools)r   )r   r   r   r   r   r   )r   r   )r,   r-   r.   r   r/   r   r0   r   r   r   r1   r   r2   r3   r   r	   )r,   r-   r   rC   )rI   r   r,   r-   rJ   rK   r   rL   r.   r   r/   r   r0   r   r   r   r1   r   r2   r3   r   r   r   r   )rF   
__module____qualname____doc__r   __annotations__r   r   r   classmethodr$   propertyr'   r)   r+   r   r   r   rB   rE   rO   __classcell__r"   r"   rG   r#   r      sF   
 *r   N)&rR   
__future__r   collections.abcr   typingr   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr	   Zlangchain_core.toolsr
   Zpydanticr   Zlangchain._api.deprecationr   Zlangchain.agents.agentr   r   Zlangchain.agents.agent_typesr   Z-langchain.agents.conversational.output_parserr   Z&langchain.agents.conversational.promptr   r   r   Zlangchain.agents.utilsr   Zlangchain.chainsr   r   r"   r"   r"   r#   <module>   s.    