protocol.py 2.75 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
from pathlib import Path
4
5
6
from typing import TYPE_CHECKING, Any, Protocol

if TYPE_CHECKING:
7
8
    from transformers import BatchEncoding

9
10
11
12
13
14
15
    from vllm.entrypoints.chat_utils import ChatCompletionMessageParam


class TokenizerLike(Protocol):
    @classmethod
    def from_pretrained(
        cls,
16
17
18
        path_or_repo_id: str | Path,
        *args,
        trust_remote_code: bool = False,
19
        revision: str | None = None,
20
21
22
        download_dir: str | None = None,
        **kwargs,
    ) -> "TokenizerLike":
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        raise NotImplementedError

    @property
    def all_special_tokens(self) -> list[str]:
        raise NotImplementedError

    @property
    def all_special_ids(self) -> list[int]:
        raise NotImplementedError

    @property
    def bos_token_id(self) -> int:
        raise NotImplementedError

    @property
    def eos_token_id(self) -> int:
        raise NotImplementedError

41
42
43
44
    @property
    def pad_token_id(self) -> int:
        raise NotImplementedError

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    @property
    def is_fast(self) -> bool:
        raise NotImplementedError

    @property
    def vocab_size(self) -> int:
        raise NotImplementedError

    @property
    def max_token_id(self) -> int:
        raise NotImplementedError

    @property
    def truncation_side(self) -> str:
        raise NotImplementedError

    def __hash__(self) -> int:
        return hash(id(self))

    def __len__(self) -> int:
        return self.vocab_size

    def __call__(
        self,
69
        text: str | list[str],
70
        text_pair: str | None = None,
71
        add_special_tokens: bool = True,
72
73
        truncation: bool = False,
        max_length: int | None = None,
74
    ) -> "BatchEncoding":
75
76
77
78
79
80
81
82
83
84
85
86
87
        raise NotImplementedError

    def get_vocab(self) -> dict[str, int]:
        raise NotImplementedError

    def get_added_vocab(self) -> dict[str, int]:
        raise NotImplementedError

    def encode(
        self,
        text: str,
        truncation: bool | None = None,
        max_length: int | None = None,
88
        add_special_tokens: bool = True,
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    ) -> list[int]:
        raise NotImplementedError

    def apply_chat_template(
        self,
        messages: list["ChatCompletionMessageParam"],
        tools: list[dict[str, Any]] | None = None,
        **kwargs,
    ) -> list[int]:
        raise NotImplementedError

    def convert_tokens_to_string(self, tokens: list[str]) -> str:
        raise NotImplementedError

103
    def decode(self, ids: list[int] | int, skip_special_tokens: bool = False) -> str:
104
105
106
107
108
        raise NotImplementedError

    def convert_ids_to_tokens(
        self,
        ids: list[int],
109
        skip_special_tokens: bool = False,
110
111
    ) -> list[str]:
        raise NotImplementedError