Skip to content

Commit 569610e

Browse files
author
Olivier Chafik
committed
tool-calls: accommodate variety of wrong tool call opening tags both Qwen 32B and 7B distills like to spit out
1 parent c397bd1 commit 569610e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

common/chat.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
548548
"\"<|tool▁call▁begin|>function<|tool▁sep|>" + name + "\\n```json\\n\" " + args_rule + " \"```<|tool▁call▁end|>\""));
549549
});
550550
data.grammar_triggers.push_back({"<|tool▁calls▁begin|>", /* .at_start = */ false});
551+
data.grammar_triggers.push_back({"<|tool_calls_begin|>", /* .at_start = */ false});
552+
data.grammar_triggers.push_back({"<|tool calls begin|>", /* .at_start = */ false});
551553
data.preserved_tokens = {
552554
"<think>",
553555
"</think>",
@@ -557,8 +559,10 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
557559
"<|tool▁call▁end|>",
558560
};
559561
builder.add_rule("root",
560-
"\"<|tool▁calls▁begin|>\""
561-
" (" +string_join(tool_rules, " | ") + ")" + (inputs.parallel_tool_calls ? "*" : "") +
562+
// Distill Qwen 7B & 32B models seem confused re/ syntax of their tool call opening tag,
563+
// so we accept common variants (then it's all constrained)
564+
"( \"<|tool▁calls▁begin|>\" | \"<|tool_calls_begin|>\" | \"<|tool calls begin|>\" ) "
565+
"(" +string_join(tool_rules, " | ") + ")" + (inputs.parallel_tool_calls ? "*" : "") + " "
562566
"\"<|tool▁calls▁end|>\""
563567
" space");
564568
}, grammar_options);
@@ -581,7 +585,7 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
581585
return data;
582586
}
583587
static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input) {
584-
static std::regex trigger_regex("<|tool▁calls▁begin|>");
588+
static std::regex trigger_regex("<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>");
585589
static std::regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
586590
static std::regex close_regex("```<|tool▁call▁end|>");
587591
static std::regex think_regex(R"(<think>([\s\S\n]*)</think>([\s\S\r\n]*))");
@@ -591,6 +595,9 @@ static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input)
591595
msg.thoughts = string_trim(match[1].str());
592596
msg.content = string_trim(match[2].str());
593597
}
598+
if (msg.content == "<|tool▁calls▁end|>") {
599+
msg.content = "";
600+
}
594601
return msg;
595602
}
596603

examples/server/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,7 @@ curl http://localhost:8080/v1/chat/completions \
12061206
llama-server --jinja -fa -hf bartowski/Mistral-Nemo-Instruct-2407-GGUF:Q6_K_L
12071207
llama-server --jinja -fa -hf bartowski/functionary-small-v3.2-GGUF:Q4_K_M
12081208
llama-server --jinja -fa -hf bartowski/Llama-3.3-70B-Instruct-GGUF:Q4_K_M
1209-
llama-server --jinja -fa -hf bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF:Q4_K_M
1210-
llama-server --jinja -fa -hf bartowski/DeepSeek-R1-Distill-Qwen-32B-GGUF:Q6_K_L
1209+
llama-server --jinja -fa -hf bartowski/DeepSeek-R1-Distill-Qwen-32B-GGUF:Q4_K_M
12111210
12121211
# Native support requires the right template for these GGUFs:
12131212

0 commit comments

Comments
 (0)