forked from abetlen/llama-cpp-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_llama_chat_format.py
More file actions
219 lines (206 loc) · 7.69 KB
/
test_llama_chat_format.py
File metadata and controls
219 lines (206 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import json
import os
import platform
from collections.abc import Iterator
from typing import cast
import pytest
import jinja2
from typeguard import ForwardRefPolicy, check_type
from llama_cpp import (
ChatCompletionRequestUserMessage,
Llama,
llama_chat_format,
llama_supports_gpu_offload,
llama_types
)
from llama_cpp.llama_chat_format import hf_tokenizer_config_to_chat_formatter
from llama_cpp.llama_types import (
ChatCompletionRequestMessage,
ChatCompletionTool,
ChatCompletionToolChoiceOption,
CreateChatCompletionResponse,
CreateChatCompletionStreamResponse,
)
def test_mistral_instruct():
chat_template = "{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token}}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}"
chat_formatter = jinja2.Template(chat_template)
messages = [
llama_types.ChatCompletionRequestUserMessage(role="user", content="Instruction"),
llama_types.ChatCompletionRequestAssistantMessage(role="assistant", content="Model answer"),
llama_types.ChatCompletionRequestUserMessage(role="user", content="Follow-up instruction"),
]
response = llama_chat_format.format_mistral_instruct(
messages=messages,
)
prompt = ("" if response.added_special else "<s>") + response.prompt
reference = chat_formatter.render(
messages=messages,
bos_token="<s>",
eos_token="</s>",
)
assert prompt == reference
mistral_7b_tokenizer_config = """{
"add_bos_token": true,
"add_eos_token": false,
"added_tokens_decoder": {
"0": {
"content": "<unk>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"1": {
"content": "<s>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"2": {
"content": "</s>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
}
},
"additional_special_tokens": [],
"bos_token": "<s>",
"clean_up_tokenization_spaces": false,
"eos_token": "</s>",
"legacy": true,
"model_max_length": 1000000000000000019884624838656,
"pad_token": null,
"sp_model_kwargs": {},
"spaces_between_special_tokens": false,
"tokenizer_class": "LlamaTokenizer",
"unk_token": "<unk>",
"use_default_system_prompt": false,
"chat_template": "{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token}}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}"
}"""
def test_hf_tokenizer_config_str_to_chat_formatter():
tokenizer_config = json.loads(mistral_7b_tokenizer_config)
chat_formatter = hf_tokenizer_config_to_chat_formatter(
tokenizer_config
)
chat_formatter_respoonse = chat_formatter(
messages=[
ChatCompletionRequestUserMessage(role="user", content="Hello, world!"),
]
)
assert chat_formatter_respoonse.prompt == ("<s>[INST] Hello, world! [/INST]</s>" "")
def is_accelerator_available() -> bool:
"""Check if an accelerator is available."""
return llama_supports_gpu_offload() or (os.cpu_count() or 1) >= 8
@pytest.mark.parametrize(
"stream",
[
pytest.param(True, id="stream=True"),
pytest.param(False, id="stream=False"),
],
)
@pytest.mark.parametrize(
"tool_choice",
[
pytest.param("none", id="tool_choice=none"),
pytest.param("auto", id="tool_choice=auto"),
pytest.param(
{"type": "function", "function": {"name": "get_weather"}}, id="tool_choice=fixed"
),
],
)
@pytest.mark.parametrize(
"user_prompt_expected_tool_calls",
[
pytest.param(
("Is 7 a prime number?", 0),
id="expected_tool_calls=0",
),
pytest.param(
("What's the weather like in Paris today?", 1),
id="expected_tool_calls=1",
),
pytest.param(
("What's the weather like in Paris today? What about New York?", 2),
id="expected_tool_calls=2",
),
],
)
@pytest.mark.parametrize(
"llm_repo_id",
[
pytest.param("bartowski/Llama-3.2-3B-Instruct-GGUF", id="llama_3.2_3B"),
pytest.param(
"bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
id="llama_3.1_8B",
marks=pytest.mark.skipif(
not is_accelerator_available(), reason="Accelerator not available"
),
),
],
)
@pytest.mark.skipif(
platform.system() == "Darwin" and (os.cpu_count() or 1) < 8,
reason="Insufficient resources on macOS",
)
def test_llama_cpp_python_tool_use(
llm_repo_id: str,
user_prompt_expected_tool_calls: tuple[str, int],
tool_choice: ChatCompletionToolChoiceOption,
stream: bool,
) -> None:
"""Test the upgraded chatml-function-calling llama-cpp-python chat handler."""
user_prompt, expected_tool_calls = user_prompt_expected_tool_calls
if isinstance(tool_choice, dict) and expected_tool_calls == 0:
pytest.skip("Nonsensical")
llm = Llama.from_pretrained(
repo_id=llm_repo_id,
filename="*Q4_K_M.gguf",
n_ctx=4096,
n_gpu_layers=-1,
verbose=False,
chat_format="chatml-function-calling",
)
messages: list[ChatCompletionRequestMessage] = [{"role": "user", "content": user_prompt}]
tools: list[ChatCompletionTool] = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather for a location.",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string", "description": "A city name."}},
},
},
}
]
response = llm.create_chat_completion(
messages=messages, tools=tools, tool_choice=tool_choice, stream=stream
)
if stream:
response = cast(Iterator[CreateChatCompletionStreamResponse], response)
num_tool_calls = 0
for chunk in response:
check_type(chunk, CreateChatCompletionStreamResponse)
tool_calls = chunk["choices"][0]["delta"].get("tool_calls")
if isinstance(tool_calls, list):
num_tool_calls = max(tool_call["index"] for tool_call in tool_calls) + 1
assert num_tool_calls == (expected_tool_calls if tool_choice != "none" else 0)
else:
response = cast(CreateChatCompletionResponse, response)
check_type(
response, CreateChatCompletionResponse, forward_ref_policy=ForwardRefPolicy.IGNORE
)
if expected_tool_calls == 0 or tool_choice == "none":
assert response["choices"][0]["message"].get("tool_calls") is None
else:
assert len(response["choices"][0]["message"]["tool_calls"]) == expected_tool_calls
assert all(
tool_call["function"]["name"] == tools[0]["function"]["name"]
for tool_call in response["choices"][0]["message"]["tool_calls"]
)