o
    `+ i8                     @  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mZm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"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dddG dd de"Z.dS )DAn agent designed to hold a conversation in addition to using tools.    )annotations)Sequence)AnyOptional)
deprecated)AgentAction)BaseCallbackManager)BaseLanguageModel)	AIMessageBaseMessageHumanMessage)BaseOutputParser)BasePromptTemplate)ChatPromptTemplateHumanMessagePromptTemplateMessagesPlaceholderSystemMessagePromptTemplate)BaseTool)Field)AgentAgentOutputParserConvoOutputParser)PREFIXSUFFIXTEMPLATE_TOOL_RESPONSE)validate_tools_single_input)LLMChainz0.1.0Zcreate_json_chat_agentz1.0)alternativeZremovalc                      s   e Zd ZU dZeedZded< 	 e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
d/ fddZe
eeddfd0ddZd1d#d$Ze
ddeedfd2d+d,Z  ZS )3ConversationalChatAgentr   )default_factoryr   output_parserstrtemplate_tool_responsekwargsr   returnc                 K  s   t  S Nr   )clsr%    r)   u/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/langchain/agents/conversational_chat/base.py_get_default_output_parser,   s   z2ConversationalChatAgent._get_default_output_parserc                 C  s   t r'   )NotImplementedErrorselfr)   r)   r*   _agent_type0   s   z#ConversationalChatAgent._agent_typec                 C     dS )z]Prefix to append the observation with.

        Returns:
            "Observation: "
        zObservation: r)   r-   r)   r)   r*   observation_prefix4      z*ConversationalChatAgent.observation_prefixc                 C  r0   )zVPrefix to append the llm call with.

        Returns:
            "Thought: "
        zThought:r)   r-   r)   r)   r*   
llm_prefix=   r2   z"ConversationalChatAgent.llm_prefixtoolsSequence[BaseTool]Nonec                   s   t  | t| j| d S r'   )super_validate_toolsr   __name__)r(   r4   	__class__r)   r*   r8   F   s   z'ConversationalChatAgent._validate_toolsNsystem_messagehuman_messageinput_variablesOptional[list[str]]Optional[BaseOutputParser]r   c                 C  s   d dd |D }d dd |D }|p|  }|j| d}	|	j||d}
|du r1g d	}t|td
dt|
tddg}t||dS )a  Create a prompt for the agent.

        Args:
            tools: The tools to use.
            system_message: The system message to use.
                Defaults to the PREFIX.
            human_message: The human message to use.
                Defaults to the SUFFIX.
            input_variables: The input variables to use. Defaults to None.
            output_parser: The output parser to use. Defaults to None.

        Returns:
            A PromptTemplate.
        
c                 S  s    g | ]}d |j  d|j qS )z> z: )namedescription.0Ztoolr)   r)   r*   
<listcomp>c   s     z9ConversationalChatAgent.create_prompt.<locals>.<listcomp>z, c                 S     g | ]}|j qS r)   rB   rD   r)   r)   r*   rF   e       )format_instructions)
tool_namesr4   N)inputchat_historyagent_scratchpadrM   )Zvariable_namerN   )r>   messages)	joinr+   formatZget_format_instructionsr   Zfrom_templater   r   r   )r(   r4   r<   r=   r>   r"   Ztool_stringsrK   _output_parserrJ   Zfinal_promptrO   r)   r)   r*   create_promptK   s(   z%ConversationalChatAgent.create_promptintermediate_stepslist[tuple[AgentAction, str]]list[BaseMessage]c                 C  sF   g }|D ]\}}| t|jd t| jj|dd}| | q|S )zJConstruct the scratchpad that lets the agent continue its thought process.)content)observation)appendr   logr   r$   rQ   )r.   rT   ZthoughtsactionrX   r=   r)   r)   r*   _construct_scratchpadx   s   z-ConversationalChatAgent._construct_scratchpadllmr
   callback_managerOptional[BaseCallbackManager]Optional[AgentOutputParser]r   c                 K  s\   |  | |p
|  }	| j|||||	d}
t||
|d}dd |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.
            system_message: The system message to use. Default is PREFIX.
            human_message: The human message to use. Default is SUFFIX.
            input_variables: The input variables to use. Default is None.
            **kwargs: Any additional arguments.

        Returns:
            An agent.
        )r<   r=   r>   r"   )r]   promptr^   c                 S  rG   r)   rH   rD   r)   r)   r*   rF      rI   z>ConversationalChatAgent.from_llm_and_tools.<locals>.<listcomp>)	llm_chainZallowed_toolsr"   Nr)   )r8   r+   rS   r   )r(   r]   r4   r^   r"   r<   r=   r>   r%   rR   ra   rb   rK   r)   r)   r*   from_llm_and_tools   s,   
z*ConversationalChatAgent.from_llm_and_tools)r%   r   r&   r   )r&   r#   )r4   r5   r&   r6   )r4   r5   r<   r#   r=   r#   r>   r?   r"   r@   r&   r   )rT   rU   r&   rV   )r]   r
   r4   r5   r^   r_   r"   r`   r<   r#   r=   r#   r>   r?   r%   r   r&   r   )r9   
__module____qualname____doc__r   r   r"   __annotations__r   r$   classmethodr+   propertyr/   r1   r3   r8   r   r   rS   r\   rc   __classcell__r)   r)   r:   r*   r    #   s<   
 
,r    N)/rf   
__future__r   collections.abcr   typingr   r   Zlangchain_core._apir   Zlangchain_core.agentsr   Zlangchain_core.callbacksr	   Zlangchain_core.language_modelsr
   Zlangchain_core.messagesr   r   r   Zlangchain_core.output_parsersr   Zlangchain_core.promptsr   Zlangchain_core.prompts.chatr   r   r   r   Zlangchain_core.toolsr   Zpydanticr   Zlangchain.agents.agentr   r   Z2langchain.agents.conversational_chat.output_parserr   Z+langchain.agents.conversational_chat.promptr   r   r   Zlangchain.agents.utilsr   Zlangchain.chainsr   r    r)   r)   r)   r*   <module>   s*    