@@ -548,6 +548,8 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
548548 " \" <|tool▁call▁begin|>function<|tool▁sep|>" + name + " \\ n```json\\ n\" " + args_rule + " \" ```<|tool▁call▁end|>\" " ));
549549 });
550550 data.grammar_triggers .push_back ({" <|tool▁calls▁begin|>" , /* .at_start = */ false });
551+ data.grammar_triggers .push_back ({" <|tool_calls_begin|>" , /* .at_start = */ false });
552+ data.grammar_triggers .push_back ({" <|tool calls begin|>" , /* .at_start = */ false });
551553 data.preserved_tokens = {
552554 " <think>" ,
553555 " </think>" ,
@@ -557,8 +559,10 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
557559 " <|tool▁call▁end|>" ,
558560 };
559561 builder.add_rule (" root" ,
560- " \" <|tool▁calls▁begin|>\" "
561- " (" +string_join (tool_rules, " | " ) + " )" + (inputs.parallel_tool_calls ? " *" : " " ) +
562+ // Distill Qwen 7B & 32B models seem confused re/ syntax of their tool call opening tag,
563+ // so we accept common variants (then it's all constrained)
564+ " ( \" <|tool▁calls▁begin|>\" | \" <|tool_calls_begin|>\" | \" <|tool calls begin|>\" ) "
565+ " (" +string_join (tool_rules, " | " ) + " )" + (inputs.parallel_tool_calls ? " *" : " " ) + " "
562566 " \" <|tool▁calls▁end|>\" "
563567 " space" );
564568 }, grammar_options);
@@ -581,7 +585,7 @@ static common_chat_params common_chat_params_init_deepseek_r1(const common_chat_
581585 return data;
582586}
583587static common_chat_msg common_chat_parse_deepseek_r1 (const std::string & input) {
584- static std::regex trigger_regex (" <|tool▁calls▁begin|>" );
588+ static std::regex trigger_regex (" <|tool▁calls▁begin|>|<|tool_calls_begin|>|<|tool calls begin|> " );
585589 static std::regex function_regex (" <|tool▁call▁begin|>function<|tool▁sep|>([^\n ]+)\n ```json\n " );
586590 static std::regex close_regex (" ```<|tool▁call▁end|>" );
587591 static std::regex think_regex (R"( <think>([\s\S\n]*)</think>([\s\S\r\n]*))" );
@@ -591,6 +595,9 @@ static common_chat_msg common_chat_parse_deepseek_r1(const std::string & input)
591595 msg.thoughts = string_trim (match[1 ].str ());
592596 msg.content = string_trim (match[2 ].str ());
593597 }
598+ if (msg.content == " <|tool▁calls▁end|>" ) {
599+ msg.content = " " ;
600+ }
594601 return msg;
595602}
596603
0 commit comments