test_chat_template.py 3.19 KB
Newer Older
1
2
import pytest

3
4
from vllm.entrypoints.chat_utils import (apply_hf_chat_template,
                                         load_chat_template)
5
from vllm.entrypoints.openai.protocol import ChatCompletionRequest
6
from vllm.transformers_utils.tokenizer import get_tokenizer
7

8
9
10
from ..utils import VLLM_PATH

chatml_jinja_path = VLLM_PATH / "examples/template_chatml.jinja"
Simon Mo's avatar
Simon Mo committed
11
12
assert chatml_jinja_path.exists()

13
14
# Define models, templates, and their corresponding expected outputs
MODEL_TEMPLATE_GENERATON_OUTPUT = [
Simon Mo's avatar
Simon Mo committed
15
    ("facebook/opt-125m", chatml_jinja_path, True, """<|im_start|>user
16
17
18
19
20
21
22
Hello<|im_end|>
<|im_start|>assistant
Hi there!<|im_end|>
<|im_start|>user
What is the capital of<|im_end|>
<|im_start|>assistant
"""),
Simon Mo's avatar
Simon Mo committed
23
    ("facebook/opt-125m", chatml_jinja_path, False, """<|im_start|>user
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Hello<|im_end|>
<|im_start|>assistant
Hi there!<|im_end|>
<|im_start|>user
What is the capital of""")
]

TEST_MESSAGES = [
    {
        'role': 'user',
        'content': 'Hello'
    },
    {
        'role': 'assistant',
        'content': 'Hi there!'
    },
    {
        'role': 'user',
        'content': 'What is the capital of'
    },
]


47
def test_load_chat_template():
48
    # Testing chatml template
49
    template_content = load_chat_template(chat_template=chatml_jinja_path)
50
51
52
53
54

    # Test assertions
    assert template_content is not None
    # Hard coded value for template_chatml.jinja
    assert template_content == """{% for message in messages %}{{'<|im_start|>' + message['role'] + '\\n' + message['content']}}{% if (loop.last and add_generation_prompt) or not loop.last %}{{ '<|im_end|>' + '\\n'}}{% endif %}{% endfor %}
55
{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{ '<|im_start|>assistant\\n' }}{% endif %}"""  # noqa: E501
56
57


58
def test_no_load_chat_template_filelike():
59
60
    # Testing chatml template
    template = "../../examples/does_not_exist"
61
62

    with pytest.raises(ValueError, match="looks like a file path"):
63
        load_chat_template(chat_template=template)
64
65


66
def test_no_load_chat_template_literallike():
67
68
69
    # Testing chatml template
    template = "{{ messages }}"

70
    template_content = load_chat_template(chat_template=template)
71

72
    assert template_content == template
73
74
75
76
77


@pytest.mark.parametrize(
    "model,template,add_generation_prompt,expected_output",
    MODEL_TEMPLATE_GENERATON_OUTPUT)
78
79
def test_get_gen_prompt(model, template, add_generation_prompt,
                        expected_output):
80
81
    # Initialize the tokenizer
    tokenizer = get_tokenizer(tokenizer_name=model)
82
    template_content = load_chat_template(chat_template=template)
83
84
85
86
87
88
89
90

    # Create a mock request object using keyword arguments
    mock_request = ChatCompletionRequest(
        model=model,
        messages=TEST_MESSAGES,
        add_generation_prompt=add_generation_prompt)

    # Call the function and get the result
91
    result = apply_hf_chat_template(
92
        tokenizer,
93
        conversation=mock_request.messages,
94
        chat_template=mock_request.chat_template or template_content,
95
        add_generation_prompt=mock_request.add_generation_prompt,
96
    )
97
98

    # Test assertion
99
100
101
    assert result == expected_output, (
        f"The generated prompt does not match the expected output for "
        f"model {model} and template {template}")