
    Ng                     z    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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                   l    e Zd ZU dZeZeed<   	 defdZdede	e
ef         fdZedefd            ZdS )	MRKLOutputParserz&MRKL Output parser for the chat agent.format_instructionsreturnc                     | j         S )z<Returns formatting instructions for the given output parser.)r   selfs    _/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/agents/mrkl/output_parser.pyget_format_instructionsz(MRKLOutputParser.get_format_instructions   s    ''    textc                    t           |v }d}t          j        ||t          j                  }|r|r|                    t                     |                    |                    d                    k     ry|                    t                     t          t                     z   }|                    d|          }t          d|||                                         i|d|                   S t          t           d|           |r|                    d                                          }|                    d          }|                    d	          }	|	                    d
          du r|	                    d          }	t          ||	|          S |rBt          d|                    t                     d                                         i|          S t          j        d|t          j                  st          d| dt          |d          t          j        d|t          j                  st          d| dt          |d          t          d| d          )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)observation
llm_outputsend_to_llmz,[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*))FINAL_ANSWER_ACTIONresearchDOTALLfindgrouplenr   stripr   .FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE
startswithr   split*MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE/MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE)
r   r   includes_answerregexaction_matchstart_index	end_indexactionaction_input
tool_inputs
             r   parsezMRKLOutputParser.parse    s    .5U 	 ybi88 	O 	yy,--		,:L:LQ:O:O0P0PPP"ii(;<<sCV?W?WW IIfk::	"tK	$9:@@BBCT*9*EU   ,EOOOO    	!''**0022F'--a00L%++C00J$$Y//588'--c22
vz4888 	4::&9::2>DDFFG   y6biHH 	Q'7777F 	    ;T29
 
 
	Q (7777K 	    ((O(O(O(OPPPr   c                     dS )Nmrkl r   s    r   _typezMRKLOutputParser._typea   s    vr   N)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   r   r4   propertyr8   r7   r   r   r
   r
      s         002222)( ( ( ( (?Q# ?Q%[(@"A ?Q ?Q ?Q ?QB s    X  r   r
   )r    typingr   langchain_core.agentsr   r   langchain_core.exceptionsr   langchain.agents.agentr   langchain.agents.mrkl.promptr   r   r*   r+   r'   r
   r7   r   r   <module>rE      s    				       : : : : : : : : ; ; ; ; ; ; 4 4 4 4 4 4 < < < < < <% 7 + > 0 O /
M M M M M( M M M M Mr   