Skip to content

Commit 18a11f4

Browse files
author
ochafik
committed
tool-call: r1: fix grammar
1 parent e84ee88 commit 18a11f4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

common/chat.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,14 +572,15 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
572572
// Distill Qwen 7B & 32B models seem confused re/ syntax of their tool call opening tag,
573573
// so we accept common variants (then it's all constrained)
574574
builder.add_rule("root",
575-
"( \"<|tool▁calls▁begin|>\" | \"<|tool_calls_begin|>\" | \"<|tool calls begin|>\" | \"<|tool\\_calls\\_begin|>\" ) "
575+
"( \"<|tool▁calls▁begin|>\" | \"<|tool_calls_begin|>\" | \"<|tool calls begin|>\" | \"<|tool\\\\_calls\\\\_begin|>\" ) "
576576
"(" +string_join(tool_rules, " | ") + ")" + (inputs.parallel_tool_calls ? "*" : "") + " "
577577
"\"<|tool▁calls▁end|>\""
578578
" space");
579579
data.grammar_triggers.push_back({"<|tool▁calls▁begin|>", /* .at_start = */ false});
580580
data.grammar_triggers.push_back({"<|tool_calls_begin|>", /* .at_start = */ false});
581581
data.grammar_triggers.push_back({"<|tool calls begin|>", /* .at_start = */ false});
582582
data.grammar_triggers.push_back({"<|tool\\_calls\\_begin|>", /* .at_start = */ false});
583+
data.grammar_triggers.push_back({"<|tool▁call▁begin|>", /* .at_start = */ false});
583584
data.preserved_tokens = {
584585
"<think>",
585586
"</think>",
@@ -613,9 +614,9 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
613614
return data;
614615
}
615616
static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input) {
616-
static std::regex trigger_regex("<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>");
617+
static std::regex trigger_regex("(<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>)?");
617618
static std::regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
618-
static std::regex close_regex("```<|tool▁call▁end|>");
619+
static std::regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");
619620
static std::regex think_regex(R"(<think>([\s\S\n]*)(</think>)?([\s\S\r\n]*))");
620621
auto msg = parse_json_tool_calls(input, trigger_regex, function_regex, close_regex);
621622
std::smatch match;

0 commit comments

Comments
 (0)