o
    -i%                     @   sl   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	 d	eZdS )
    )SequenceN)PreTrainedTokenizerBase)ChatCompletionRequest)DeltaMessage)init_logger)ReasoningParserc                       s   e Zd ZdZdef fddZdee defddZ	de
e de
e fd	d
ZdededeedB edB f fddZdee dee defddZdedededee dee dee dedB fddZ  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                    s   t  j|g|R i | d| _d| _d| _d| _td| j d| j d| j tj| _	t| 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__ i/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/reasoning/hunyuan_a13b_reasoning_parser.pyr   +   s8   







z#HunyuanA13BReasoningParser.__init__	input_idsreturnc                 C   s
   | j dkS )Nr   )r+   r2   r9   r7   r7   r8   is_reasoning_endR   s   
z+HunyuanA13BReasoningParser.is_reasoning_endc                 C   s   g S )Nr7   r;   r7   r7   r8   extract_content_idsU   s   z.HunyuanA13BReasoningParser.extract_content_idsmodel_outputrequestNc                 C   s   | j |}|r"|d \}}t|dkrd}t|dkrd}||fS | j}||}|rV|d \}}|| jrB|dt| j  }t|dkrJd}t|dkrRd}||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   )r2   r>   r?   re_matchr   response_contentfallback_regexfallback_matchr7   r7   r8   extract_reasoning\   s(   
z,HunyuanA13BReasoningParser.extract_reasoningsubsequencesequencec                 C   sB   |sdS d}|D ]}|t |k r||| kr|d7 }q|t |kS )NFr      )rA   )r2   rH   rI   sub_idxnumr7   r7   r8   !_is_strict_increasing_subsequence   s   z<HunyuanA13BReasoningParser._is_strict_increasing_subsequenceprevious_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsc                    s^   j } j} j}	t|dksJ |d }
 fdd} fdd}||
}|ru j|
   j|7  _  jd7  _||
rs jdkrOd _| _	 j
 _n jdkr[d	 _|	 _	n jd	krjd _| _	 j _d _g  _d
 _dS  jrt jdkr j| }d _g  _d
 _ jdkrt|ddS td|dS  jdkrt|ddS td|dS )z5Extract content using token ID sequence state machinerJ   r   c                    sD    j dks
 j dkr|  j j kp|  j j kS |  j j kS )Nr   think)r+   r-   r/   r.   tokenr2   r7   r8   check_token_with_sequence   s
   zYHunyuanA13BReasoningParser.extract_reasoning_streaming.<locals>.check_token_with_sequencec                    sl    j dks
 j dkr. jd t jk r&|  j jd  kr& jt jkS  jt jkS  jt jkS )Nr   rT   rJ   )r+   r/   rA   r.   r-   rU   rW   r7   r8   check_last_token   s   zPHunyuanA13BReasoningParser.extract_reasoning_streaming.<locals>.check_last_tokenr   rT   r   r   N)r   content)r#   r%   r'   rA   r0   appendr1   r/   r+   r-   r&   r.   r$   r   )r2   rN   rO   rP   rQ   rR   rS   think_start_sequenceresponse_start_sequenceresponse_end_sequencerV   rX   rY   token_in_state_seqbuffered_contentr7   rW   r8   extract_reasoning_streaming   sN   	






z6HunyuanA13BReasoningParser.extract_reasoning_streaming)__name__
__module____qualname____doc__r   r   r   intboolr<   listr=   strr   tuplerG   rM   r   ra   __classcell__r7   r7   r5   r8   r      sD    '
*
r   )collections.abcr   regexr   transformersr   0vllm.entrypoints.openai.chat_completion.protocolr   'vllm.entrypoints.openai.engine.protocolr   vllm.loggerr   vllm.reasoningr   rb   loggerr   r7   r7   r7   r8   <module>   s   