"examples/vscode:/vscode.git/clone" did not exist on "a21256c46327ec366b7804d22ba66ed04c2ae18b"
test_mistral_tokenizer.py 7.1 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4

import pytest
5
6
7
8
9
from mistral_common.protocol.instruct.messages import (
    AssistantMessage,
    ToolMessage,
    UserMessage,
)
10
from mistral_common.protocol.instruct.request import ChatCompletionRequest
11
12
13
14
15
16
from mistral_common.protocol.instruct.tool_calls import (
    Function,
    FunctionCall,
    Tool,
    ToolCall,
)
17
18

from vllm.transformers_utils.tokenizers.mistral import (
19
20
    make_mistral_chat_completion_request,
)
21
22
23
24


@pytest.mark.parametrize(
    "openai_request,expected_mistral_request",
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    [
        (
            {
                "messages": [
                    {
                        "role": "user",
                        "content": "What is the current local date and time?",
                    }
                ],
                "tools": [
                    {
                        "type": "function",
                        "function": {
                            "description": "Fetch the current local date and time.",
                            "name": "get_current_time",
                        },
                    }
                ],
            },
            ChatCompletionRequest(
                messages=[
                    UserMessage(content="What is the current local date and time?")
                ],
                tools=[
                    Tool(
                        type="function",
                        function=Function(
                            name="get_current_time",
                            description="Fetch the current local date and time.",
                            parameters={},
                        ),
                    )
                ],
            ),
59
        ),
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        (
            {
                "messages": [
                    {
                        "role": "user",
                        "content": "What is the current local date and time?",
                    }
                ],
                "tools": [
                    {
                        "type": "function",
                        "function": {
                            "description": "Fetch the current local date and time.",
                            "name": "get_current_time",
                            "parameters": None,
                        },
                    }
                ],
            },
            ChatCompletionRequest(
                messages=[
                    UserMessage(content="What is the current local date and time?")
                ],
                tools=[
                    Tool(
                        type="function",
                        function=Function(
                            name="get_current_time",
                            description="Fetch the current local date and time.",
                            parameters={},
                        ),
                    )
                ],
            ),
        ),
    ],
96
)
97
def test_make_mistral_chat_completion_request(openai_request, expected_mistral_request):
98
    actual_request = make_mistral_chat_completion_request(
99
100
        openai_request["messages"], openai_request["tools"]
    )
101
102
103
104
    assert actual_request == expected_mistral_request


# Tool use with list content and reasoning_content
105
106
107
108
@pytest.mark.parametrize(
    "openai_request,expected_mistral_request",
    [
        (
109
            {
110
111
112
113
                "messages": [
                    {
                        "role": "user",
                        "content": "What's the weather in Paris?",
114
                    },
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
                    {
                        "role": "assistant",
                        "reasoning_content": None,
                        "content": None,
                        "tool_calls": [
                            {
                                "id": "call123",
                                "type": "function",
                                "function": {
                                    "name": "get_weather",
                                    "arguments": '{"city": "Paris"}',
                                },
                            }
                        ],
                    },
                    {
                        "role": "tool",
                        "content": [{"type": "text", "text": "Rainy"}],
                        "name": "get_weather",
                        "tool_call_id": "call123",
135
                    },
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
                ],
                "tools": [
                    {
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "description": "Gets the current weather in a city.",
                            "parameters": {
                                "type": "object",
                                "properties": {
                                    "city": {
                                        "type": "string",
                                        "description": "The city name",
                                    }
                                },
                                "required": ["city"],
                            },
                        },
                    }
                ],
156
            },
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
            ChatCompletionRequest(
                messages=[
                    UserMessage(content="What's the weather in Paris?"),
                    AssistantMessage(
                        content=None,
                        tool_calls=[
                            ToolCall(
                                id="call123",
                                function=FunctionCall(
                                    name="get_weather",
                                    arguments='{"city": "Paris"}',
                                ),
                            )
                        ],
                    ),
                    ToolMessage(
                        content="Rainy",
                        tool_call_id="call123",
                        name="get_weather",
                    ),
                ],
                tools=[
                    Tool(
                        type="function",
                        function=Function(
182
                            name="get_weather",
183
184
185
186
187
188
189
190
191
192
193
                            description="Gets the current weather in a city.",
                            parameters={
                                "type": "object",
                                "properties": {
                                    "city": {
                                        "type": "string",
                                        "description": "The city name",
                                    }
                                },
                                "required": ["city"],
                            },
194
195
196
197
                        ),
                    )
                ],
            ),
198
199
200
        )
    ],
)
201
def test_make_mistral_chat_completion_request_list_content(
202
203
    openai_request, expected_mistral_request
):
204
    actual_request = make_mistral_chat_completion_request(
205
206
        openai_request["messages"], openai_request["tools"]
    )
207
    assert actual_request == expected_mistral_request