Skip to content

Commit f0da116

Browse files
committed
Try to set message['prefix'] when thinking is enabled.
1 parent 0d372f4 commit f0da116

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

common/chat.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,30 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
13161316

13171317
static common_chat_params common_chat_params_init_deepseek_v3_1(const common_chat_template & tmpl, const struct templates_params & inputs) {
13181318
common_chat_params data;
1319-
auto prompt = apply(tmpl, inputs);
1319+
1320+
// Pass thinking context for DeepSeek V3.1 template
1321+
json additional_context = {
1322+
{"thinking", inputs.enable_thinking},
1323+
};
1324+
1325+
// For DeepSeek V3.1, we need to set prefix on assistant messages to trigger <think> generation
1326+
json adjusted_messages = inputs.messages;
1327+
if (inputs.enable_thinking) {
1328+
adjusted_messages = json::array();
1329+
for (const auto & msg : inputs.messages) {
1330+
auto adjusted_msg = msg;
1331+
// Set prefix on assistant messages to trigger <think> generation
1332+
if (msg.is_object() && msg.contains("role") && msg["role"] == "assistant") {
1333+
adjusted_msg["prefix"] = "<think>";
1334+
}
1335+
adjusted_messages.push_back(adjusted_msg);
1336+
}
1337+
}
1338+
1339+
auto prompt = apply(tmpl, inputs,
1340+
/* messages_override= */ adjusted_messages,
1341+
/* tools_override= */ std::nullopt,
1342+
additional_context);
13201343
data.prompt = prompt;
13211344
data.format = COMMON_CHAT_FORMAT_DEEPSEEK_V3_1;
13221345
if (string_ends_with(data.prompt, "<think>\n")) {

0 commit comments

Comments
 (0)