Skip to content

Commit 86a52f4

Browse files
committed
add tests and unsloth template
1 parent d601b07 commit 86a52f4

3 files changed

Lines changed: 306 additions & 7 deletions

File tree

common/chat.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ static common_chat_params common_chat_params_init_lfm2(const common_chat_templat
989989
return data;
990990
}
991991

992-
static common_chat_params common_chat_params_init_mistral_3(const common_chat_template & tmpl, const struct templates_params & inputs) {
992+
static common_chat_params common_chat_params_init_ministral_3(const common_chat_template & tmpl, const struct templates_params & inputs) {
993993
common_chat_params data;
994994

995995
// Build up messages to follow the format: https://huggingface.co/mistralai/Ministral-3-14B-Reasoning-2512/blob/main/chat_template.jinja
@@ -1049,7 +1049,7 @@ static common_chat_params common_chat_params_init_mistral_3(const common_chat_te
10491049

10501050
// Response format parser
10511051
if (inputs.json_schema.is_object() && !inputs.json_schema.empty()) {
1052-
// Ministral wants to emit json surrounded code fences
1052+
// Ministral wants to emit json surrounded by code fences
10531053
return reasoning << "```json" << p.content(p.schema(p.json(), "response-format", inputs.json_schema)) << "```";
10541054
}
10551055

@@ -2619,11 +2619,11 @@ static common_chat_params common_chat_templates_apply_jinja(
26192619
return common_chat_params_init_llama_3_x(tmpl, params, allow_python_tag_builtin_tools);
26202620
}
26212621

2622-
// Ministral/Mistral 3
2622+
// Ministral/Mistral Large 3
26232623
if (src.find("[SYSTEM_PROMPT]") != std::string::npos &&
26242624
src.find("[TOOL_CALLS]") != std::string::npos &&
26252625
src.find("[ARGS]") != std::string::npos) {
2626-
return common_chat_params_init_mistral_3(tmpl, params);
2626+
return common_chat_params_init_ministral_3(tmpl, params);
26272627
}
26282628

26292629
if (src.find("[THINK]") != std::string::npos && src.find("[/THINK]") != std::string::npos) {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{#- Unsloth template fixes #}
2+
{#- Default system message if no system prompt is passed. #}
3+
{%- set default_system_message = '# HOW YOU SHOULD THINK AND ANSWER\n\nFirst draft your thinking process (inner monologue) until you arrive at a response. Format your response using Markdown, and use LaTeX for any mathematical equations. Write both your thoughts and the response in the same language as the input.\n\nYour thinking process must follow the template below:[THINK]Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate the response to the user.[/THINK]Here, provide a self-contained response.' %}
4+
5+
{#- Begin of sequence token. #}
6+
{{- bos_token }}
7+
8+
{#- Handle system prompt if it exists. #}
9+
{#- System prompt supports text content or text and thinking chunks. #}
10+
{%- if messages[0]['role'] == 'system' %}
11+
{{- '[SYSTEM_PROMPT]' -}}
12+
{%- if messages[0]['content'] is string %}
13+
{{- messages[0]['content'] -}}
14+
{%- else %}
15+
{%- for block in messages[0]['content'] %}
16+
{%- if block['type'] == 'text' %}
17+
{{- block['text'] }}
18+
{%- elif block['type'] == 'thinking' %}
19+
{{- '[THINK]' + block['thinking'] + '[/THINK]' }}
20+
{%- else %}
21+
{{- raise_exception('Only text and thinking chunks are supported in system message contents.') }}
22+
{%- endif %}
23+
{%- endfor %}
24+
{%- endif %}
25+
{{- '[/SYSTEM_PROMPT]' -}}
26+
{%- set loop_messages = messages[1:] %}
27+
{%- else %}
28+
{%- set loop_messages = messages %}
29+
{%- if default_system_message != '' %}
30+
{{- '[SYSTEM_PROMPT]' + default_system_message + '[/SYSTEM_PROMPT]' }}
31+
{%- endif %}
32+
{%- endif %}
33+
34+
35+
{#- Tools definition #}
36+
{%- set tools_definition = '' %}
37+
{%- set has_tools = false %}
38+
{%- if tools is defined and tools is not none and tools|length > 0 %}
39+
{%- set has_tools = true %}
40+
{%- set tools_definition = '[AVAILABLE_TOOLS]' + (tools| tojson) + '[/AVAILABLE_TOOLS]' %}
41+
{{- tools_definition }}
42+
{%- endif %}
43+
44+
{#- Checks for alternating user/assistant messages. #}
45+
{%- set ns = namespace(index=0) %}
46+
{%- for message in loop_messages %}
47+
{%- if message.role == 'user' or (message.role == 'assistant' and (message.tool_calls is not defined or message.tool_calls is none or message.tool_calls | length == 0)) %}
48+
{%- if (message['role'] == 'user') != (ns.index % 2 == 0) %}
49+
{{- raise_exception('After the optional system message, conversation roles must alternate user and assistant roles except for tool calls and results.') }}
50+
{%- endif %}
51+
{%- set ns.index = ns.index + 1 %}
52+
{%- endif %}
53+
{%- endfor %}
54+
55+
{#- Handle conversation messages. #}
56+
{%- for message in loop_messages %}
57+
58+
{#- User messages supports text content or text and image chunks. #}
59+
{%- if message['role'] == 'user' %}
60+
{%- if message['content'] is string %}
61+
{{- '[INST]' + message['content'] + '[/INST]' }}
62+
{%- elif message['content'] | length > 0 %}
63+
{{- '[INST]' }}
64+
{%- if message['content'] | length == 2 %}
65+
{%- set blocks = message['content'] | sort(attribute='type') %}
66+
{%- else %}
67+
{%- set blocks = message['content'] %}
68+
{%- endif %}
69+
{%- for block in blocks %}
70+
{%- if block['type'] == 'text' %}
71+
{{- block['text'] }}
72+
{%- elif block['type'] in ['image', 'image_url'] %}
73+
{{- '[IMG]' }}
74+
{%- else %}
75+
{{- raise_exception('Only text, image and image_url chunks are supported in user message content.') }}
76+
{%- endif %}
77+
{%- endfor %}
78+
{{- '[/INST]' }}
79+
{%- else %}
80+
{{- raise_exception('User message must have a string or a list of chunks in content') }}
81+
{%- endif %}
82+
83+
{#- Assistant messages supports text content or text, image and thinking chunks. #}
84+
{%- elif message['role'] == 'assistant' %}
85+
86+
{%- if message['content'] is string and message['content'] != '' %}
87+
{{- message['content'] }}
88+
{%- elif message['content'] is iterable and message['content'] | length > 0 %}
89+
{%- for block in message['content'] %}
90+
{%- if block['type'] == 'text' %}
91+
{{- block['text'] }}
92+
{%- elif block['type'] == 'thinking' %}
93+
{{- '[THINK]' + block['thinking'] + '[/THINK]' }}
94+
{%- else %}
95+
{{- raise_exception('Only text and thinking chunks are supported in assistant message contents.') }}
96+
{%- endif %}
97+
{%- endfor %}
98+
{%- endif %}
99+
100+
{%- if message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
101+
{%- for tool in message['tool_calls'] %}
102+
{{- '[TOOL_CALLS]' }}
103+
{%- set name = tool['function']['name'] %}
104+
{%- set arguments = tool['function']['arguments'] %}
105+
{%- if arguments is not string %}
106+
{%- set arguments = arguments|tojson|safe %}
107+
{%- elif arguments == '' %}
108+
{%- set arguments = '{}' %}
109+
{%- endif %}
110+
{{- name + '[ARGS]' + arguments }}
111+
{%- endfor %}
112+
{%- endif %}
113+
114+
{{- eos_token }}
115+
116+
{#- Tool messages only supports text content. #}
117+
{%- elif message['role'] == 'tool' %}
118+
{{- '[TOOL_RESULTS]' + message['content']|string + '[/TOOL_RESULTS]' }}
119+
120+
{#- Raise exception for unsupported roles. #}
121+
{%- else %}
122+
{{- raise_exception('Only user, assistant and tool roles are supported, got ' + message['role'] + '.') }}
123+
{%- endif %}
124+
{%- endfor %}
125+
126+
{#- Copyright 2025-present Unsloth. Apache 2.0 License. #}

tests/test-chat.cpp

Lines changed: 176 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ static std::string renormalize_json(const std::string & json_str) {
150150
return json_str;
151151
}
152152
}
153+
154+
// Use for PEG parser implementations
155+
struct make_peg_parser {
156+
common_chat_params params_;
157+
common_peg_arena arena_;
158+
159+
make_peg_parser(common_chat_templates * tmpls, const common_chat_templates_inputs & inputs) {
160+
params_ = common_chat_templates_apply(tmpls, inputs);
161+
arena_.load(params_.parser);
162+
}
163+
164+
common_chat_msg operator()(const std::string & msg, bool is_partial) {
165+
return common_chat_peg_parse(arena_, msg, is_partial, /* syntax = */ {params_.format});
166+
}
167+
};
168+
153169
static void assert_msg_equals(const common_chat_msg & expected, const common_chat_msg & actual, bool ignore_whitespace_differences = false) {
154170
assert_equals(expected.role, actual.role);
155171
if (ignore_whitespace_differences) {
@@ -429,9 +445,16 @@ static void test_templates(const struct common_chat_templates * tmpls, const std
429445
template <typename T>
430446
static void test_parser_with_streaming(const common_chat_msg & expected, const std::string & raw_message, T parse_msg) {
431447
auto merged = simple_assist_msg("");
432-
auto last_msg = parse_msg("");
448+
common_chat_msg last_msg;
449+
last_msg.role = "assistant";
433450
for (size_t i = 1; i <= raw_message.size(); ++i) {
434-
auto curr_msg = parse_msg(raw_message.substr(0, i));
451+
auto is_partial = i < raw_message.size();
452+
common_chat_msg curr_msg;
453+
if constexpr (std::is_invocable_v<T, std::string, bool>) {
454+
curr_msg = parse_msg(raw_message.substr(0, i), is_partial);
455+
} else {
456+
curr_msg = parse_msg(raw_message.substr(0, i));
457+
}
435458
if (curr_msg == simple_assist_msg("")) continue;
436459
LOG_INF("Streaming msg: %s\n", common_chat_msgs_to_json_oaicompat<json>({curr_msg}).dump().c_str());
437460
for (auto diff: common_chat_msg_diff::compute_diffs(last_msg, curr_msg)) {
@@ -456,7 +479,11 @@ static void test_parser_with_streaming(const common_chat_msg & expected, const s
456479
assert_msg_equals(curr_msg, merged, true);
457480
last_msg = curr_msg;
458481
}
459-
assert_msg_equals(expected, parse_msg(raw_message), true);
482+
if constexpr (std::is_invocable_v<T, std::string, bool>) {
483+
assert_msg_equals(expected, parse_msg(raw_message, false), true);
484+
} else {
485+
assert_msg_equals(expected, parse_msg(raw_message), true);
486+
}
460487
assert_msg_equals(expected, merged, true);
461488
}
462489

@@ -3302,6 +3329,152 @@ Hey there!<|im_end|>
33023329
GGML_ASSERT(grammar && "Failed to build Qwen3-Coder grammar with union types");
33033330
}
33043331

3332+
{
3333+
// Ministral-3-14B-Reasoning-2512
3334+
auto tmpls = read_templates("models/templates/unsloth-mistral-Ministral-3-14B-Reasoning-2512.jinja");
3335+
common_chat_msg msg;
3336+
msg.role = "user";
3337+
msg.content = "hello";
3338+
3339+
{
3340+
// Test basic message
3341+
common_chat_msg expected;
3342+
expected.role = "assistant";
3343+
expected.content = "Hello world";
3344+
3345+
common_chat_templates_inputs inputs;
3346+
inputs.messages = {msg};
3347+
3348+
test_parser_with_streaming(expected,
3349+
"Hello world",
3350+
make_peg_parser(tmpls.get(), inputs)
3351+
);
3352+
}
3353+
{
3354+
// Test basic message and reasoning with reasoning_format = none
3355+
common_chat_msg expected;
3356+
expected.role = "assistant";
3357+
expected.content = "[THINK]I am thinking[/THINK]Hello world";
3358+
3359+
common_chat_templates_inputs inputs;
3360+
inputs.messages = {msg};
3361+
3362+
test_parser_with_streaming(expected,
3363+
"[THINK]I am thinking[/THINK]Hello world",
3364+
make_peg_parser(tmpls.get(), inputs)
3365+
);
3366+
}
3367+
{
3368+
// Test basic message and reasoning with reasoning_format = auto
3369+
common_chat_msg expected;
3370+
expected.role = "assistant";
3371+
expected.content = "Hello world";
3372+
expected.reasoning_content = "I am thinking";
3373+
3374+
common_chat_templates_inputs inputs;
3375+
inputs.messages = {msg};
3376+
inputs.reasoning_format = COMMON_REASONING_FORMAT_AUTO;
3377+
3378+
test_parser_with_streaming(expected,
3379+
"[THINK]I am thinking[/THINK]Hello world",
3380+
make_peg_parser(tmpls.get(), inputs)
3381+
);
3382+
}
3383+
{
3384+
// Test basic tool call
3385+
common_chat_msg expected;
3386+
expected.role = "assistant";
3387+
expected.reasoning_content = "I need to get the weather in New York City";
3388+
expected.tool_calls = {{
3389+
/* .name = */ "get_weather",
3390+
/* .arguments = */ R"({"location": "New York City, NY"})",
3391+
/* .id = */ {},
3392+
}};
3393+
3394+
common_chat_templates_inputs inputs;
3395+
inputs.messages = {msg};
3396+
inputs.reasoning_format = COMMON_REASONING_FORMAT_AUTO;
3397+
inputs.tools = {{
3398+
/* .name = */ "get_weather",
3399+
/* .description = */ "get the weather",
3400+
/* .parameters = */ R"({
3401+
"type": "object",
3402+
"properties": {
3403+
"location": {"type": "string"}
3404+
}
3405+
})"
3406+
}};
3407+
3408+
test_parser_with_streaming(expected,
3409+
"[THINK]I need to get the weather in New York City[/THINK]"
3410+
R"([TOOL_CALLS]get_weather[ARGS]{"location": "New York City, NY"})",
3411+
make_peg_parser(tmpls.get(), inputs)
3412+
);
3413+
}
3414+
{
3415+
// Test basic tool call with parallel_tool_calls = true
3416+
common_chat_msg expected;
3417+
expected.role = "assistant";
3418+
expected.reasoning_content = "I need to get the weather in New York City and Los Angeles";
3419+
expected.tool_calls = {{
3420+
/* .name = */ "get_weather",
3421+
/* .arguments = */ R"({"location": "New York City, NY"})",
3422+
/* .id = */ {},
3423+
}, {
3424+
/* .name = */ "get_weather",
3425+
/* .arguments = */ R"({"location": "Los Angeles, CA"})",
3426+
/* .id = */ {},
3427+
}};
3428+
3429+
common_chat_templates_inputs inputs;
3430+
inputs.messages = {msg};
3431+
inputs.reasoning_format = COMMON_REASONING_FORMAT_AUTO;
3432+
inputs.parallel_tool_calls = true;
3433+
inputs.tools = {{
3434+
/* .name = */ "get_weather",
3435+
/* .description = */ "get the weather",
3436+
/* .parameters = */ R"({
3437+
"type": "object",
3438+
"properties": {
3439+
"location": {"type": "string"}
3440+
}
3441+
})"
3442+
}};
3443+
3444+
test_parser_with_streaming(expected,
3445+
"[THINK]I need to get the weather in New York City and Los Angeles[/THINK]"
3446+
R"([TOOL_CALLS]get_weather[ARGS]{"location": "New York City, NY"})"
3447+
R"([TOOL_CALLS]get_weather[ARGS]{"location": "Los Angeles, CA"})",
3448+
make_peg_parser(tmpls.get(), inputs)
3449+
);
3450+
}
3451+
{
3452+
// Test response format
3453+
common_chat_msg expected;
3454+
expected.role = "assistant";
3455+
expected.reasoning_content = "I need to output the invoice details in JSON";
3456+
expected.content = R"({"amount": 123.45, "date": "2025-12-03"})";
3457+
3458+
common_chat_templates_inputs inputs;
3459+
inputs.messages = {msg};
3460+
inputs.reasoning_format = COMMON_REASONING_FORMAT_AUTO;
3461+
inputs.json_schema = R"({
3462+
"type": "object",
3463+
"properties": {
3464+
"amount": {"type": "number"},
3465+
"date": {"type": "string"}
3466+
}
3467+
})";
3468+
3469+
test_parser_with_streaming(expected,
3470+
"[THINK]I need to output the invoice details in JSON[/THINK]"
3471+
"```json\n"
3472+
R"({"amount": 123.45, "date": "2025-12-03"})"
3473+
"\n```",
3474+
make_peg_parser(tmpls.get(), inputs)
3475+
);
3476+
}
3477+
}
33053478
}
33063479

33073480
static void test_msg_diffs_compute() {

0 commit comments

Comments
 (0)