o
    )i`                     @   sz   d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZ eeZedG dd	 d	eZd
S )    )Sequence)OptionalUnion)PreTrainedTokenizerBase)ChatCompletionRequestDeltaMessage)init_logger)ReasoningParserReasoningParserManagerZqwen3c                       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dee dee dee deedf fddZdededeee ee f fddZ  ZS )Qwen3ReasoningParserau  
    Reasoning parser for the Qwen3 model.

    The Qwen3 model uses <think>...</think> tokens to denote reasoning text
    within its output. The model provides a strict switch to disable reasoning
    output via the 'enable_thinking=False' parameter. This parser extracts the
    reasoning content enclosed by <think> and </think> tokens from the model's
    output.
    	tokenizerc                    sf   t  | d| _d| _| jstd| j| j| _| j| j| _	| jd u s-| j	d u r1t
dd S )Nz<think>z</think>zZThe model tokenizer must be passed to the ReasoningParser constructor during construction.zPQwen3 reasoning parser could not locate think start/end tokens in the tokenizer!)super__init__think_start_tokenthink_end_tokenZmodel_tokenizer
ValueErrorZvocabgetthink_start_token_idthink_end_token_idRuntimeError)selfr   	__class__ q/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/reasoning/qwen3_reasoning_parser.pyr      s   

zQwen3ReasoningParser.__init__	input_idsreturnc                 C   s
   | j |v S )N)r   r   r   r   r   r   is_reasoning_end/   s   
z%Qwen3ReasoningParser.is_reasoning_endc                 C   s.   | j |dd vrg S ||| j d d S )z:
        Extract the content after the end tokens
        N   )r   indexr   r   r   r   extract_content_ids2   s   z(Qwen3ReasoningParser.extract_content_idsprevious_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsNc                 C   s   t |dkr|d | j| jfv rdS | j|v rN| j|v r?|| j}|d| }||t | j d }	t||	r;|	dS ddS | j|v rIt|dS t|dS | j|v r| j|v r|| j}
|| j}||
t | j | }||t | j d }	t||	r|	dS ddS t|dS t|dS )a$  
        Extract reasoning content from a delta message.
        Handles streaming output where previous + delta = current.
        Uses token IDs for faster processing.
        For text <think>abc</think>xyz:
        - 'abc' goes to reasoning_content
        - 'xyz' goes to content
        r    r   N)reasoning_contentcontent)r*   )r)   )lenr   r   findr   r   r   )r   r#   r$   r%   r&   r'   r(   Z	end_indexr)   r*   start_indexr   r   r   #extract_reasoning_content_streaming;   sD   








z8Qwen3ReasoningParser.extract_reasoning_content_streamingmodel_outputrequestc                 C   st   | j |vs
| j|vrd|fS || j }|d r|d n|d }| j|vr)d|fS || j\}}}|p5d}||fS )a  
        Extract reasoning content from the model output.

        For text <think>abc</think>xyz:
        - 'abc' goes to reasoning_content
        - 'xyz' goes to content

        Returns:
            tuple[Optional[str], Optional[str]]: reasoning content and content
        Nr       r   )r   r   	partition)r   r/   r0   Zmodel_output_partsr)   _r*   Zfinal_contentr   r   r   extract_reasoning_contentv   s    



z.Qwen3ReasoningParser.extract_reasoning_content)__name__
__module____qualname____doc__r   r   listintboolr   r"   strr   r   r   r.   r   tupler   r4   __classcell__r   r   r   r   r      s6    
	

;r   N)collections.abcr   typingr   r   Ztransformersr   Z vllm.entrypoints.openai.protocolr   r   Zvllm.loggerr   Zvllm.reasoningr	   r
   r5   loggerZregister_moduler   r   r   r   r   <module>   s   