
    .`i                         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rd dlmZ d dlmZ neZeZ G d	 d
e
          ZdS )    )abstractmethod)Sequence)TYPE_CHECKINGAny)DeltaMessage)ReasoningParser)TokenizerLike)ChatCompletionRequest)ResponsesRequestc                   t    e Zd ZdZeedefd                        Zeedefd                        Zde	f fdZ
dee         defdZdee         d	ee         defd
Zdee         de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dedeez  deedz  edz  f         fdZ x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                     t           )z(The token that starts reasoning content.NotImplementedErrorselfs    p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/reasoning/basic_parsers.pystart_tokenz'BaseThinkingReasoningParser.start_token$   
     "!    c                     t           )z&The token that ends reasoning content.r   r   s    r   	end_tokenz%BaseThinkingReasoningParser.end_token*   r   r   	tokenizerc                     t                      j        |g|R i | | j        st          d          | j        r| j        st          d          | j                            | j                  | _        | j                            | j                  | _	        | j        | j	        t          | 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kwargsr%   s       r   r   z$BaseThinkingReasoningParser.__init__0   s    4T444V444# 	3  
  	Xt~ 	XVWWW"jnnT-=>> JNN4>::&$*;*C>* ; ; ;   +D*Cr   	input_idsc                     | j         }| j        }t          t          |          dz
  dd          D ] }||         |k    r dS ||         |k    r dS !dS )N   FT)r"   r#   rangelen)r   r)   r"   r#   is        r   is_reasoning_endz,BaseThinkingReasoningParser.is_reasoning_endD   sn    ,(s9~~)2r22 	 	A|~--uu||++tt ,ur   	delta_idsc                     | j         }||v S )N)r#   )r   r)   r1   r#   s       r   is_reasoning_end_streamingz6BaseThinkingReasoningParser.is_reasoning_end_streamingO   s     (y((r   c                 r    | j         |dd         vrg S ||                    | j                   dz   d         S )z:
        Extract the content after the end tokens
        Nr,   r+   )r#   index)r   r)   s     r   extract_content_idsz/BaseThinkingReasoningParser.extract_content_idsU   sD     IcrcN22IY__T->??!CEEFFr   previous_textcurrent_text
delta_textprevious_token_idscurrent_token_idsdelta_token_idsNc                    t          |          dk    r|d         | j        | j        fv rdS | j        |v r| j        |v rX|                    | j                  }|d|         }||t          | j                  z   d         }	t          ||	r|	nd          S | j        |v rt          |          S t          |          S | j        |v r| j        |v r|                    | j                  }
|                    | j                  }||
t          | j                  z   |         }||t          | j                  z   d         }	t          ||	r|	nd          S t          |          S t          |          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)r?   )r>   )r.   r"   r#   findr   r   r   )r   r7   r8   r9   r:   r;   r<   	end_indexr>   r?   start_indexs              r   extract_reasoning_streamingz7BaseThinkingReasoningParser.extract_reasoning_streaming^   s    1$$A4#68I"JJJ4 "444 O33 'OODN;;	&z	z2	$YT^1D1D%D%F%FG#'G1M    "&888 $J7777 $j9999 O33 O33 )ood.>??&OODN;;	&{S9I5J5J'JY'VW	$YT^1D1D%D%F%FG#'G1M    $j9999  
3333r   model_outputrequestc                     |                     | 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   rD   rE   model_output_partsr>   _r?   final_contents           r   extract_reasoningz-BaseThinkingReasoningParser.extract_reasoning   s     *33D4DEE%7%:Uq!!@RST@U 	 >--%%$0$:$:4>$J$J!Iq'#OtMm++r   )r&   
__module____qualname____doc__propertyr   strr   r   r	   r   r   intboolr0   r3   listr6   r   rC   r
   r   tuplerL   __classcell__)r%   s   @r   r   r      s       	 	 "S " " " ^ X" "3 " " " ^ X"-      (	(3- 	D 	 	 	 	)!#)3;C=)	) ) ) )GT#Y G49 G G G G9494 94 	94
 %SM94 $C=94 "#94 
	94 94 94 94v,,*?BR*R,	sTz3:%	&, , , , , , , ,r   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   <module>r`      s         $ $ $ $ $ $ % % % % % % % % @ @ @ @ @ @ @ @ @ @ @ @ ) ) ) ) ) ) 	             Y, Y, Y, Y, Y,/ Y, Y, Y, Y, Y,r   