o
    
۾i                     @   s   d dl m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 er5d dlmZ d dlmZ neZeZG d	d
 d
e
ZdS )    )abstractmethod)Sequence)TYPE_CHECKINGAny)DeltaMessage)ReasoningParser)TokenizerLike)ChatCompletionRequest)ResponsesRequestc                       s   e Zd ZdZeedefddZeedefddZde	f fdd	Z
d
ee defddZd
ee 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dB fddZdedeeB deedB edB f fddZ  ZS )BaseThinkingReasoningParseraU  
    Base class for reasoning parsers that use thinking tokens.

    This class provides common functionality for parsers that use start and end
    tokens to delimit reasoning content (
        e.g., <think>...</think>, <seed:think>...</seed:think>).

    Subclasses must implement the start and end tokens via abstract
    properties.
    returnc                 C      t )z(The token that starts reasoning content.NotImplementedErrorself r   P/home/ubuntu/.local/lib/python3.10/site-packages/vllm/reasoning/basic_parsers.pystart_token$      z'BaseThinkingReasoningParser.start_tokenc                 C   r   )z&The token that ends reasoning content.r   r   r   r   r   	end_token*   r   z%BaseThinkingReasoningParser.end_token	tokenizerc                    s   t  j|g|R i | | jstd| jr| jstd| j| j| _| j| j| _	| jd u s8| j	d u rAt
| jj dd S )NzZThe model tokenizer must be passed to the ReasoningParser constructor during construction.z7start_token and end_token must be defined in subclasseszK reasoning parser could not locate think start/end tokens in the tokenizer!)super__init__model_tokenizer
ValueErrorr   r   vocabgetstart_token_idend_token_idRuntimeError	__class____name__)r   r   argskwargsr!   r   r   r   0   s   z$BaseThinkingReasoningParser.__init__	input_idsc                 C   sN   | j }| j}tt|d ddD ]}|| |kr dS || |kr$ dS qdS )N   FT)r   r   rangelen)r   r&   r   r   ir   r   r   is_reasoning_endD   s   z,BaseThinkingReasoningParser.is_reasoning_end	delta_idsc                 C   s   | j }||v S )N)r   )r   r&   r-   r   r   r   r   is_reasoning_end_streamingO   s   z6BaseThinkingReasoningParser.is_reasoning_end_streamingc                 C   s.   | j |dd vrg S ||| j d d S )z:
        Extract the content after the end tokens
        Nr(   r'   )r   index)r   r&   r   r   r   extract_content_idsU   s   z/BaseThinkingReasoningParser.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 )z
        Extract reasoning content from a delta message.
        Handles streaming output where previous + delta = current.
        Uses token IDs for faster processing.
        r'   r   N)	reasoningcontent)r8   )r7   )r*   r   r   findr   r   r   )r   r1   r2   r3   r4   r5   r6   	end_indexr7   r8   start_indexr   r   r   extract_reasoning_streaming^   s:   








z7BaseThinkingReasoningParser.extract_reasoning_streamingmodel_outputrequestc                 C   sX   | | j}|d r|d n|d }| j|vr|dfS | | j\}}}|p'd}||fS )z
        Extract reasoning content from the model output.

        This is the base implementation that works for most models.
        Subclasses can override this method for specific behavior.
        r'      r   N)	partitionr   r   )r   r=   r>   model_output_partsr7   _r8   final_contentr   r   r   extract_reasoning   s   
z-BaseThinkingReasoningParser.extract_reasoning)r"   
__module____qualname____doc__propertyr   strr   r   r   r   r   intboolr,   r.   listr0   r   r<   r	   r
   tuplerD   __classcell__r   r   r%   r   r      sP    
	
;r   N)abcr   collections.abcr   typingr   r   'vllm.entrypoints.openai.engine.protocolr   $vllm.reasoning.abs_reasoning_parsersr   vllm.tokenizersr   0vllm.entrypoints.openai.chat_completion.protocolr	   *vllm.entrypoints.openai.responses.protocolr
   r   r   r   r   r   <module>   s   