o
    {qi                     @   sl   d dl 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dZd	Zd
ZG dd de	ZdS )    N)Union)AgentActionAgentFinish)OutputParserException)AgentOutputParser)FORMAT_INSTRUCTIONSzFinal Answer:z1Invalid Format: Missing 'Action:' after 'Thought:z7Invalid Format: Missing 'Action Input:' after 'Action:'zHParsing LLM output produced both a final answer and a parse-able action:c                   @   sZ   e Zd ZU dZe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 )MRKLOutputParserz&MRKL Output parser for the chat agent.format_instructions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/mrkl/output_parser.pyget_format_instructions   s   z(MRKLOutputParser.get_format_instructionstextc                 C   sj  t |v }d}t||tj}|rJ|rJ|t ||dk r?|t tt  }|d|}td|||  i|d| S t	 d| }t
||ro|d }|d}	|	d	}
|
d
du ri|
d}
t||
|S |rtd|t d  i|S td|tjsd| d}t
|t|ddtd|tjsd| d}t
|t|ddd| d}t
|)a*  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.
        zFAction\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)r   z

outputNz:        zSELECT F"zAction\s*\d*\s*:[\s]*(.*?)zCould not parse LLM output: ``T)ZobservationZ
llm_outputZsend_to_llmz,[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*))FINAL_ANSWER_ACTIONresearchDOTALLfindgrouplenr   strip.FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGEr   
startswithr   split*MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE/MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE)r   r   Zincludes_answerregexZaction_matchstart_indexZ	end_indexmsgactionZaction_inputZ
tool_inputr   r   r   parse    s`   



zMRKLOutputParser.parsec                 C   s   dS )NZmrklr   r   r   r   r   _typee   s   zMRKLOutputParser._typeN)__name__
__module____qualname____doc__r   r	   str__annotations__r   r   r   r   r)   propertyr*   r   r   r   r   r      s   
 Er   )r   typingr   Zlangchain_core.agentsr   r   Zlangchain_core.exceptionsr   Zlangchain.agents.agentr   Zlangchain.agents.mrkl.promptr   r   r#   r$   r    r   r   r   r   r   <module>   s    