return thoughts in reasoning_content field

This commit is contained in:
Olivier Chafik 2025-02-04 11:37:09 +00:00
parent d1b66910c5
commit 39c1d8163b
5 changed files with 23 additions and 23 deletions

View file

@ -623,13 +623,13 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input) {
static std::regex function_regex("<tool▁call▁begin>function<tool▁sep>([^\n]+)\n```json\n");
static std::regex close_regex("```[\\s\\r\\n]*<tool▁call▁end>");
static std::regex thoughts_regex("(?:<think>([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
static std::regex reasoning_content_regex("(?:<think>([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
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>");
common_chat_msg msg;
msg.role = "assistant";
std::smatch match;
if (std::regex_match(input, match, thoughts_regex)) {
msg.thoughts = string_trim(match[1].str());
if (std::regex_match(input, match, reasoning_content_regex)) {
msg.reasoning_content = string_trim(match[1].str());
auto rest = match[2].str();
if (std::regex_search(rest, match, tool_calls_regex)) {

View file

@ -623,7 +623,7 @@ struct common_chat_msg {
std::string role;
std::string content;
std::vector<common_tool_call> tool_calls;
std::string thoughts = "";
std::string reasoning_content = "";
std::string tool_plan = "";
};

View file

@ -745,8 +745,8 @@ struct server_task_result_cmpl_final : server_task_result {
{"tool_calls", tool_calls},
{"role", "assistant"},
};
if (!msg.thoughts.empty()) {
message["thoughts"] = msg.thoughts;
if (!msg.reasoning_content.empty()) {
message["reasoning_content"] = msg.reasoning_content;
}
if (!msg.tool_plan.empty()) {
message["tool_plan"] = msg.tool_plan;

View file

@ -436,12 +436,12 @@ def test_calc_result(result_override: str | None, n_predict: int, hf_repo: str,
@pytest.mark.slow
@pytest.mark.parametrize("n_predict,expect_content,expect_thoughts,hf_repo,template_override", [
@pytest.mark.parametrize("n_predict,expect_content,expect_reasoning_content,hf_repo,template_override", [
(128, "^The sum of 102 and 7 is 109.*", None, "bartowski/Phi-3.5-mini-instruct-GGUF:Q4_K_M", None),
(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),
(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)),
])
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):
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):
global server
server.n_slots = 1
server.jinja = True
@ -470,9 +470,9 @@ def test_thoughts(n_predict: int, expect_content: str | None, expect_thoughts: s
if expect_content is not None:
assert re.match(expect_content, content), f'Expected {expect_content}, got {content}'
thoughts = choice["message"].get("thoughts")
if expect_thoughts is not None:
assert re.match(expect_thoughts, thoughts), f'Expected {expect_thoughts}, got {thoughts}'
reasoning_content = choice["message"].get("reasoning_content")
if expect_reasoning_content is not None:
assert re.match(expect_reasoning_content, reasoning_content), f'Expected {expect_reasoning_content}, got {reasoning_content}'
@pytest.mark.slow

View file

@ -26,8 +26,8 @@ static common_chat_msg msg_from_json(const json & message) {
if (message.contains("tool_plan")) {
ret.tool_plan = message.at("tool_plan");
}
if (message.contains("thoughts")) {
ret.thoughts = message.at("thoughts");
if (message.contains("reasoning_content")) {
ret.reasoning_content = message.at("reasoning_content");
}
auto has_tool_calls = message.contains("tool_calls");
if (has_tool_calls) {
@ -108,7 +108,7 @@ static std::string dump(const json & j) {
static void assert_msg_equals(const common_chat_msg & expected, const common_chat_msg & actual) {
assert_equals(expected.role, actual.role);
assert_equals(expected.content, actual.content);
assert_equals(expected.thoughts, actual.thoughts);
assert_equals(expected.reasoning_content, actual.reasoning_content);
assert_equals(expected.tool_plan, actual.tool_plan);
assert_equals(expected.tool_calls.size(), actual.tool_calls.size());
for (size_t i = 0; i < expected.tool_calls.size(); i++) {
@ -293,10 +293,10 @@ static void test_template_output_parsers() {
{ "role", "assistant" },
{ "content", "Hello, world!" },
};
json text_thoughts_message {
json text_reasoning_message {
{ "role", "assistant" },
{ "content", "Hello, world!" },
{ "thoughts", "I'm thinking" },
{ "reasoning_content", "I'm thinking" },
};
json tool_calls = json::array({{
{ "type", "function" },
@ -316,10 +316,10 @@ static void test_template_output_parsers() {
},
}},
};
json tool_call_thoughts_message = {
json tool_call_reasoning_message = {
{ "role", "assistant" },
{ "content", nullptr },
{ "thoughts", "I'm\nthinking" },
{ "reasoning_content", "I'm\nthinking" },
{ "tool_calls", {
{
{ "type", "function" },
@ -589,8 +589,8 @@ static void test_template_output_parsers() {
assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_params_init(tmpl, inputs_tools).format);
test_template(tmpl, end_tokens, text_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
test_template(tmpl, end_tokens, text_thoughts_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
assert_msg_equals(msg_from_json(text_thoughts_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
test_template(tmpl, end_tokens, text_reasoning_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
assert_msg_equals(msg_from_json(text_reasoning_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
// test_template(tmpl, end_tokens, tool_call_message, tools,
// "<tool▁calls▁begin><tool▁call▁begin>function<tool▁sep>special_function\n"
// "```json\n"
@ -609,10 +609,10 @@ static void test_template_output_parsers() {
assert_equals(COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_params_init(tmpl, inputs_tools).format);
test_template(tmpl, end_tokens, text_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
test_template(tmpl, end_tokens, text_thoughts_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
assert_msg_equals(msg_from_json(text_thoughts_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
test_template(tmpl, end_tokens, text_reasoning_message, tools, "Hello, world!", /* expect_grammar_triggered= */ false);
assert_msg_equals(msg_from_json(text_reasoning_message), common_chat_parse("<think>I'm thinking</think>Hello, world!", COMMON_CHAT_FORMAT_DEEPSEEK_R1));
assert_msg_equals(msg_from_json(tool_call_thoughts_message),
assert_msg_equals(msg_from_json(tool_call_reasoning_message),
common_chat_parse(
"<think>I'm\nthinking</think>\n\n"
"<tool▁calls▁begin><tool▁call▁begin>function<tool▁sep>special_function\n"