Skip to content

Commit 7242dd9

Browse files
vorobyov01Sergei Vorobevngxson
authored
llama-chat : Add Yandex instruct model template support (#12621)
* add yandex template * update yandex chat template * fix tests * adjust chat template * fix style * fix tool macro in template * add clarify comment --------- Co-authored-by: Sergei Vorobev <serv01@yandex-team.ru> Co-authored-by: Xuan-Son Nguyen <thichthat@gmail.com>
1 parent 492d7f1 commit 7242dd9

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/llama-chat.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
5959
{ "granite", LLM_CHAT_TEMPLATE_GRANITE },
6060
{ "gigachat", LLM_CHAT_TEMPLATE_GIGACHAT },
6161
{ "megrez", LLM_CHAT_TEMPLATE_MEGREZ },
62+
{ "yandex", LLM_CHAT_TEMPLATE_YANDEX },
6263
};
6364

6465
llm_chat_template llm_chat_template_from_str(const std::string & name) {
@@ -168,6 +169,8 @@ llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
168169
return LLM_CHAT_TEMPLATE_GIGACHAT;
169170
} else if (tmpl_contains("<|role_start|>")) {
170171
return LLM_CHAT_TEMPLATE_MEGREZ;
172+
} else if (tmpl_contains(" Ассистент:")) {
173+
return LLM_CHAT_TEMPLATE_YANDEX;
171174
}
172175
return LLM_CHAT_TEMPLATE_UNKNOWN;
173176
}
@@ -567,6 +570,24 @@ int32_t llm_chat_apply_template(
567570
if (add_ass) {
568571
ss << "<|role_start|>assistant<|role_end|>";
569572
}
573+
} else if (tmpl == LLM_CHAT_TEMPLATE_YANDEX) {
574+
// Yandex template ("\n\n" is defined as EOT token)
575+
576+
ss << "<s>";
577+
578+
for (size_t i = 0; i < chat.size(); i++) {
579+
std::string role(chat[i]->role);
580+
if (role == "user") {
581+
ss << " Пользователь: " << chat[i]->content << "\n\n";
582+
} else if (role == "assistant") {
583+
ss << " Ассистент: " << chat[i]->content << "\n\n";
584+
}
585+
}
586+
587+
// Add generation prompt if needed
588+
if (add_ass) {
589+
ss << " Ассистент:[SEP]";
590+
}
570591
} else {
571592
// template not supported
572593
return -1;
@@ -585,4 +606,3 @@ int32_t llama_chat_builtin_templates(const char ** output, size_t len) {
585606
}
586607
return (int32_t) LLM_CHAT_TEMPLATES.size();
587608
}
588-

src/llama-chat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum llm_chat_template {
3838
LLM_CHAT_TEMPLATE_GRANITE,
3939
LLM_CHAT_TEMPLATE_GIGACHAT,
4040
LLM_CHAT_TEMPLATE_MEGREZ,
41+
LLM_CHAT_TEMPLATE_YANDEX,
4142
LLM_CHAT_TEMPLATE_UNKNOWN,
4243
};
4344

tests/test-chat-template.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ int main(void) {
270270
/* .bos_token= */ "",
271271
/* .eos_token= */ "",
272272
},
273+
{
274+
/* .name= */ "yandex/YandexGPT-5-Lite-8B-instruct",
275+
/* .template_str= */ "<s>{%- set names = {'assistant': ' Ассистент:', 'user': ' Пользователь:'} %}\n{%- set tools_prefix = 'Тебе доступны следующие функции:' %}\n{%- macro __render_tool(tool) %}\n {%- set name = tool.function.name %}\n {%- set description = tool.function.description|default('') %}\n {%- set parameters = tool.function.parameters|tojson %}\n {{- '\\n' }}function {{ '{' }}'name':'{{ name }}',\n {%- if tool.function.description %}'description':'{{ description }}',{% endif %}\n'parameters':{{ parameters }}\n {{- '}' }}\n{%- endmacro %}\n{%- macro __render_tools(tools) %}\n {{- tools_prefix }}\n {%- for tool in tools %}\n {{- __render_tool(tool) }}\n {%- endfor %}\n {{- '\\n\\n' }}\n{%- endmacro %}\n{%- macro __render_tool_message(message) %}\n {{- '\\n\\nРезультат вызова' }} {{ message.name }}: {{ message.content }} {{ '\\n\\n' }}\n{%- endmacro %}\n{%- if tools -%}\n {{- __render_tools(tools) }}\n{%- endif -%}\n{%- macro __render_user_message(message) %}\n{{ names.user }} {{ message.content + '\\n\\n' }}\n{%- endmacro %}\n{%- macro __render_assistant_message(message) %}\n {{- names.assistant }}\n {%- set call = message['function_call'] %}\n {%- if call %}\n {{- '\\n[TOOL_CALL_START]' }}{{ call.name }}{{ '\\n' }}{{ call.arguments|tojson }}\n {%- else %}\n {{- ' ' + message.content + '\\n\\n' }}\n {%- endif %}\n{%- endmacro %}\n{%- if not add_generation_prompt is defined %}\n{%- set add_generation_prompt = false %}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'user' %}\n {{- __render_user_message(message) }}\n {%- endif %}\n {%- if message.role == 'assistant' and not loop.last %}\n {{- __render_assistant_message(message) }}\n {%- endif %}\n {%- if message.role == 'tool' %}\n {{- __render_tool_message(message) }}\n {%- endif %}\n {%- if loop.last %}\n {{- ' Ассистент:[SEP]' }}\n {%- endif %}\n{%- endfor %}\n",
276+
/* .expected_output= */ "<s> Пользователь: Hello\n\n Ассистент: Hi there\n\n Пользователь: Who are you\n\n Ассистент: I am an assistant \n\n Пользователь: Another question\n\n Ассистент:[SEP]",
277+
/* .expected_output_jinja= */ "<s> Пользователь: You are a helpful assistant\nHello\n\n Ассистент: Hi there\n\n Пользователь: Who are you\n\n Ассистент: I am an assistant \n\n Пользователь: Another question\n\n Ассистент:[SEP]",
278+
/* .bos_token= */ "",
279+
/* .eos_token= */ "",
280+
},
273281
};
274282
std::vector<char> formatted_chat(1024);
275283
int32_t res;

0 commit comments

Comments
 (0)