Skip to content

Commit 0d96944

Browse files
Add DeepSeek V3.1 thinking mode support
- Added COMMON_CHAT_FORMAT_DEEPSEEK_V3_1 enum value - Created common_chat_params_init_deepseek_v3_1() function (currently uses R1 implementation) - Created common_chat_parse_deepseek_v3_1() function that handles V3.1 thinking format: - Extracts reasoning content before '</think>' tag into reasoning_content - Extracts regular content after '</think>' tag into content - No opening '<think>' tag in V3.1 format - Added detection logic for V3.1 templates based on pattern: 'message['prefix'] is defined and message['prefix'] and thinking' - Added V3.1 case to parsing switch statement This addresses the issue where V3.1 outputs reasoning content followed by '</think>' and then regular content without the opening '<think>' tag.
1 parent 54a241f commit 0d96944

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

common/chat.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,12 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
13121312
}
13131313
return data;
13141314
}
1315+
1316+
static common_chat_params common_chat_params_init_deepseek_v3_1(const common_chat_template & tmpl, const struct templates_params & inputs) {
1317+
// For now, use the same implementation as R1
1318+
return common_chat_params_init_deepseek_r1(tmpl, inputs);
1319+
}
1320+
13151321
static void common_chat_parse_deepseek_r1(common_chat_msg_parser & builder) {
13161322
builder.try_parse_reasoning("<think>", "</think>");
13171323
if (!builder.syntax().parse_tool_calls) {
@@ -1333,6 +1339,32 @@ static void common_chat_parse_deepseek_r1(common_chat_msg_parser & builder) {
13331339
tool_calls_end);
13341340
}
13351341

1342+
static void common_chat_parse_deepseek_v3_1(common_chat_msg_parser & builder) {
1343+
// DeepSeek V3.1 outputs reasoning content followed by "</think>" and then regular content
1344+
// There's no opening "<think>" tag, so we need to handle this differently
1345+
1346+
// First, try to find the "</think>" tag that separates thinking from regular content
1347+
static const common_regex thinking_end_regex("</think>");
1348+
if (auto res = builder.try_find_regex(thinking_end_regex)) {
1349+
// Extract everything before "</think>" as reasoning content
1350+
auto reasoning_content = builder.str(common_string_range{0, res->groups[0].begin});
1351+
auto stripped_reasoning = string_strip(reasoning_content);
1352+
1353+
if (!stripped_reasoning.empty()) {
1354+
builder.add_reasoning_content(stripped_reasoning);
1355+
}
1356+
1357+
// Move past the "</think>" tag
1358+
builder.move_to(res->groups[0].end);
1359+
1360+
// The rest is regular content
1361+
builder.add_content(builder.consume_rest());
1362+
} else {
1363+
// If no "</think>" tag found, treat everything as regular content
1364+
builder.add_content(builder.consume_rest());
1365+
}
1366+
}
1367+
13361368
static common_chat_params common_chat_params_init_gpt_oss(const common_chat_template & tmpl, const struct templates_params & inputs) {
13371369
common_chat_params data;
13381370
auto prompt = apply(tmpl, inputs);
@@ -2100,6 +2132,12 @@ static common_chat_params common_chat_templates_apply_jinja(
21002132
}
21012133
}
21022134

2135+
// DeepSeek V3.1: detect based on specific patterns in the template
2136+
if (src.find("message['prefix'] is defined and message['prefix'] and thinking") != std::string::npos &&
2137+
params.json_schema.is_null()) {
2138+
return common_chat_params_init_deepseek_v3_1(tmpl, params);
2139+
}
2140+
21032141
// DeepSeek R1: use handler in all cases except json schema (thinking / tools).
21042142
if (src.find("<|tool▁calls▁begin|>") != std::string::npos && params.json_schema.is_null()) {
21052143
return common_chat_params_init_deepseek_r1(tmpl, params);
@@ -2262,6 +2300,9 @@ static void common_chat_parse(common_chat_msg_parser & builder) {
22622300
case COMMON_CHAT_FORMAT_DEEPSEEK_R1:
22632301
common_chat_parse_deepseek_r1(builder);
22642302
break;
2303+
case COMMON_CHAT_FORMAT_DEEPSEEK_V3_1:
2304+
common_chat_parse_deepseek_v3_1(builder);
2305+
break;
22652306
case COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2:
22662307
common_chat_parse_functionary_v3_2(builder);
22672308
break;

common/chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ enum common_chat_format {
107107
COMMON_CHAT_FORMAT_FIREFUNCTION_V2,
108108
COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2,
109109
COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1,
110+
COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
110111
COMMON_CHAT_FORMAT_HERMES_2_PRO,
111112
COMMON_CHAT_FORMAT_COMMAND_R7B,
112113
COMMON_CHAT_FORMAT_GRANITE,

0 commit comments

Comments
 (0)