Skip to content

Commit 707cde1

Browse files
authored
Merge pull request #8 from createthis/deepseek_3_1_thinking_mode_experimental
Strip grammar down to strictly what we expect based on model card. Throw
2 parents 3dee75a + a7316b0 commit 707cde1

File tree

3 files changed

+15
-40
lines changed

3 files changed

+15
-40
lines changed

common/chat.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,9 +1366,9 @@ static common_chat_params common_chat_params_init_deepseek_v3_1(const common_cha
13661366
auto parameters = function.at("parameters");
13671367
builder.resolve_refs(parameters);
13681368
tool_rules.push_back(builder.add_rule(name + "-call",
1369-
"( \"<|tool▁call▁begin|>\" )? \"function<|tool▁sep|>" + name + "\\n"
1370-
"```json\\n\" " + builder.add_schema(name + "-args", parameters) + " "
1371-
"\"```<|tool▁call▁end|>\""));
1369+
"( \"<|tool▁call▁begin|>\" )? \"" + name + "<|tool▁sep|>"
1370+
"\" " + builder.add_schema(name + "-args", parameters) + " "
1371+
"\"<|tool▁call▁end|>\""));
13721372
});
13731373
// Distill Qwen 7B & 32B models seem confused re/ syntax of their tool call opening tag,
13741374
// so we accept common variants (then it's all constrained)
@@ -1421,9 +1421,9 @@ static void common_chat_parse_deepseek_r1(common_chat_msg_parser & builder) {
14211421
}
14221422

14231423
static void common_chat_parse_deepseek_v3_1_content(common_chat_msg_parser & builder) {
1424-
static const common_regex function_regex("(?:<|tool▁call▁begin|>)?(?:function<|tool▁sep|>)?([^\\n<]+)(?:\\n```json\\n|<|tool▁sep|>)");
1424+
static const common_regex function_regex("(?:<|tool▁call▁begin|>)?([^\\n<]+)(?:<|tool▁sep|>)");
14251425

1426-
static const common_regex close_regex("(?:[\\n]*```[\\s\\r\\n]*)?<|tool▁call▁end|>");
1426+
static const common_regex close_regex("(?:[\\s]*)?<|tool▁call▁end|>");
14271427
static const common_regex tool_calls_begin("(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>|<|tool▁calls|>)");
14281428
static const common_regex tool_calls_end("<|tool▁calls▁end|>");
14291429

tests/test-chat-parser.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,7 @@ static void test_deepseek_v3_1_tool_calls() {
243243
assert_equals(variant, std::string(""), msg.content);
244244
assert_equals(variant, std::string(""), msg.reasoning_content);
245245

246-
// variant: function + fenced JSON
247-
{
248-
const std::string variant("fenced");
249-
const std::string in = "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_time\n```json\n{\"city\": \"Tokyo\"}\n```<|tool▁call▁end|><|tool▁calls▁end|>";
250-
auto m = common_chat_parse(in, false, syntax);
251-
assert_equals<std::size_t>(variant, 1, m.tool_calls.size());
252-
assert_equals(variant, std::string("get_time"), m.tool_calls[0].name);
253-
assert_equals(variant, std::string("{\"city\":\"Tokyo\"}"), m.tool_calls[0].arguments);
254-
assert_equals(variant, std::string(""), m.content);
255-
assert_equals(variant, std::string(""), m.reasoning_content);
256-
}
257-
258-
// variant: function + fenced JSON + thinking open
246+
// variant: simple + thinking open
259247
{
260248
common_chat_syntax syntax = {
261249
/* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
@@ -264,8 +252,8 @@ static void test_deepseek_v3_1_tool_calls() {
264252
/* .thinking_forced_open = */ true,
265253
/* .parse_tool_calls = */ true,
266254
};
267-
const std::string variant("fenced_thinking");
268-
const std::string in = "REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_time\n```json\n{\"city\": \"Tokyo\"}\n```<|tool▁call▁end|><|tool▁calls▁end|>";
255+
const std::string variant("simple_thinking");
256+
const std::string in = "REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>get_time<|tool▁sep|>{\"city\": \"Tokyo\"}<|tool▁call▁end|><|tool▁calls▁end|>";
269257
auto m = common_chat_parse(in, false, syntax);
270258
assert_equals<std::size_t>(variant, 1, m.tool_calls.size());
271259
assert_equals(variant, std::string("get_time"), m.tool_calls[0].name);
@@ -295,7 +283,7 @@ static void test_deepseek_v3_1_tool_calls() {
295283
}
296284

297285

298-
// variant: thinking forced open + tool call in reasoning content + function + fenced JSON
286+
// variant: thinking forced open + tool call in reasoning content
299287
{
300288
common_chat_syntax syntax = {
301289
/* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
@@ -304,8 +292,8 @@ static void test_deepseek_v3_1_tool_calls() {
304292
/* .thinking_forced_open = */ true,
305293
/* .parse_tool_calls = */ true,
306294
};
307-
const std::string variant("thinking_forced_open_tool_call_in_reasoning_fenced_thinking");
308-
const std::string in = "REASONING<|tool▁calls▁begin|><|tool▁call▁begin|>get_time2<|tool▁sep|>{\"city\": \"Tokyo2\"}<|tool▁call▁end|><|tool▁calls▁end|>REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_time\n```json\n{\"city\": \"Tokyo\"}\n```<|tool▁call▁end|><|tool▁calls▁end|>";
295+
const std::string variant("thinking_forced_open_tool_call_in_reasoning");
296+
const std::string in = "REASONING<|tool▁calls▁begin|><|tool▁call▁begin|>get_time2<|tool▁sep|>{\"city\": \"Tokyo2\"}<|tool▁call▁end|><|tool▁calls▁end|>REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>get_time<|tool▁sep|>{\"city\": \"Tokyo\"}<|tool▁call▁end|><|tool▁calls▁end|>";
309297
auto m = common_chat_parse(in, false, syntax);
310298
assert_equals<std::size_t>(variant, 1, m.tool_calls.size());
311299
assert_equals(variant, std::string("get_time"), m.tool_calls[0].name);

tests/test-chat.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,24 +1804,11 @@ static void test_template_output_parsers() {
18041804
/* .thinking_forced_open = */ false,
18051805
/* .parse_tool_calls = */ true,
18061806
}));
1807-
// variant: function + fenced JSON
1808-
assert_msg_equals(
1809-
simple_assist_msg("", "", "get_time", "{\"city\":\"Tokyo\"}"),
1810-
common_chat_parse(
1811-
"<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_time\n```json\n{\"city\": \"Tokyo\"}\n```<|tool▁call▁end|><|tool▁calls▁end|>",
1812-
/* is_partial= */ false,
1813-
{
1814-
COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
1815-
/* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
1816-
/* .reasoning_in_content = */ false,
1817-
/* .thinking_forced_open = */ false,
1818-
/* .parse_tool_calls = */ true,
1819-
}));
1820-
// variant: function + fenced JSON + thinking open
1807+
// variant: simple + thinking open
18211808
assert_msg_equals(
18221809
simple_assist_msg("", "REASONING", "get_time", "{\"city\":\"Tokyo\"}"),
18231810
common_chat_parse(
1824-
"REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_time\n```json\n{\"city\": \"Tokyo\"}\n```<|tool▁call▁end|><|tool▁calls▁end|>",
1811+
"REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>get_time<|tool▁sep|>{\"city\": \"Tokyo\"}<|tool▁call▁end|><|tool▁calls▁end|>",
18251812
/* is_partial= */ false,
18261813
{
18271814
COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
@@ -1848,11 +1835,11 @@ static void test_template_output_parsers() {
18481835
/* .thinking_forced_open = */ false,
18491836
/* .parse_tool_calls = */ true,
18501837
}));
1851-
// variant: thinking forced open + tool call in reasoning content + function + fenced JSON
1838+
// variant: thinking forced open + tool call in reasoning content
18521839
assert_msg_equals(
18531840
simple_assist_msg("", "REASONING<|tool▁calls▁begin|><|tool▁call▁begin|>get_time2<|tool▁sep|>{\"city\": \"Tokyo2\"}<|tool▁call▁end|><|tool▁calls▁end|>REASONING", "get_time", "{\"city\":\"Tokyo\"}"),
18541841
common_chat_parse(
1855-
"REASONING<|tool▁calls▁begin|><|tool▁call▁begin|>get_time2<|tool▁sep|>{\"city\": \"Tokyo2\"}<|tool▁call▁end|><|tool▁calls▁end|>REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>get_time\n```json\n{\"city\": \"Tokyo\"}\n```<|tool▁call▁end|><|tool▁calls▁end|>",
1842+
"REASONING<|tool▁calls▁begin|><|tool▁call▁begin|>get_time2<|tool▁sep|>{\"city\": \"Tokyo2\"}<|tool▁call▁end|><|tool▁calls▁end|>REASONING</think><|tool▁calls▁begin|><|tool▁call▁begin|>get_time<|tool▁sep|>{\"city\": \"Tokyo\"}<|tool▁call▁end|><|tool▁calls▁end|>",
18561843
/* is_partial= */ false,
18571844
{
18581845
COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,

0 commit comments

Comments
 (0)