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

if TYPE_CHECKING:
8
9
    from transformers import BatchEncoding

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


class TokenizerLike(Protocol):
    @classmethod
    def from_pretrained(
        cls,
17
18
19
        path_or_repo_id: str | Path,
        *args,
        trust_remote_code: bool = False,
20
        revision: str | None = None,
21
22
23
        download_dir: str | None = None,
        **kwargs,
    ) -> "TokenizerLike":
24
25
        raise NotImplementedError

26
27
28
    def num_special_tokens_to_add(self) -> int:
        raise NotImplementedError

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    @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

45
46
47
48
    @property
    def pad_token_id(self) -> int:
        raise NotImplementedError

49
50
51
52
53
54
55
56
57
58
59
60
    @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

61
62
63
64
    @property
    def max_chars_per_token(self) -> int:
        raise NotImplementedError

65
66
67
68
69
70
71
72
73
74
75
76
    @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,
77
        text: str | list[str],
78
        text_pair: str | None = None,
79
        add_special_tokens: bool = True,
80
81
        truncation: bool = False,
        max_length: int | None = None,
82
    ) -> "BatchEncoding":
83
84
85
86
87
88
89
90
91
92
93
94
95
        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,
96
        add_special_tokens: bool = True,
97
98
99
100
101
102
103
104
    ) -> list[int]:
        raise NotImplementedError

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

108
109
110
111
112
113
114
115
116
    @overload
    def convert_tokens_to_ids(self, tokens: str) -> int: ...

    @overload
    def convert_tokens_to_ids(self, tokens: list[str]) -> list[int]: ...

    def convert_tokens_to_ids(self, tokens: str | list[str]) -> int | list[int]:
        raise NotImplementedError

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

120
121
122
    def decode(
        self, ids: Sequence[int] | int, skip_special_tokens: bool = False
    ) -> str:
123
124
125
126
        raise NotImplementedError

    def convert_ids_to_tokens(
        self,
127
        ids: Sequence[int],
128
        skip_special_tokens: bool = False,
129
130
    ) -> list[str]:
        raise NotImplementedError