@@ -58,6 +58,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
5858 { " granite" , LLM_CHAT_TEMPLATE_GRANITE },
5959 { " gigachat" , LLM_CHAT_TEMPLATE_GIGACHAT },
6060 { " megrez" , LLM_CHAT_TEMPLATE_MEGREZ },
61+ { " velvet" , LLM_CHAT_TEMPLATE_VELVET },
6162};
6263
6364llm_chat_template llm_chat_template_from_str (const std::string & name) {
@@ -167,6 +168,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
167168 return LLM_CHAT_TEMPLATE_GIGACHAT;
168169 } else if (tmpl_contains (" <|role_start|>" )) {
169170 return LLM_CHAT_TEMPLATE_MEGREZ;
171+ } else if (tmpl_contains (" <instruction>" )) {
172+ return LLM_CHAT_TEMPLATE_VELVET;
170173 }
171174 return LLM_CHAT_TEMPLATE_UNKNOWN;
172175}
@@ -566,10 +569,32 @@ int32_t llm_chat_apply_template(
566569 if (add_ass) {
567570 ss << " <|role_start|>assistant<|role_end|>" ;
568571 }
572+ } else if (tmpl == LLM_CHAT_TEMPLATE_VELVET) {
573+ // Velvet template
574+ std::string leading_space = " " ;
575+ std::string trailing_space = " " ;
576+ bool trim_assistant_message = true ;
577+ bool is_inside_turn = false ;
578+ for (auto message : chat) {
579+ if (!is_inside_turn) {
580+ ss << leading_space << " <instruction>" << trailing_space;
581+ is_inside_turn = true ;
582+ }
583+ std::string role (message->role );
584+ std::string content (message->content );
585+ if (role == " system" ) {
586+ ss << content << " \n\n " ;
587+ } else if (role == " user" ) {
588+ ss << content << leading_space << " </instruction>" ;
589+ } else {
590+ ss << trailing_space << (trim_assistant_message ? trim (content) : content) << " </s>" ;
591+ is_inside_turn = false ;
592+ }
593+ }
569594 } else {
570595 // template not supported
571596 return -1 ;
572- }
597+ }
573598 dest = ss.str ();
574599 return dest.size ();
575600}
0 commit comments