o
    -i                     @   s|   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
 d
eZdS )    )Sequence)cached_property)ChatCompletionRequest)ResponsesRequest)init_logger)ReasoningParser)BaseThinkingReasoningParser)MistralTokenizerc                	   @   s   e Zd ZdZdefddZedefddZede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eB deedB edB f fd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                 O   sz   t |ts	tdtj| |g|R i | | jstd|j| j| _	|j| j
| _| j	d u s7| jd u r;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kwargs r   d/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/reasoning/mistral_reasoning_parser.pyr   "   s   
zMistralReasoningParser.__init__returnc                 C      ddl m} |jS )z(The token that starts reasoning content.r   SpecialTokens)%mistral_common.tokens.tokenizers.baser   begin_thinkr   r   r   r   r   r   7      z"MistralReasoningParser.start_tokenc                 C   r   )z&The token that ends reasoning content.r   r   )r   r   	end_thinkr!   r   r   r   r   >   r"   z MistralReasoningParser.end_token	input_idsc                 C   s<   d}|d d d D ]}|| j kr|  S || jkrd}q	dS )NFT)r   r   )r   r$   has_eot_tokenidr   r   r   is_reasoning_endE   s   

z'MistralReasoningParser.is_reasoning_endc                 C   s   d}d}d}d}t |D ]\}}|| jkr|sd}|}q|| jkr'd}|} nq|r2|s2|d| S |s8|s8|S |rJ|rJ|d| ||d d  S |d| ||d d  S )z%
        Extract the content
        Fr%   TN   )	enumerater   r   )r   r$   has_bot_tokenr&   bot_token_indexeot_token_indexitoken_idr   r   r   extract_content_idsP   s(   
z*MistralReasoningParser.extract_content_idsmodel_outputrequestNc                 C   s   |sdS | | j\}}}t|}|o| j|v }|r2|r2| | j\}}	}
||
 }||r/|fS dfS |r=||r:|fS dfS | j|v }|rS| | j\}}	}
d||
 fS d|fS )zB
        Extract reasoning content from the model output.
        )N N)	partitionr   boolr   )r   r1   r2   prev_bot_token	bot_tokenpost_bot_tokenr+   has_valid_eot_tokenprev_eot_token_post_eot_tokencontenthas_non_valid_eot_tokenr   r   r   extract_reasonings   s(   


z(MistralReasoningParser.extract_reasoning)__name__
__module____qualname____doc__r	   r   r   strr   r   r   intr5   r(   listr0   r   r   tupler?   r   r   r   r   r
      s     #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	   r@   loggerr
   r   r   r   r   <module>   s   