@@ -327,7 +327,17 @@ static void test_templates(const struct common_chat_templates * tmpls, const std
327327 {
328328 const auto & pattern = trigger.value ;
329329 if (std::regex_match (constrained, match, std::regex (pattern))) {
330- pos = match.position (1 );
330+ auto mpos = std::string::npos;
331+ for (size_t i = 1 ; i < match.size (); ++i) {
332+ if (match[i].length () > 0 ) {
333+ mpos = match.position (i);
334+ break ;
335+ }
336+ }
337+ if (mpos == std::string::npos) {
338+ mpos = match.position (0 );
339+ }
340+ pos = mpos;
331341 }
332342 break ;
333343 }
@@ -469,6 +479,15 @@ const common_chat_msg message_assist_call {
469479 /* .tool_name = */ " " ,
470480 /* .tool_call_id = */ " " ,
471481};
482+ const common_chat_msg message_assist_call_content {
483+ " assistant" ,
484+ " Hello, world!\n What's up?" ,
485+ /* .content_parts = */ {},
486+ tool_calls,
487+ /* .reasoning_content = */ " " ,
488+ /* .tool_name = */ " " ,
489+ /* .tool_call_id = */ " " ,
490+ };
472491const common_chat_msg message_assist_thoughts_no_content {
473492 " assistant" ,
474493 " " ,
@@ -722,8 +741,11 @@ static void test_template_output_parsers() {
722741 auto tmpls = read_templates (" models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja" );
723742 std::vector<std::string> end_tokens{ " <|END_OF_TURN_TOKEN|>" };
724743
725- assert_equals (COMMON_CHAT_FORMAT_COMMAND_R7B, common_chat_templates_apply (tmpls.get (), inputs_no_tools).format );
726- assert_equals (COMMON_CHAT_FORMAT_COMMAND_R7B, common_chat_templates_apply (tmpls.get (), inputs_tools).format );
744+ for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
745+ auto params = common_chat_templates_apply (tmpls.get (), inputs);
746+ assert_equals (COMMON_CHAT_FORMAT_COMMAND_R7B, params.format );
747+ assert_equals (false , params.thinking_forced_open );
748+ }
727749
728750 assert_msg_equals (message_assist,
729751 common_chat_parse (
@@ -877,11 +899,25 @@ static void test_template_output_parsers() {
877899 tmpls.get (), end_tokens, message_assist_call_id, tools,
878900 " [TOOL_CALLS][{\" name\" : \" special_function\" , \" arguments\" : {\" arg1\" : 1}, \" id\" : \" 123456789\" }]" );
879901 }
902+ {
903+ auto tmpls = read_templates (" models/templates/Qwen-QwQ-32B.jinja" );
904+ std::vector<std::string> end_tokens{ " <|im_end|>" };
905+
906+ for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
907+ auto params = common_chat_templates_apply (tmpls.get (), inputs);
908+ assert_equals (COMMON_CHAT_FORMAT_HERMES_2_PRO, params.format );
909+ assert_equals (true , params.thinking_forced_open );
910+ }
911+ }
880912 {
881913 auto tmpls = read_templates (" models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja" );
882914 std::vector<std::string> end_tokens{ " <|im_end|>" };
883915
884- assert_equals (COMMON_CHAT_FORMAT_HERMES_2_PRO, common_chat_templates_apply (tmpls.get (), inputs_tools).format );
916+ for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
917+ auto params = common_chat_templates_apply (tmpls.get (), inputs);
918+ assert_equals (COMMON_CHAT_FORMAT_HERMES_2_PRO, params.format );
919+ assert_equals (false , params.thinking_forced_open );
920+ }
885921 assert_equals (
886922 COMMON_CHAT_FORMAT_HERMES_2_PRO,
887923 common_chat_templates_apply (
@@ -937,6 +973,13 @@ static void test_template_output_parsers() {
937973 " </tool_call>" ,
938974 /* is_partial= */ false ,
939975 {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
976+ assert_msg_equals (message_assist_call_content,
977+ common_chat_parse (
978+ " Hello, world!\n What's up?<tool_call>\n "
979+ " {\" name\" : \" special_function\" , \" arguments\" : {\" arg1\" : 1}}\n "
980+ " </tool_call>" ,
981+ /* is_partial= */ false ,
982+ {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
940983 assert_msg_equals (
941984 message_assist_call,
942985 common_chat_parse (
@@ -1066,6 +1109,27 @@ static void test_template_output_parsers() {
10661109 /* is_partial= */ false ,
10671110 {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
10681111
1112+ assert_msg_equals (
1113+ {
1114+ /* .role = */ " assistant" ,
1115+ " This is not a tool call:\n "
1116+ " {\" name\" : \" special_function\" , \" arguments\" : {\" arg1\" : 1}}" ,
1117+ /* .content_parts = */ {},
1118+ /* .tool_calls = */ {},
1119+ /* .reasoning_content = */ " " ,
1120+ /* .tool_name = */ " " ,
1121+ /* .tool_call_id = */ " " ,
1122+ },
1123+ common_chat_parse (
1124+ " This is not a tool call:\n "
1125+ " {\" name\" : \" special_function\" , \" arguments\" : {\" arg1\" : 1}}" ,
1126+ /* is_partial= */ false ,
1127+ {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
1128+ assert_msg_equals (message_assist,
1129+ common_chat_parse (
1130+ " Hello, world!\n What's up?" ,
1131+ /* is_partial= */ false ,
1132+ {COMMON_CHAT_FORMAT_HERMES_2_PRO}));
10691133 assert_msg_equals (message_assist_thoughts_unparsed_deepseek,
10701134 common_chat_parse (
10711135 " <think>I'm thinking</think>Hello, world!\n What's up?" ,
@@ -1162,7 +1226,7 @@ static void test_template_output_parsers() {
11621226 is_partial,
11631227 {COMMON_CHAT_FORMAT_FUNCTIONARY_V3_1_LLAMA_3_1}));
11641228 }
1165-
1229+
11661230 test_templates (tmpls.get (), end_tokens, message_assist, tools, " Hello, world!\n What's up?" , /* expect_grammar_triggered= */ false );
11671231 test_templates (tmpls.get (), end_tokens, message_assist_call, tools,
11681232 " <function=special_function>{\" arg1\" : 1}</function>" );
@@ -1243,7 +1307,11 @@ static void test_template_output_parsers() {
12431307 auto tmpls = read_templates (" models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja" );
12441308 std::vector<std::string> end_tokens{ " <|end▁of▁sentence|>" };
12451309
1246- assert_equals (COMMON_CHAT_FORMAT_DEEPSEEK_R1, common_chat_templates_apply (tmpls.get (), inputs_tools).format );
1310+ for (const auto & inputs : { inputs_no_tools, inputs_tools }) {
1311+ auto params = common_chat_templates_apply (tmpls.get (), inputs);
1312+ assert_equals (COMMON_CHAT_FORMAT_DEEPSEEK_R1, params.format );
1313+ assert_equals (true , params.thinking_forced_open );
1314+ }
12471315
12481316 test_templates (tmpls.get (), end_tokens, message_assist, tools, " Hello, world!\n What's up?" , /* expect_grammar_triggered= */ false );
12491317 test_templates (tmpls.get (), end_tokens, message_assist_thoughts, tools, " Hello, world!\n What's up?" , /* expect_grammar_triggered= */ false );
0 commit comments