o
    {qi	                     @   st   d dl Z d dl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ZG d	d
 d
eZdS )    N)Pattern)Union)AgentActionAgentFinish)OutputParserException)AgentOutputParser)FORMAT_INSTRUCTIONSzFinal Answer:c                   @   sr   e Zd ZU dZeZeed< 	 e	dej
Zeed< 	 defddZdedeeef fd	d
ZedefddZdS )ChatOutputParserz!Output parser for the chat agent.format_instructionsz ^.*?`{3}(?:json)?\n(.*?)`{3}.*?$patternreturnc                 C   s   | j S )z<Returns formatting instructions for the given output parser.)r
   self r   j/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/langchain/agents/chat/output_parser.pyget_format_instructions   s   z(ChatOutputParser.get_format_instructionstextc           
   
   C   s   t |v }z8| j|}|sd}t||d}t| }d|v }|r0|r0d| }t|t	|d |
di |W S  tyj } z"|sPd| }t|||t d  }	td|	i|W  Y d	}~S d	}~ww )
ad  Parse the output from the agent into
        an AgentAction or AgentFinish object.

        Args:
            text: The text to parse.

        Returns:
            An AgentAction or AgentFinish object.

        Raises:
            OutputParserException: If the output could not be parsed.
            ValueError: If the action could not be found.
        zaction not found   actionzDParsing LLM output produced a final answer and a parse-able action: Zaction_inputzCould not parse LLM output: outputN)FINAL_ANSWER_ACTIONr   search
ValueErrorgroupjsonloadsstripr   r   get	Exceptionsplitr   )
r   r   Zincludes_answerfoundmsgr   responseZincludes_actionexcr   r   r   r   parse   s8   



zChatOutputParser.parsec                 C   s   dS )NZchatr   r   r   r   r   _typeH   s   zChatOutputParser._typeN)__name__
__module____qualname____doc__r   r
   str__annotations__recompileDOTALLr   r   r   r   r   r   r%   propertyr&   r   r   r   r   r	      s   
 ,r	   )r   r-   r   typingr   Zlangchain_core.agentsr   r   Zlangchain_core.exceptionsr   Zlangchain.agents.agentr   Zlangchain.agents.chat.promptr   r   r	   r   r   r   r   <module>   s    