Skip to content

Commit 31f5eb2

Browse files
author
ochafik
committed
accommodate yet another deepseek r1 distill fantasy syntax (<|tool▁calls|>)
1 parent d4cb7fe commit 31f5eb2

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

common/chat.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,22 +1130,22 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
11301130
auto parameters = function.at("parameters");
11311131
builder.resolve_refs(parameters);
11321132
tool_rules.push_back(builder.add_rule(name + "-call",
1133-
"\"<|tool▁call▁begin|>function<|tool▁sep|>" + name + "\\n"
1133+
"( \"<|tool▁call▁begin|>\" )? \"function<|tool▁sep|>" + name + "\\n"
11341134
"```json\\n\" " + builder.add_schema(name + "-args", parameters) + " "
11351135
"\"```<|tool▁call▁end|>\""));
11361136
});
11371137
// Distill Qwen 7B & 32B models seem confused re/ syntax of their tool call opening tag,
11381138
// so we accept common variants (then it's all constrained)
11391139
builder.add_rule("root",
11401140
std::string(data.thinking_forced_open ? "\"</think>\" space " : "") +
1141-
"( \"<|tool▁calls▁begin|>\" | \"<|tool_calls_begin|>\" | \"<|tool calls begin|>\" | \"<|tool\\\\_calls\\\\_begin|>\" ) "
1141+
"( \"<|tool▁calls▁begin|>\" | \"<|tool_calls_begin|>\" | \"<|tool calls begin|>\" | \"<|tool\\\\_calls\\\\_begin|>\" | \"<|tool▁calls|>\" ) "
11421142
"(" + string_join(tool_rules, " | ") + ")" + (inputs.parallel_tool_calls ? "*" : "") + " "
11431143
"\"<|tool▁calls▁end|>\""
11441144
" space");
11451145
data.grammar_triggers.push_back({
11461146
COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN_FULL,
11471147
std::string(data.thinking_forced_open ? "[\\s\\S]*?</think>" : "(?:<think>[\\s\\S]*?</think>)?") +
1148-
"\\s*(<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>)[\\s\\S]*"
1148+
"\\s*(<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>|<|tool▁calls|>)[\\s\\S]*"
11491149
});
11501150
data.preserved_tokens = {
11511151
"<think>",
@@ -1163,9 +1163,9 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
11631163
static void common_chat_parse_deepseek_r1(common_chat_msg_parser & builder) {
11641164
builder.try_parse_reasoning("<think>", "</think>");
11651165

1166-
static const common_regex tool_calls_begin("[\\s\\r\\n]*(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>)");
1166+
static const common_regex tool_calls_begin("[\\s\\r\\n]*(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>|<|tool▁calls|>)");
11671167
static const common_regex tool_calls_end("<|tool▁calls▁end|>");
1168-
static const common_regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
1168+
static const common_regex function_regex("(?:<|tool▁call▁begin|>)?function<|tool▁sep|>([^\n]+)\n```json\n");
11691169
static const common_regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");
11701170

11711171
parse_json_tool_calls(builder, tool_calls_begin, function_regex, close_regex, tool_calls_end);

tests/test-chat.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,15 @@ static void test_template_output_parsers() {
13031303
"```<|tool▁call▁end|><|tool▁calls▁end|>",
13041304
/* is_partial= */ false,
13051305
{COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
1306+
assert_msg_equals(message_assist_call,
1307+
common_chat_parse(
1308+
"<|tool▁calls|>function<|tool▁sep|>special_function\n"
1309+
"```json\n"
1310+
"{\"arg1\": 1}\n"
1311+
"```<|tool▁call▁end|><|tool▁calls▁end|>",
1312+
/* is_partial= */ false,
1313+
{COMMON_CHAT_FORMAT_DEEPSEEK_R1}));
1314+
13061315
assert_msg_equals(message_assist_call_thoughts,
13071316
common_chat_parse(
13081317
"<think>I'm\nthinking</think>\n\n"

0 commit comments

Comments
 (0)