Skip to content

Commit 39c1d81

Browse files
author
Olivier Chafik
committed
return thoughts in reasoning_content field
1 parent d1b6691 commit 39c1d81

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

common/chat.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,13 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
623623
static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input) {
624624
static std::regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
625625
static std::regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");
626-
static std::regex thoughts_regex("(?:<think>([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
626+
static std::regex reasoning_content_regex("(?:<think>([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
627627
static std::regex tool_calls_regex("[\\s\\r\\n]*(?:<|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|>|<|tool\\\\_calls\\\\_begin|>)([\\s\\S\\r\\n]*?)<|tool▁calls▁end|>");
628628
common_chat_msg msg;
629629
msg.role = "assistant";
630630
std::smatch match;
631-
if (std::regex_match(input, match, thoughts_regex)) {
632-
msg.thoughts = string_trim(match[1].str());
631+
if (std::regex_match(input, match, reasoning_content_regex)) {
632+
msg.reasoning_content = string_trim(match[1].str());
633633
auto rest = match[2].str();
634634

635635
if (std::regex_search(rest, match, tool_calls_regex)) {

common/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ struct common_chat_msg {
623623
std::string role;
624624
std::string content;
625625
std::vector<common_tool_call> tool_calls;
626-
std::string thoughts = "";
626+
std::string reasoning_content = "";
627627
std::string tool_plan = "";
628628
};
629629

examples/server/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ struct server_task_result_cmpl_final : server_task_result {
745745
{"tool_calls", tool_calls},
746746
{"role", "assistant"},
747747
};
748-
if (!msg.thoughts.empty()) {
749-
message["thoughts"] = msg.thoughts;
748+
if (!msg.reasoning_content.empty()) {
749+
message["reasoning_content"] = msg.reasoning_content;
750750
}
751751
if (!msg.tool_plan.empty()) {
752752
message["tool_plan"] = msg.tool_plan;

examples/server/tests/unit/test_tool_call.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ def test_calc_result(result_override: str | None, n_predict: int, hf_repo: str,
436436

437437

438438
@pytest.mark.slow
439-
@pytest.mark.parametrize("n_predict,expect_content,expect_thoughts,hf_repo,template_override", [
439+
@pytest.mark.parametrize("n_predict,expect_content,expect_reasoning_content,hf_repo,template_override", [
440440
(128, "^The sum of 102 and 7 is 109.*", None, "bartowski/Phi-3.5-mini-instruct-GGUF:Q4_K_M", None),
441441
(1024, "To find the sum of.*", "I need to calculate the sum of 102 and 7.*", "bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF:Q4_K_M", None),
442442
(1024, "To find the sum of.*", "First, I need to add the tens place.*", "bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF:Q4_K_M", ("llama-cpp-deepseek-r1", None)),
443443
])
444-
def test_thoughts(n_predict: int, expect_content: str | None, expect_thoughts: str | None, hf_repo: str, template_override: str | Tuple[str, str | None] | None):
444+
def test_reasoning_content(n_predict: int, expect_content: str | None, expect_reasoning_content: str | None, hf_repo: str, template_override: str | Tuple[str, str | None] | None):
445445
global server
446446
server.n_slots = 1
447447
server.jinja = True
@@ -470,9 +470,9 @@ def test_thoughts(n_predict: int, expect_content: str | None, expect_thoughts: s
470470
if expect_content is not None:
471471
assert re.match(expect_content, content), f'Expected {expect_content}, got {content}'
472472

473-
thoughts = choice["message"].get("thoughts")
474-
if expect_thoughts is not None:
475-
assert re.match(expect_thoughts, thoughts), f'Expected {expect_thoughts}, got {thoughts}'
473+
reasoning_content = choice["message"].get("reasoning_content")
474+
if expect_reasoning_content is not None:
475+
assert re.match(expect_reasoning_content, reasoning_content), f'Expected {expect_reasoning_content}, got {reasoning_content}'
476476

477477

478478
@pytest.mark.slow

tests/test-chat.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ static common_chat_msg msg_from_json(const json & message) {
2626
if (message.contains("tool_plan")) {
2727
ret.tool_plan = message.at("tool_plan");
2828
}
29-
if (message.contains("thoughts")) {
30-
ret.thoughts = message.at("thoughts");
29+
if (message.contains("reasoning_content")) {
30+
ret.reasoning_content = message.at("reasoning_content");
3131
}
3232
auto has_tool_calls = message.contains("tool_calls");
3333
if (has_tool_calls) {
@@ -108,7 +108,7 @@ static std::string dump(const json & j) {
108108
static void assert_msg_equals(const common_chat_msg & expected, const common_chat_msg & actual) {
109109
assert_equals(expected.role, actual.role);
110110
assert_equals(expected.content, actual.content);
111-
assert_equals(expected.thoughts, actual.thoughts);
111+
assert_equals(expected.reasoning_content, actual.reasoning_content);
112112
assert_equals(expected.tool_plan, actual.tool_plan);
113113
assert_equals(expected.tool_calls.size(), actual.tool_calls.size());
114114
for (size_t i = 0; i < expected.tool_calls.size(); i++) {
@@ -293,10 +293,10 @@ static void test_template_output_parsers() {
293293
{ "role", "assistant" },
294294
{ "content", "Hello, world!" },
295295
};
296-
json text_thoughts_message {
296+
json text_reasoning_message {
297297
{ "role", "assistant" },
298298
{ "content", "Hello, world!" },
299-
{ "thoughts", "I'm thinking" },
299+
{ "reasoning_content", "I'm thinking" },
300300
};
301301
json tool_calls = json::array({{
302302
{ "type", "function" },
@@ -316,10 +316,10 @@ static void test_template_output_parsers() {
316316
},
317317
}},
318318
};
319-
json tool_call_thoughts_message = {
319+
json tool_call_reasoning_message = {
320320
{ "role", "assistant" },
321321
{ "content", nullptr },
322-
{ "thoughts", "I'm\nthinking" },
322+
{ "reasoning_content", "I'm\nthinking" },
323323
{ "tool_calls", {
324324
{
325325
{ "type", "function" },
@@ -589,8 +589,8 @@ static void test_template_output_parsers() {
589589
assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_params_init(tmpl, inputs_tools).format);
590590

591591
test_template(tmpl, end_tokens, text_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
592-
test_template(tmpl, end_tokens, text_thoughts_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
593-
assert_msg_equals(msg_from_json(text_thoughts_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
592+
test_template(tmpl, end_tokens, text_reasoning_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
593+
assert_msg_equals(msg_from_json(text_reasoning_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
594594
// test_template(tmpl, end_tokens, tool_call_message, tools,
595595
// "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
596596
// "```json\n"
@@ -609,10 +609,10 @@ static void test_template_output_parsers() {
609609
assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_params_init(tmpl, inputs_tools).format);
610610

611611
test_template(tmpl, end_tokens, text_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
612-
test_template(tmpl, end_tokens, text_thoughts_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
613-
assert_msg_equals(msg_from_json(text_thoughts_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
612+
test_template(tmpl, end_tokens, text_reasoning_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
613+
assert_msg_equals(msg_from_json(text_reasoning_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
614614

615-
assert_msg_equals(msg_from_json(tool_call_thoughts_message),
615+
assert_msg_equals(msg_from_json(tool_call_reasoning_message),
616616
common_chat_parse(
617617
"<think>I'm\nthinking</think>\n\n"
618618
"<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"

0 commit comments

Comments
 (0)