Skip to content

Commit 3d00d62

Browse files
committed
Update the test
1 parent fe86282 commit 3d00d62

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

common/chat.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ const char * common_chat_format_name(common_chat_format format) {
618618
case COMMON_CHAT_FORMAT_FIREFUNCTION_V2: return "FireFunction v2";
619619
case COMMON_CHAT_FORMAT_FUNCTIONARY_V3_2: return "Functionary v3.2";
620620
case COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1: return "Functionary v3.1 Llama 3.1";
621+
case COMMON_CHAT_FORMAT_DEEPSEEK_V3_1: return "DeepSeek V3.1";
621622
case COMMON_CHAT_FORMAT_HERMES_2_PRO: return "Hermes 2 Pro";
622623
case COMMON_CHAT_FORMAT_COMMAND_R7B: return "Command R7B";
623624
case COMMON_CHAT_FORMAT_GRANITE: return "Granite";
@@ -1352,18 +1353,15 @@ static void common_chat_parse_deepseek_v3_1(common_chat_msg_parser & builder) {
13521353

13531354
// First, try to find the "</think>" tag that separates thinking from regular content
13541355
static const common_regex thinking_end_regex("</think>");
1355-
if (auto res = builder.try_find_regex(thinking_end_regex)) {
1356-
// Extract everything before "</think>" as reasoning content
1357-
auto reasoning_content = builder.str(common_string_range{0, res->groups[0].begin});
1358-
auto stripped_reasoning = string_strip(reasoning_content);
1356+
if (auto res = builder.try_find_regex(thinking_end_regex, std::string::npos, false)) {
1357+
// The prelude contains everything before the "</think>" tag
1358+
auto stripped_reasoning = string_strip(res->prelude);
13591359

13601360
if (!stripped_reasoning.empty()) {
13611361
builder.add_reasoning_content(stripped_reasoning);
13621362
}
13631363

1364-
// Move past the "</think>" tag
1365-
builder.move_to(res->groups[0].end);
1366-
1364+
// The parser position is already advanced past the "</think>" tag by try_find_regex
13671365
// The rest is regular content
13681366
builder.add_content(builder.consume_rest());
13691367
} else {

tests/test-chat-parser.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,51 @@ static void test_deepseek_v3_1() {
197197
/* .thinking_forced_open = */ false,
198198
/* .parse_tool_calls = */ true,
199199
};
200-
common_chat_msg_parser builder("REASONING</think>ok", /* is_partial= */ false, {});
200+
common_chat_msg_parser builder("REASONING</think>ok", /* is_partial= */ false, syntax);
201+
assert_equals(std::string("REASONING"), builder.result().reasoning_content);
202+
assert_equals(std::string("ok"), builder.result().content);
203+
}
204+
205+
// Test with whitespace around reasoning content
206+
{
207+
common_chat_syntax syntax = {
208+
/* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
209+
/* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
210+
/* .reasoning_in_content = */ false,
211+
/* .thinking_forced_open = */ false,
212+
/* .parse_tool_calls = */ true,
213+
};
214+
common_chat_msg_parser builder(" REASONING WITH SPACES </think>ok", /* is_partial= */ false, syntax);
215+
assert_equals(std::string("REASONING WITH SPACES"), builder.result().reasoning_content);
216+
assert_equals(std::string("ok"), builder.result().content);
217+
}
218+
219+
// Test without thinking tag (should be all regular content)
220+
{
221+
common_chat_syntax syntax = {
222+
/* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
223+
/* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
224+
/* .reasoning_in_content = */ false,
225+
/* .thinking_forced_open = */ false,
226+
/* .parse_tool_calls = */ true,
227+
};
228+
common_chat_msg_parser builder("just regular content", /* is_partial= */ false, syntax);
229+
assert_equals(std::string(""), builder.result().reasoning_content);
230+
assert_equals(std::string("just regular content"), builder.result().content);
231+
}
232+
233+
// Test with empty reasoning content
234+
{
235+
common_chat_syntax syntax = {
236+
/* .format = */ COMMON_CHAT_FORMAT_DEEPSEEK_V3_1,
237+
/* .reasoning_format = */ COMMON_REASONING_FORMAT_DEEPSEEK,
238+
/* .reasoning_in_content = */ false,
239+
/* .thinking_forced_open = */ false,
240+
/* .parse_tool_calls = */ true,
241+
};
242+
common_chat_msg_parser builder(" </think>ok", /* is_partial= */ false, syntax);
243+
assert_equals(std::string(""), builder.result().reasoning_content);
244+
assert_equals(std::string("ok"), builder.result().content);
201245
}
202246
}
203247

@@ -362,6 +406,7 @@ int main() {
362406
test_json_with_dumped_args();
363407
test_reasoning();
364408
test_regex();
409+
test_deepseek_v3_1();
365410
std::cout << "All tests passed!\n";
366411
return 0;
367412
}

0 commit comments

Comments
 (0)