a
    •˜ëj¤  ã                   @   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                   @   sV   e Zd ZU dZeZeed< edœdd„Zee	e
ef dœdd„Zeedœ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   úb/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/agents/mrkl/output_parser.pyÚget_format_instructions   s    z(MRKLOutputParser.get_format_instructions)Útextr
   c                 C   sr  t |v }d}t ||tj¡}|r”|r”| t ¡| | d¡¡k r~| t ¡tt ƒ }| d|¡}td|||…  ¡ i|d|… ƒS t	› d|› �}t
|ƒ‚|rÞ| d¡ ¡ }| d¡}	|	 d	¡}
|
 d
¡du rÒ|
 d¡}
t||
|ƒS |rþtd| t ¡d  ¡ i|ƒS t d|tj¡�s,d|› d�}t
|t|dd�‚t d|tj¡�sZd|› d�}t
|t|dd�‚d|› d�}t
|ƒ‚dS )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.
        zAAction\s*\d*\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   Ú_typeb   s    zMRKLOutputParser._typeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   ÚstrÚ__annotations__r   r   r   r   r)   Úpropertyr*   r   r   r   r   r      s   
B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   ÿÿÿ