identity_reasoning_parser.py 2.3 KB
Newer Older
1
2
3
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

4
from collections.abc import Iterable, Sequence
5
from typing import TYPE_CHECKING
6
7
8

from transformers import PreTrainedTokenizerBase

9
from vllm.entrypoints.openai.engine.protocol import DeltaMessage
10
11
12
from vllm.logger import init_logger
from vllm.reasoning import ReasoningParser

13
14
15
16
if TYPE_CHECKING:
    from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest
    from vllm.entrypoints.openai.responses.protocol import ResponsesRequest

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
logger = init_logger(__name__)


class IdentityReasoningParser(ReasoningParser):
    """
    Identity reasoning parser.

    This parser does not attempt to parse or strip out reasoning tokens.
    It treats the entire model output as content and ignores reasoning.
    """

    def __init__(self, tokenizer: PreTrainedTokenizerBase, *args, **kwargs):
        super().__init__(tokenizer, *args, **kwargs)
        if not self.model_tokenizer:
            raise ValueError(
                "The model tokenizer must be passed to the ReasoningParser "
                "constructor during construction."
            )

36
    def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
37
38
39
        # Always return True, since we never treat reasoning specially
        return True

40
    def is_reasoning_end_streaming(
41
        self, input_ids: Sequence[int], delta_ids: Iterable[int]
42
43
44
    ) -> bool:
        return True

45
46
47
48
    def extract_content_ids(self, input_ids: list[int]) -> list[int]:
        # Identity: return all tokens as content
        return input_ids

49
    def extract_reasoning_streaming(
50
51
52
53
54
55
56
57
58
59
60
61
62
        self,
        previous_text: str,
        current_text: str,
        delta_text: str,
        previous_token_ids: Sequence[int],
        current_token_ids: Sequence[int],
        delta_token_ids: Sequence[int],
    ) -> DeltaMessage | None:
        # Just wrap delta_text as content, ignore reasoning
        if delta_text:
            return DeltaMessage(content=delta_text)
        return None

63
    def extract_reasoning(
64
        self, model_output: str, request: "ChatCompletionRequest | ResponsesRequest"
65
    ) -> tuple[str | None, str | None]:
66
        # No reasoning separation: return None for reasoning,
67
68
        # and full model_output as content
        return None, model_output