|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Baidu, Inc. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# This file is a part of the vllm-kunlun project. |
| 16 | +# |
| 17 | +"""Reasoning parser for MiniMax-M2.7 model.""" |
| 18 | + |
| 19 | +from collections.abc import Sequence |
| 20 | +from typing import TYPE_CHECKING |
| 21 | + |
| 22 | +from vllm.entrypoints.openai.engine.protocol import DeltaMessage |
| 23 | +from vllm.reasoning.basic_parsers import BaseThinkingReasoningParser |
| 24 | + |
| 25 | +if TYPE_CHECKING: |
| 26 | + from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest |
| 27 | + from vllm.entrypoints.openai.responses.protocol import ResponsesRequest |
| 28 | + from vllm.tokenizers import TokenizerLike |
| 29 | + |
| 30 | + |
| 31 | +class MiniMaxM2ReasoningParser(BaseThinkingReasoningParser): |
| 32 | + """ |
| 33 | + Reasoning parser for the MiniMax-M2.7 model. |
| 34 | +
|
| 35 | + MiniMax-M2.7 uses <think>...</think> tokens to denote reasoning text, |
| 36 | + similar to Qwen3. This parser handles the extraction of reasoning |
| 37 | + content from model outputs. |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self, tokenizer: "TokenizerLike", *args, **kwargs): |
| 41 | + super().__init__(tokenizer, *args, **kwargs) |
| 42 | + chat_kwargs = kwargs.get("chat_template_kwargs", {}) or {} |
| 43 | + self.thinking_enabled = chat_kwargs.get("enable_thinking", True) |
| 44 | + |
| 45 | + @property |
| 46 | + def start_token(self) -> str: |
| 47 | + return "<think>" |
| 48 | + |
| 49 | + @property |
| 50 | + def end_token(self) -> str: |
| 51 | + return "</think>" |
| 52 | + |
| 53 | + def extract_reasoning( |
| 54 | + self, model_output: str, request: "ChatCompletionRequest | ResponsesRequest" |
| 55 | + ) -> tuple[str | None, str | None]: |
| 56 | + model_output_parts = model_output.partition(self.start_token) |
| 57 | + model_output = ( |
| 58 | + model_output_parts[2] if model_output_parts[1] else model_output_parts[0] |
| 59 | + ) |
| 60 | + |
| 61 | + if self.end_token not in model_output: |
| 62 | + if not self.thinking_enabled: |
| 63 | + return None, model_output |
| 64 | + return model_output, None |
| 65 | + |
| 66 | + reasoning, _, content = model_output.partition(self.end_token) |
| 67 | + return reasoning, content or None |
| 68 | + |
| 69 | + def extract_reasoning_streaming( |
| 70 | + self, |
| 71 | + previous_text: str, |
| 72 | + current_text: str, |
| 73 | + delta_text: str, |
| 74 | + previous_token_ids: Sequence[int], |
| 75 | + current_token_ids: Sequence[int], |
| 76 | + delta_token_ids: Sequence[int], |
| 77 | + ) -> DeltaMessage | None: |
| 78 | + if not self.thinking_enabled: |
| 79 | + return DeltaMessage(content=delta_text) if delta_text else None |
| 80 | + |
| 81 | + if self.start_token_id in delta_token_ids: |
| 82 | + start_idx = delta_text.find(self.start_token) |
| 83 | + if start_idx >= 0: |
| 84 | + delta_text = delta_text[start_idx + len(self.start_token) :] |
| 85 | + |
| 86 | + if self.end_token_id in delta_token_ids: |
| 87 | + end_index = delta_text.find(self.end_token) |
| 88 | + if end_index >= 0: |
| 89 | + reasoning = delta_text[:end_index] |
| 90 | + content = delta_text[end_index + len(self.end_token) :] |
| 91 | + if not reasoning and not content: |
| 92 | + return None |
| 93 | + return DeltaMessage( |
| 94 | + reasoning=reasoning if reasoning else None, |
| 95 | + content=content if content else None, |
| 96 | + ) |
| 97 | + return None |
| 98 | + |
| 99 | + if not delta_text: |
| 100 | + return None |
| 101 | + elif self.end_token_id in previous_token_ids: |
| 102 | + return DeltaMessage(content=delta_text) |
| 103 | + else: |
| 104 | + return DeltaMessage(reasoning=delta_text) |
0 commit comments