test_chat.py 4.13 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
import weakref
4

5
6
7
import pytest

from vllm import LLM
8
from vllm.distributed import cleanup_dist_env_and_memory
9

10
from ..openai.test_vision import TEST_IMAGE_ASSETS
11
12


13
14
15
16
@pytest.fixture(scope="function")
def text_llm():
    # pytest caches the fixture so we use weakref.proxy to
    # enable garbage collection
17
    llm = LLM(model="meta-llama/Llama-3.2-1B-Instruct", enforce_eager=True, seed=0)
18

19
    yield weakref.proxy(llm)
20

21
    del llm
22
23
24
25
26

    cleanup_dist_env_and_memory()


def test_chat(text_llm):
27
28
    prompt1 = "Explain the concept of entropy."
    messages = [
29
30
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": prompt1},
31
    ]
32
    outputs = text_llm.chat(messages)
33
34
35
    assert len(outputs) == 1


36
def test_multi_chat(text_llm):
37
38
39
40
    prompt1 = "Explain the concept of entropy."
    prompt2 = "Explain what among us is."

    conversation1 = [
41
42
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": prompt1},
43
44
45
    ]

    conversation2 = [
46
47
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": prompt2},
48
49
50
51
    ]

    messages = [conversation1, conversation2]

52
    outputs = text_llm.chat(messages)
53
54
55
    assert len(outputs) == 2


56
57
58
59
@pytest.fixture(scope="function")
def vision_llm():
    # pytest caches the fixture so we use weakref.proxy to
    # enable garbage collection
60
    llm = LLM(
61
        model="microsoft/Phi-3.5-vision-instruct",
62
63
64
65
66
        max_model_len=4096,
        max_num_seqs=5,
        enforce_eager=True,
        trust_remote_code=True,
        limit_mm_per_prompt={"image": 2},
67
        seed=0,
68
69
    )

70
    yield weakref.proxy(llm)
71

72
    del llm
73
74
75
76

    cleanup_dist_env_and_memory()


77
78
79
@pytest.mark.parametrize(
    "image_urls", [[TEST_IMAGE_ASSETS[0], TEST_IMAGE_ASSETS[1]]], indirect=True
)
80
def test_chat_multi_image(vision_llm, image_urls: list[str]):
81
82
83
84
85
86
87
88
89
90
91
92
    messages = [
        {
            "role": "user",
            "content": [
                *(
                    {"type": "image_url", "image_url": {"url": image_url}}
                    for image_url in image_urls
                ),
                {"type": "text", "text": "What's in this image?"},
            ],
        }
    ]
93
    outputs = vision_llm.chat(messages)
94
    assert len(outputs) >= 0
95
96


97
def test_llm_chat_tokenization_no_double_bos(text_llm):
98
99
100
101
102
    """
    LLM.chat() should not add special tokens when using chat templates.
    Check we get a single BOS token for llama chat.
    """
    messages = [
103
104
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Hello!"},
105
    ]
106
    outputs = text_llm.chat(messages)
107
    assert len(outputs) == 1
108
109

    prompt_token_ids = outputs[0].prompt_token_ids
110
111
    assert prompt_token_ids is not None

112
    bos_token = text_llm.get_tokenizer().bos_token_id
113
114
115
116

    # Ensure we have a single BOS
    assert prompt_token_ids[0] == bos_token
    assert prompt_token_ids[1] != bos_token, "Double BOS"
117
118
119
120
121
122
123
124
125
126
127
128
129


@pytest.fixture(scope="function")
def thinking_llm():
    # pytest caches the fixture so we use weakref.proxy to
    # enable garbage collection
    llm = LLM(
        model="Qwen/Qwen3-0.6B",
        max_model_len=4096,
        enforce_eager=True,
        seed=0,
    )

130
    yield weakref.proxy(llm)
131

132
    del llm
133
134
135
136
137
138
139

    cleanup_dist_env_and_memory()


@pytest.mark.parametrize("enable_thinking", [True, False])
def test_chat_extra_kwargs(thinking_llm, enable_thinking):
    messages = [
140
141
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "What is 1+1?"},
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    ]

    outputs = thinking_llm.chat(
        messages,
        chat_template_kwargs={"enable_thinking": enable_thinking},
    )
    assert len(outputs) == 1

    prompt_token_ids = outputs[0].prompt_token_ids
    assert prompt_token_ids is not None

    think_id = thinking_llm.get_tokenizer().get_vocab()["<think>"]

    if enable_thinking:
        assert think_id not in prompt_token_ids
    else:
        # The chat template includes dummy thinking process
        assert think_id in prompt_token_ids