main
py 131 lines 3.68 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from typing import NamedTuple
5
6 import openai # use the official client for correctness check
7 import pytest
8 import pytest_asyncio
9
10 from tests.utils import RemoteOpenAIServer
11 from vllm.config import ModelConfig
12
13 # # any model with a chat template should work here
14 MODEL_NAME = "Qwen/Qwen2-1.5B-Instruct"
15
16
17 def get_vocab_size(model_name):
18 config = ModelConfig(
19 model=model_name,
20 seed=0,
21 dtype="float16",
22 )
23 return config.get_vocab_size()
24
25
26 @pytest.fixture(scope="module")
27 def server():
28 args = [
29 # use half precision for speed and memory savings in CI environment
30 "--dtype",
31 "float16",
32 "--enforce-eager",
33 "--max-model-len",
34 "4080",
35 "--max-logprobs", # test prompt_logprobs equal to -1
36 "151936",
37 ]
38
39 with RemoteOpenAIServer(MODEL_NAME, args) as remote_server:
40 yield remote_server
41
42
43 @pytest_asyncio.fixture
44 async def client(server):
45 async with server.get_async_client() as async_client:
46 yield async_client
47
48
49 class TestCase(NamedTuple):
50 model_name: str
51 echo: bool
52
53
54 @pytest.mark.asyncio
55 @pytest.mark.parametrize(
56 "test_case",
57 [
58 TestCase(model_name=MODEL_NAME, echo=True),
59 TestCase(model_name=MODEL_NAME, echo=False),
60 ],
61 )
62 async def test_chat_session_with_echo_and_continue_final_message(
63 client: openai.AsyncOpenAI, test_case: TestCase
64 ):
65 saying: str = "Here is a common saying about apple. An apple a day, keeps"
66 # test echo with continue_final_message parameter
67 chat_completion = await client.chat.completions.create(
68 model=test_case.model_name,
69 messages=[
70 {"role": "user", "content": "tell me a common saying"},
71 {"role": "assistant", "content": saying},
72 ],
73 extra_body={
74 "echo": test_case.echo,
75 "continue_final_message": True,
76 "add_generation_prompt": False,
77 },
78 )
79 assert chat_completion.id is not None
80 assert len(chat_completion.choices) == 1
81
82 choice = chat_completion.choices[0]
83 assert choice.finish_reason == "stop"
84
85 message = choice.message
86 if test_case.echo:
87 assert message.content is not None and saying in message.content
88 else:
89 assert message.content is not None and saying not in message.content
90 assert message.role == "assistant"
91
92
93 @pytest.mark.asyncio
94 async def test_prompt_logprobs(client: openai.AsyncOpenAI):
95 messages = [
96 {"role": "system", "content": "You are a helpful assistant."},
97 {"role": "user", "content": "Beijing is the capital of which country?"},
98 ]
99
100 completion = await client.chat.completions.create(
101 model=MODEL_NAME,
102 messages=messages,
103 extra_body={"prompt_logprobs": -1},
104 )
105
106 assert completion.prompt_logprobs is not None
107 assert len(completion.prompt_logprobs) > 0
108
109
110 @pytest.mark.asyncio
111 async def test_top_logprobs(client: openai.AsyncOpenAI):
112 messages = [
113 {"role": "system", "content": "You are a helpful assistant."},
114 {"role": "user", "content": "Beijing is the capital of which country?"},
115 ]
116
117 completion = await client.chat.completions.create(
118 model=MODEL_NAME,
119 messages=messages,
120 max_tokens=1,
121 extra_body={
122 "top_logprobs": -1,
123 "logprobs": "true",
124 },
125 )
126 assert completion.choices[0].logprobs is not None
127 assert completion.choices[0].logprobs.content is not None
128 assert len(completion.choices[0].logprobs.content) > 0
129 assert len(
130 completion.choices[0].logprobs.content[0].top_logprobs
131 ) == get_vocab_size(MODEL_NAME)