identity_reasoning_parser.py 2.46 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
37
38
39
40
41
42
43
    @property
    def reasoning_start_str(self) -> str | None:
        return None

    @property
    def reasoning_end_str(self) -> str | None:
        return None

44
    def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
45
46
47
        # Always return True, since we never treat reasoning specially
        return True

48
    def is_reasoning_end_streaming(
49
        self, input_ids: Sequence[int], delta_ids: Iterable[int]
50
51
52
    ) -> bool:
        return True

53
54
55
56
    def extract_content_ids(self, input_ids: list[int]) -> list[int]:
        # Identity: return all tokens as content
        return input_ids

57
    def extract_reasoning_streaming(
58
59
60
61
62
63
64
65
66
67
68
69
70
        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

71
    def extract_reasoning(
72
        self, model_output: str, request: "ChatCompletionRequest | ResponsesRequest"
73
    ) -> tuple[str | None, str | None]:
74
        # No reasoning separation: return None for reasoning,
75
76
        # and full model_output as content
        return None, model_output