
    .`i                         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 d dlmZ d dlmZ  e	e          Z G d	 d
e          ZdS )    )Sequence)cached_property)ChatCompletionRequest)ResponsesRequest)init_logger)ReasoningParser)BaseThinkingReasoningParser)MistralTokenizerc            	           e Zd ZdZdefdZedefd            Zede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S )MistralReasoningParsera  
    Reasoning parser for Mistral models.

    The Mistral models uses `[THINK]`...`[/THINK]` tokens to denote reasoning
    text. This parser extracts the reasoning content from the model output.

    A valid reasoning trace should always start with a `[THINK]` token and end with
    a `[/THINK]` token.

    If `[THINK]` token is not generated, then this parser only returns content.
    	tokenizerc                 p   t          |t                    st          d          t          j        | |g|R i | | j        st          d          |j                            | j                  | _	        |j                            | j
                  | _        | j	        | j        t          d          d S )Nz6The tokenizer must be an instance of MistralTokenizer.zZThe model tokenizer must be passed to the ReasoningParser constructor during construction.zRMistral reasoning parser could not locate think start/end tokens in the tokenizer!)
isinstancer
   
ValueErrorr   __init__model_tokenizerr   get_control_tokenstart_tokenstart_token_id	end_tokenend_token_idRuntimeError)selfr   argskwargss       {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/reasoning/mistral_reasoning_parser.pyr   zMistralReasoningParser.__init__"   s    )%566 	WUVVV yB4BBB6BBB# 	3  
 (1CCDDTUU%/AA$.QQ&$*;*C+   +D*C    returnc                     ddl m} |j        S )z(The token that starts reasoning content.r   SpecialTokens)%mistral_common.tokens.tokenizers.baser!   begin_thinkr   r!   s     r   r   z"MistralReasoningParser.start_token7   s      	HGGGGG((r   c                     ddl m} |j        S )z&The token that ends reasoning content.r   r    )r"   r!   	end_thinkr$   s     r   r   z MistralReasoningParser.end_token>   s      	HGGGGG&&r   	input_idsc                 ^    d}|d d d         D ]}|| j         k    r|c S || j        k    rd}dS )NFT)r   r   )r   r'   has_eot_tokenids       r   is_reasoning_endz'MistralReasoningParser.is_reasoning_endE   sS    DDbD/ 	% 	%BT((($$$$t((( $ur   c                    d}d}d}d}t          |          D ](\  }}|| j        k    r|sd}|}|| j        k    rd}|} n)|r|s
|d|         S |s|s|S |r|r|d|         ||dz   d         z   S |d|         ||dz   d         z   S )z%
        Extract the content
        Fr)   TN   )	enumerater   r   )r   r'   has_bot_tokenr*   bot_token_indexeot_token_indexitoken_ids           r   extract_content_idsz*MistralReasoningParser.extract_content_idsP   s    $Y// 		 		KAx 4...}. $"#T... $"# /  	R 	R-o-.. 	R} 	R 	R} 	R-o-.?Q;N;P;P1QQQ -o-.?Q;N;P;P1QQQr   model_outputrequestNc                 T   |sdS |                     | j                  \  }}}t          |          }|o| j        |v }|r-|r+|                     | j                  \  }}	}
||
z   }||r|ndfS |r||r|ndfS | j        |v }|r%|                     | j                  \  }}	}
d||
z   fS d|fS )zB
        Extract reasoning content from the model output.
        )N N)	partitionr   boolr   )r   r6   r7   prev_bot_token	bot_tokenpost_bot_tokenr0   has_valid_eot_tokenprev_eot_token_post_eot_tokencontenthas_non_valid_eot_tokens                r   extract_reasoningz(MistralReasoningParser.extract_reasonings   s     	: 5A4J4J5
 5
1	> Y+P.0P  	,0 	,0>0H0H0X0X-NA~$~5G!g#?774?? 	,!^#M>>MM '+n&F# ' ,4B4L4LN5 51> ^n<<< ^++r   )__name__
__module____qualname____doc__r
   r   r   strr   r   r   intr;   r,   listr5   r   r   tuplerE    r   r   r   r      s#       
 
"2    * )S ) ) ) _) '3 ' ' ' _'	(3- 	D 	 	 	 	!RT#Y !R49 !R !R !R !RF*,*,*?BR*R*,	sTz3:%	&*, *, *, *, *, *,r   r   N)collections.abcr   	functoolsr   0vllm.entrypoints.openai.chat_completion.protocolr   *vllm.entrypoints.openai.responses.protocolr   vllm.loggerr   vllm.reasoningr   vllm.reasoning.basic_parsersr	   vllm.tokenizers.mistralr
   rF   loggerr   rN   r   r   <module>rX      s    % $ $ $ $ $ % % % % % %           $ # # # # # * * * * * * D D D D D D 4 4 4 4 4 4	X		H, H, H, H, H,8 H, H, H, H, H,r   