
    .`iu+              	          d dl Zd dlZd dlmZ d dlmZ d dlZer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 d d	lmZ  ee          Z G d
 dej                  Z ej        d           G d d                      Zdededeedz  edz  f         fdZej         G d d                      Z G d de          ZdS )    N)Sequence)TYPE_CHECKING)TokenizerLike)ChatCompletionRequest)DeltaMessage)ResponsesRequest)init_logger)ReasoningParserc                       e Zd ZdZdZdS )Olmo3ReasoningState      N)__name__
__module____qualname__	REASONINGCONTENT     y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/reasoning/olmo3_reasoning_parser.pyr   r      s        IGGGr   r   T)frozenc                   *    e Zd ZU eed<   eed<   d ZdS )Indicesstartendc                      | j         | j        z
  S N)r   r   selfs    r   __len__zIndices.__len__&   s    x$*$$r   N)r   r   r   int__annotations__r    r   r   r   r   r   !   s7         JJJ	HHH% % % % %r   r   abreturnc                 T   t          |           t          |          k     r| |dfn|| df\  } }}| |v rmt          dt          |                     }t          |                    |           |                    |           t          |           z             }|r||fn||fS t          t          |           dz
  dd          D ]b}| | d         |d|         k    rIt          t          |           |z
  t          |                     }t          d|          }|r||fn||fc S ct          t          |           dz
  dd          D ]b}|| d         | d|         k    rIt          d|          }t          t          |          |z
  t          |                    }|r||fn||fc S cdS )a.  
    Find the longest overlap where the end of string a matches the start
    of string b.

    Args:
        a: First string
        b: Second string

    Returns:
        Tuple of IndicesTuples representing the overlapping portions in each
        string, or a tuple of None if no overlap exists
    FTr   r   N)NN)lenr   indexrange)r#   r$   swapind_aind_bis         r   string_overlapr/   *   s    #&a&&3q66//!Q1d|JAq$ 	Avv3q66""

AGGAJJQ$788!%9u~~E5>9 3q66A:q"%% > >aRSS6QrrU??CFFQJA//EAqMME%)=E5>>u~===  3q66A:q"%% > >aRSS6QrrU??AqMMECFFQJA//E%)=E5>>u~=== 
 :r   c                       e Zd ZU dZeed<   dZeed<   dZeed<   ej	        Z
eed<   ded	z  fd
Zd Zdeded	z  fdZd	S )Olmo3ReasoningBuffer<think>think_start</think>	think_end bufferstater%   Nc                    | j                             | j                  }|dk    r`t          j        | _        | j         d |         | j         |t          | j                  z   d          c}| _         |dk    rt          |          S | j                             | j	                  }|dk    r`t          j
        | _        | j         d |         | j         |t          | j	                  z   d          c}| _         |dk    rt          |          S | j        t          j        k    r| j         dc}| _         t          |          S | j        t          j
        k    r| j         dc}| _         t          |          S d S )Nr   )content)	reasoningr6   )r7   findr3   r   r   r8   r(   r   rfindr5   r   )r   start_think_idxpretextend_think_idxtext_buffers        r   process_bufferz#Olmo3ReasoningBuffer.process_buffer`   s   +**4+;<<a,6DJ,_,-Oc$2B.C.CCEEF !GT[ "" $G4444))$.99A,4DJN]N+MC,?,??AAB !GT[ q   $g6666:,666 R+6666:,444 R4444 tr   c                 *    t          | j                  S r   )r(   r7   r   s    r   r    zOlmo3ReasoningBuffer.__len__   s    4;r   
delta_textc                    | xj         |z  c_         d }t          || j                  \  }}t          || j                  \  }}|d uo$t	          |          t	          | j                  k     }|d uo$t	          |          t	          | j                  k     }|r%| j        | j         v r|s|                                 }n?|r#| j        | j         v r|                                 }n|s|rd S |                                 }|S r   )r7   r/   r3   r5   r(   rB   )r   rD   delta_message_overlap_think_startoverlap_think_endpartial_overlap_startpartial_overlap_ends           r   add_textzOlmo3ReasoningBuffer.add_text   sG   z! .2 "0
D<L!M!M-j$.II 34 ? !"CE
 E
 !!E" 0t;  A
 A
A 
 "	2 DK//' 0 !//11MM  	2T^t{%B%B !//11MM" 	2&9 	2 4 !//11Mr   )r   r   r   r3   strr"   r5   r7   r   r   r8   r   rB   r    rL   r   r   r   r1   r1   T   s          K   IsFC "5!>E>>>.t 3 . . . .`     /3 /<$+> / / / / / /r   r1   c                        e Zd ZdZd fdZdee         defdZde	e         de	e         fdZ
d	ed
eez  deedz  edz  f         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 )Olmo3ReasoningParsera  
    Reasoning parser for Olmo 3 model

    Olmo3ReasoningParser

    This class implements a reasoning parser specifically designed for the
    Olmo 3 family of models. Olmo 3 models do not use special tokens to
    indicate reasoning; rather, reasoning trace is wrapped in `<think>` and
    `</think>`, which are tokenized using standard vocabulary entries.
    Because of this, the parser operates in string space, accumulating the
    characters in a buffer until it sees `<think>` or `</think>`. tokens
    to switch modes.

    Key Features:
        - For non-stream output, Recognizes and extracts reasoning (text
          bracketed by `<think>` and `</think>`) and content (everything
          after the first `</think>`).
        - For stream process, it uses a buffer to accumulate delta text,
          and output progressive delta messages as soon as thinking starts
          or ends.
        - For reliability, some Olmo 3 models may hardcode the first
          `<think>` token is the input text (similar to Deepseek R1,
          or reasoning-only Qwen models). To support such variants, the
          parser can optionally work in cases where the first `<think>`
          token is missing from generation.
    	tokenizerr   c                     t                      j        |g|R i | d| _        d| _        d| j         d| j         dz   }t	          j        |t          j                  | _        t          | j        | j                  | _	        d S )Nr2   r4   z^(?:z)?(?P<reasoning>.*?)z(?P<content>.*)$)r3   r5   )
super__init__r3   r5   recompileDOTALLreasoning_regexr1   r7   )r   rP   argskwargsreasoning_expr	__class__s        r   rS   zOlmo3ReasoningParser.__init__   s    4T444V444%$ ;D$:::.2223 	  "z.")DD*(DN
 
 
r   	input_idsr%   c                 H    | j                             |          }| j        |v S r   )model_tokenizerdecoder5   )r   r\   texts      r   is_reasoning_endz%Olmo3ReasoningParser.is_reasoning_end   s%    #**955~%%r   c                     g S r   r   )r   r\   s     r   extract_content_idsz(Olmo3ReasoningParser.extract_content_ids   s	    
 	r   model_outputrequestNc                     | j                             |          }|r2|                    d          pd}|                    d          pd}||fS d|fS )a1  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 | ResponsesRequest): Request being
                processed.

        Returns:
            tuple[Optional[str], Optional[str]]: Tuple pair containing the
            reasoning content and non-reasoning content.
        r;   Nr:   )rW   matchgroup)r   rd   re   re_matchr;   r:   s         r   extract_reasoningz&Olmo3ReasoningParser.extract_reasoning   sf    & '--l;; 	& {33;tInnY//74Gg%% \!!r   previous_textcurrent_textrD   previous_token_idscurrent_token_idsdelta_token_idsc                     | j                             |          }|1| j         j        | j         j         v r| j                                         }|S )z5Extract content using token ID sequence state machine)r7   rL   r5   rB   )r   rk   rl   rD   rm   rn   ro   rF   s           r   extract_reasoning_streamingz0Olmo3ReasoningParser.extract_reasoning_streaming  sN     ,,Z88 T[%:dk>P%P%P !K6688Mr   )rP   r   )r   r   r   __doc__rS   r   r!   boolra   listrc   rM   r   r   tuplerj   r   rq   __classcell__)r[   s   @r   rO   rO      sH        6
 
 
 
 
 
&&(3- &D & & & &T#Y 49    "" ')99" 
sTz3:%	&	" " " "8  	
 %SM $C= "# 
	       r   rO   ) dataclassesdtenumcollections.abcr   typingr   regexrT   vllm.tokenizersr   0vllm.entrypoints.openai.chat_completion.protocolr   'vllm.entrypoints.openai.engine.protocolr   *vllm.entrypoints.openai.responses.protocolr   vllm.loggerr	   vllm.reasoningr
   r   loggerEnumr   	dataclassr   rM   ru   r/   r1   rO   r   r   r   <module>r      s        $ $ $ $ $ $                 .------                $ # # # # # * * * * * *	X		    $)   
 T% % % % % % % %'c 'c 'eGdNGdN,J&K ' ' ' 'T n n n n n n n nbk k k k k? k k k k kr   