Skip to content

Commit 91542ca

Browse files
author
ochafik
committed
tool-calls: allow r1 output to miss <think> opening tag (since latest template update adds it)
1 parent e598e7a commit 91542ca

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

common/chat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
640640
static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input, bool extract_reasoning) {
641641
static std::regex function_regex("<|tool▁call▁begin|>function<|tool▁sep|>([^\n]+)\n```json\n");
642642
static std::regex close_regex("```[\\s\\r\\n]*<|tool▁call▁end|>");
643-
static std::regex reasoning_content_regex("(<think>([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
643+
static std::regex reasoning_content_regex("((?:<think>)?([\\s\\S\\r\\n]*?)</think>)?([\\s\\S\\r\\n]*)");
644644
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|>");
645645
common_chat_msg msg;
646646
msg.role = "assistant";

tests/test-chat.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ static void test_template_output_parsers() {
675675
assert_msg_equals(msg_from_json(message_assist_thoughts),
676676
common_chat_parse("<think>I'm thinking</think>Hello, world!\nWhat's up?",
677677
COMMON_CHAT_FORMAT_DEEPSEEK_R1_EXTRACT_REASONING));
678+
assert_msg_equals(msg_from_json(message_assist_thoughts),
679+
// Latest template update (ast of 20250209) adds a trailing <think>\n if add_generation_prompt is true.
680+
common_chat_parse("I'm thinking</think>Hello, world!\nWhat's up?",
681+
COMMON_CHAT_FORMAT_DEEPSEEK_R1_EXTRACT_REASONING));
678682
// test_template(tmpl, end_tokens, message_assist_call, tools,
679683
// "<|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>special_function\n"
680684
// "```json\n"

0 commit comments

Comments
 (0)