main
py 54 lines 1.65 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from unittest.mock import MagicMock
5
6 import pytest
7
8 from vllm.entrypoints.openai.chat_completion.protocol import (
9 ChatCompletionRequest,
10 )
11
12
13 @pytest.fixture()
14 def should_do_global_cleanup_after_test() -> bool:
15 return False
16
17
18 def make_mock_tokenizer(
19 vocab: dict[str, int],
20 special_tokens: list[str] | None = None,
21 ) -> MagicMock:
22 """Create a mock tokenizer with the given vocabulary.
23
24 Args:
25 vocab: Mapping of token text to token ID.
26 special_tokens: Which tokens to mark as special. When ``None``
27 (the default), every key in *vocab* is treated as special —
28 convenient when the vocab only contains delimiter tokens.
29
30 The returned mock supports get_vocab(), encode(), and decode().
31 decode() maps known token IDs back to their text and falls back to
32 chr(id) for ASCII IDs or ``<id>`` for others.
33
34 """
35 id_to_text = {v: k for k, v in vocab.items()}
36 tokenizer = MagicMock()
37 tokenizer.encode.return_value = [1, 2, 3]
38 tokenizer.get_vocab.return_value = dict(vocab)
39 tokenizer.decode.side_effect = lambda ids: "".join(
40 id_to_text.get(i, chr(i) if i < 128 else f"<{i}>") for i in ids
41 )
42 st = special_tokens if special_tokens is not None else list(vocab.keys())
43 tokenizer.all_special_tokens = st
44 tokenizer.all_special_ids = [vocab[t] for t in st if t in vocab]
45 return tokenizer
46
47
48 @pytest.fixture
49 def mock_request():
50 req = MagicMock(spec=ChatCompletionRequest)
51 req.tools = []
52 req.tool_choice = "auto"
53 req.include_reasoning = True
54 return req