
    h                     r    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y)    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                   ^    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y)	MRKLOutputParserz&MRKL Output parser for the chat agent.format_instructionsreturnc                     | j                   S )z<Returns formatting instructions for the given output parser.)r   selfs    a/var/www/html/eduruby.in/venv/lib/python3.12/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|j	                  t               |j	                  |j                  d            k  rY|j	                  t               t        t               z   }|j	                  d|      }t        d||| j                         i|d|       S t         d| }t        |      |rr|j                  d      j                         }|j                  d      }	|	j                  d	      }
|
j                  d
      du r|
j                  d      }
t        ||
|      S |r2t        d|j                  t               d   j                         i|      S t        j                  d|t        j                        sd| d}t        |t        |d      t        j                  d|t        j                        sd| d}t        |t        |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)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   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   includes_answerregexaction_matchstart_index	end_indexmsgactionaction_input
tool_inputs              r   parsezMRKLOutputParser.parse    s    .5U 	 yybii8Oyy,-		,:L:LQ:O0PP"ii(;<sCV?WW IIfk:	"tK	:@@BC)$  DDBtfMC',,!''*002F'--a0L%++C0J$$Y/58'--c2
vz4884::&9:2>DDFG 
 yy6biiH1$q9C'F 	  yy;II

 2$q9C'K 	  .dV15#C((r   c                      y)Nmrkl r   s    r   _typezMRKLOutputParser._typee   s    r   N)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   r   r5   propertyr9   r8   r   r   r
   r
      s[    022)( (C)# C)%[(@"A C)J s  r   r
   )r    typingr   langchain_core.agentsr   r   langchain_core.exceptionsr   langchain.agents.agentr   langchain.agents.mrkl.promptr   r   r*   r+   r'   r
   r8   r   r   <module>rF      sI    	  : ; 4 <% 7 + > 0 O /
Q( Qr   