|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +from collections.abc import Sequence |
| 5 | +from typing import Optional, Union |
| 6 | + |
| 7 | +from transformers import PreTrainedTokenizerBase |
| 8 | + |
| 9 | +from vllm.entrypoints.openai.protocol import (ChatCompletionRequest, |
| 10 | + DeltaMessage) |
| 11 | +from vllm.logger import init_logger |
| 12 | +from vllm.reasoning import ReasoningParser, ReasoningParserManager |
| 13 | + |
| 14 | +logger = init_logger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +@ReasoningParserManager.register_module("GptOss") |
| 18 | +class GptOssReasoningParser(ReasoningParser): |
| 19 | + """ |
| 20 | + Reasoning parser for GptOss model. |
| 21 | +
|
| 22 | + The GptOss model uses harmony to extract reasoning content and this parser |
| 23 | + is only used for detecting the end of the reasoning content. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, tokenizer: PreTrainedTokenizerBase): |
| 27 | + super().__init__(tokenizer) |
| 28 | + self.reasoning_end_token_ids = self.model_tokenizer.encode( |
| 29 | + "<|start|>assistant<|channel|>final<|message|>") |
| 30 | + |
| 31 | + def is_reasoning_end(self, input_ids: list[int]) -> bool: |
| 32 | + end_token_ids = self.reasoning_end_token_ids |
| 33 | + assert len(end_token_ids) > 0, "reasoning_end_token_ids is empty" |
| 34 | + # Check if the end sequence is present in the input_ids. |
| 35 | + # We search from the end of input_ids to find the last match. |
| 36 | + for i in range(len(input_ids) - len(end_token_ids), -1, -1): |
| 37 | + if input_ids[i:i + len(end_token_ids)] == end_token_ids: |
| 38 | + return True |
| 39 | + return False |
| 40 | + |
| 41 | + def extract_content_ids(self, input_ids: list[int]) -> list[int]: |
| 42 | + raise RuntimeError( |
| 43 | + "GptOss model uses harmony to extract reasoning content. This " |
| 44 | + "function should not be called.") |
| 45 | + |
| 46 | + def extract_reasoning_content_streaming( |
| 47 | + self, |
| 48 | + previous_text: str, |
| 49 | + current_text: str, |
| 50 | + delta_text: str, |
| 51 | + previous_token_ids: Sequence[int], |
| 52 | + current_token_ids: Sequence[int], |
| 53 | + delta_token_ids: Sequence[int], |
| 54 | + ) -> Union[DeltaMessage, None]: |
| 55 | + raise RuntimeError( |
| 56 | + "GptOss model uses harmony to extract reasoning content. This " |
| 57 | + "function should not be called.") |
| 58 | + |
| 59 | + def extract_reasoning_content( |
| 60 | + self, model_output: str, request: ChatCompletionRequest |
| 61 | + ) -> tuple[Optional[str], Optional[str]]: |
| 62 | + raise RuntimeError( |
| 63 | + "GptOss model uses harmony to extract reasoning content. This " |
| 64 | + "function should not be called.") |
0 commit comments