
    .`i%                         d dl mZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ  ee          Z G d d	e          ZdS )
    )SequenceN)PreTrainedTokenizerBase)ChatCompletionRequest)DeltaMessage)init_logger)ReasoningParserc                       e Zd ZdZdef fdZdee         defdZ	de
e         de
e         fdZded	edeed
z  ed
z  f         fdZdee         dee         defdZdedededee         dee         dee         ded
z  fdZ xZS )HunyuanA13BReasoningParserah  
    Reasoning parser for Hunyuan A13B Model

    HunyuanReasoningParser

    This class implements a reasoning parser specifically designed
    for the Hunyuan A13B Model. It is responsible for parsing and
    extracting structured reasoning and answer segments from model
    outputs that follow a specific pattern.

    Key Features:
        - For non-stream output , Recognizes and extracts reasoning ("think")
         and answer ("answer") sections from text using regular expressions.
        - For stream process, it requires a token id sequences to change the
          reasoning state and other state so it maintains internal state to
          manage parsing across multiple token.


    think start: "<think>
": [14023, 771, 397]
    think ends: "
</think>
<answer>
": [198, 524, 27963, 397, 27, 9399, 397]
    response ends: "
</answer>": [524, 9399, 29]
    	tokenizerc                 j    t                      j        |g|R i | d| _        d| _        d| _        d| _        t          j        d| j         d| j         d| j         t          j                  | _	        t          j        | j         d| j         dt          j                  | _
        g d	| _        g d
| _        g d| _        g d| _        g d| _        g d| _        g | _        g | _        d| _        ddg| _        d| _        | j        | _        | j        | _        d| _        g | _        d| _        d S )Nz	<think>\nz\n</think>\nz\n</think>\n<answer>\nz\n</answer>z(?:z(.*?)z)?(.*?)z(.*))6      )r   r   S  )     ;m  r      $  r   )r   r   r   r   r   r   )r   r   r      )	r   r   r   r   r   r   r   r   r   	reasoningresponseidler    )super__init__think_start_exprthink_end_exprresponse_start_exprresponse_end_exprrecompileDOTALLfull_match_reasoning_regexhalf_match_reasoning_regexthink_start_idsthink_start_ids_fastresponse_start_idsresponse_start_ids_fastresponse_end_idsfast_think_idsbuffered_textbuffered_idscurrent_state
all_statesexpected_sequenceexpected_sequence_sidesequence_indextoken_buffertext_buffer)selfr   argskwargs	__class__s       /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/reasoning/hunyuan_a13b_reasoning_parser.pyr   z#HunyuanA13BReasoningParser.__init__+   st   4T444V444 ,-#< !/*,*h4(hht/GhhPTPfhhI+
 +
'
 +-*%JJD,DJJJBI+
 +
'  100$6$6$6!"G"G"G'G'G'G$ 4 4 4PPP  (&
3#!%!5&*&?#    	input_idsreturnc                     | j         dk    S )Nr   )r.   r5   r;   s     r9   is_reasoning_endz+HunyuanA13BReasoningParser.is_reasoning_endR   s    !Z//r:   c                     g S )N r>   s     r9   extract_content_idsz.HunyuanA13BReasoningParser.extract_content_idsU   s	    
 	r:   model_outputrequestNc                    | j                             |          }|r9|d         \  }}t          |          dk    rd}t          |          dk    rd}||fS | j        }|                    |          }|rp|d         \  }}|                    | j                  r|dt          | j                            }t          |          dk    rd}t          |          dk    rd}||fS d|fS )a  Extract the reasoning content & content sections, respectively.
        If the sequence doesn't match what we expect, i.e., the model generates
        something else, all content is considered non-reasoning content.

        Args:
            model_output (str): Output of the model to be parsed.
            request (ChatCompletionRequest): Request being processed.

        Returns:
            tuple[Optional[str], Optional[str]]: Tuple pair containing the
            reasoning content and non-reasoning content.
        r   N)r$   findalllenr%   endswithr    )r5   rC   rD   re_matchr   response_contentfallback_regexfallback_matchs           r9   extract_reasoningz,HunyuanA13BReasoningParser.extract_reasoning\   s      2::<HH 	/*21+'I'9~~"" 	#$$))#' ...8'//== 	/*8*;'I'(()?@@ T#34Rs4;Q7R7R6R4R#S 9~~"" 	#$$))#' ...\!!r:   subsequencesequencec                     |sdS d}|D ]&}|t          |          k     r|||         k    r|dz  }'|t          |          k    S )NFr      )rG   )r5   rN   rO   sub_idxnums        r9   !_is_strict_increasing_subsequencez<HunyuanA13BReasoningParser._is_strict_increasing_subsequence   sd      	5 	 	C[))))c[5I.I.I1#k****r:   previous_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsc                 J     j         } j        } j        }	t          |          dk    sJ |d         }
 fd} fd} ||
          }|r j                            |
            xj        |z  c_         xj        dz  c_         ||
          rz j        dk    rd _        | _	         j
         _        n? j        dk    rd _        |	 _	        n% j        dk    rd _        | _	         j         _        d _        g  _        d _        n j        rdt           j                  dk    rL j        |z   }d _        g  _        d _         j        dk    rt          |d	
          S t          d	|
          S  j        dk    rt          |d	
          S t          d	|
          S d	S )z5Extract content using token ID sequence state machinerQ   r   c                     j         dk    sj         dk    r,| j        j                 k    p| j        j                 k    S | j        j                 k    S )Nr   think)r.   r0   r2   r1   tokenr5   s    r9   check_token_with_sequencezYHunyuanA13BReasoningParser.extract_reasoning_streaming.<locals>.check_token_with_sequence   sg    !V++t/AW/L/LT3D4GHH Q ;D<O PP
  6t7J KKKr:   c                 P   j         dk    sj         dk    rsj        dz
  t          j                  k     r6| j        j        dz
           k    rj        t          j                  k    S j        t          j                  k    S j        t          j                  k    S )Nr   r]   rQ   )r.   r2   rG   r1   r0   r^   s    r9   check_last_tokenzPHunyuanA13BReasoningParser.extract_reasoning_streaming.<locals>.check_last_token   s    !V++t/AW/L/L '!+c$2M.N.NNN!<T=PST=T!UUU.#d6Q2R2RRR.#d6L2M2MMM*c$2H.I.IIIr:   r   r]   r   r   N)r   content)r&   r(   r*   rG   r3   appendr4   r2   r.   r0   r)   r1   r'   r   )r5   rU   rV   rW   rX   rY   rZ   think_start_sequenceresponse_start_sequenceresponse_end_sequencer_   r`   rb   token_in_state_seqbuffered_contents   `              r9   extract_reasoning_streamingz6HunyuanA13BReasoningParser.extract_reasoning_streaming   s>     $3"&"9 $ 5?##q(((("	L 	L 	L 	L 	L	J 	J 	J 	J 	J 76u== /	L$$U+++
*1$  && &%//)0D&-DD*262ND//'722)3D&-BD**':55)/D&-AD*262KD/ '(#$&!#%    LS):%;%;a%?%?#'#3j#@ &'#$&!#%  %00'2BDQQQQ'$@PQQQQ %00'*dKKKK'$
KKKK tr:   )__name__
__module____qualname____doc__r   r   r   intboolr?   listrB   strr   tuplerM   rT   r   rj   __classcell__)r8   s   @r9   r
   r
      s        .%"9 % % % % % %N0(3- 0D 0 0 0 0T#Y 49    ("("*?("	sTz3:%	&(" (" (" ("T
+#C=
+4<SM
+	
+ 
+ 
+ 
+^^ ^ 	^
 %SM^ $C=^ "#^ 
	^ ^ ^ ^ ^ ^ ^ ^r:   r
   )collections.abcr   regexr!   transformersr   0vllm.entrypoints.openai.chat_completion.protocolr   'vllm.entrypoints.openai.engine.protocolr   vllm.loggerr   vllm.reasoningr   rk   loggerr
   rA   r:   r9   <module>r}      s    % $ $ $ $ $     0 0 0 0 0 0      A @ @ @ @ @ # # # # # # * * * * * *	X		] ] ] ] ] ] ] ] ] ]r:   