llama-chat.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. #include "llama-chat.h"
  2. #include "llama.h"
  3. #include <map>
  4. #include <sstream>
  5. #include <algorithm>
  6. #if __cplusplus >= 202000L
  7. #define LU8(x) (const char*)(u8##x)
  8. #else
  9. #define LU8(x) u8##x
  10. #endif
  11. // trim whitespace from the beginning and end of a string
  12. static std::string trim(const std::string & str) {
  13. size_t start = 0;
  14. size_t end = str.size();
  15. while (start < end && isspace(str[start])) {
  16. start += 1;
  17. }
  18. while (end > start && isspace(str[end - 1])) {
  19. end -= 1;
  20. }
  21. return str.substr(start, end - start);
  22. }
  23. static const std::map<std::string, llm_chat_template> LLM_CHAT_TEMPLATES = {
  24. { "chatml", LLM_CHAT_TEMPLATE_CHATML },
  25. { "llama2", LLM_CHAT_TEMPLATE_LLAMA_2 },
  26. { "llama2-sys", LLM_CHAT_TEMPLATE_LLAMA_2_SYS },
  27. { "llama2-sys-bos", LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS },
  28. { "llama2-sys-strip", LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP },
  29. { "mistral-v1", LLM_CHAT_TEMPLATE_MISTRAL_V1 },
  30. { "mistral-v3", LLM_CHAT_TEMPLATE_MISTRAL_V3 },
  31. { "mistral-v3-tekken", LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN },
  32. { "mistral-v7", LLM_CHAT_TEMPLATE_MISTRAL_V7 },
  33. { "mistral-v7-tekken", LLM_CHAT_TEMPLATE_MISTRAL_V7_TEKKEN },
  34. { "phi3", LLM_CHAT_TEMPLATE_PHI_3 },
  35. { "phi4", LLM_CHAT_TEMPLATE_PHI_4 },
  36. { "falcon3", LLM_CHAT_TEMPLATE_FALCON_3 },
  37. { "zephyr", LLM_CHAT_TEMPLATE_ZEPHYR },
  38. { "monarch", LLM_CHAT_TEMPLATE_MONARCH },
  39. { "gemma", LLM_CHAT_TEMPLATE_GEMMA },
  40. { "orion", LLM_CHAT_TEMPLATE_ORION },
  41. { "openchat", LLM_CHAT_TEMPLATE_OPENCHAT },
  42. { "vicuna", LLM_CHAT_TEMPLATE_VICUNA },
  43. { "vicuna-orca", LLM_CHAT_TEMPLATE_VICUNA_ORCA },
  44. { "deepseek", LLM_CHAT_TEMPLATE_DEEPSEEK },
  45. { "deepseek2", LLM_CHAT_TEMPLATE_DEEPSEEK_2 },
  46. { "deepseek3", LLM_CHAT_TEMPLATE_DEEPSEEK_3 },
  47. { "command-r", LLM_CHAT_TEMPLATE_COMMAND_R },
  48. { "llama3", LLM_CHAT_TEMPLATE_LLAMA_3 },
  49. { "chatglm3", LLM_CHAT_TEMPLATE_CHATGLM_3 },
  50. { "chatglm4", LLM_CHAT_TEMPLATE_CHATGLM_4 },
  51. { "glmedge", LLM_CHAT_TEMPLATE_GLMEDGE },
  52. { "minicpm", LLM_CHAT_TEMPLATE_MINICPM },
  53. { "exaone3", LLM_CHAT_TEMPLATE_EXAONE_3 },
  54. { "exaone4", LLM_CHAT_TEMPLATE_EXAONE_4 },
  55. { "rwkv-world", LLM_CHAT_TEMPLATE_RWKV_WORLD },
  56. { "granite", LLM_CHAT_TEMPLATE_GRANITE },
  57. { "gigachat", LLM_CHAT_TEMPLATE_GIGACHAT },
  58. { "megrez", LLM_CHAT_TEMPLATE_MEGREZ },
  59. { "yandex", LLM_CHAT_TEMPLATE_YANDEX },
  60. { "bailing", LLM_CHAT_TEMPLATE_BAILING },
  61. { "llama4", LLM_CHAT_TEMPLATE_LLAMA4 },
  62. { "smolvlm", LLM_CHAT_TEMPLATE_SMOLVLM },
  63. { "hunyuan-moe", LLM_CHAT_TEMPLATE_HUNYUAN_MOE },
  64. { "gpt-oss", LLM_CHAT_TEMPLATE_OPENAI_MOE },
  65. { "hunyuan-dense", LLM_CHAT_TEMPLATE_HUNYUAN_DENSE },
  66. { "kimi-k2", LLM_CHAT_TEMPLATE_KIMI_K2 },
  67. };
  68. llm_chat_template llm_chat_template_from_str(const std::string & name) {
  69. return LLM_CHAT_TEMPLATES.at(name);
  70. }
  71. llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
  72. try {
  73. return llm_chat_template_from_str(tmpl);
  74. } catch (const std::out_of_range &) {
  75. // ignore
  76. }
  77. auto tmpl_contains = [&tmpl](const char * haystack) -> bool {
  78. return tmpl.find(haystack) != std::string::npos;
  79. };
  80. if (tmpl_contains("<|im_start|>")) {
  81. return tmpl_contains("<|im_sep|>")
  82. ? LLM_CHAT_TEMPLATE_PHI_4
  83. : tmpl_contains("<end_of_utterance>")
  84. ? LLM_CHAT_TEMPLATE_SMOLVLM // SmolVLM uses <|im_start|> as BOS, but it is NOT chatml
  85. : LLM_CHAT_TEMPLATE_CHATML;
  86. } else if (tmpl.find("mistral") == 0 || tmpl_contains("[INST]")) {
  87. if (tmpl_contains("[SYSTEM_PROMPT]")) {
  88. return LLM_CHAT_TEMPLATE_MISTRAL_V7;
  89. } else if (
  90. // catches official 'v1' template
  91. tmpl_contains("' [INST] ' + system_message")
  92. // catches official 'v3' and 'v3-tekken' templates
  93. || tmpl_contains("[AVAILABLE_TOOLS]")
  94. ) {
  95. // Official mistral 'v1', 'v3' and 'v3-tekken' templates
  96. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  97. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  98. if (tmpl_contains(" [INST]")) {
  99. return LLM_CHAT_TEMPLATE_MISTRAL_V1;
  100. } else if (tmpl_contains("\"[INST]\"")) {
  101. return LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN;
  102. }
  103. return LLM_CHAT_TEMPLATE_MISTRAL_V3;
  104. } else {
  105. // llama2 template and its variants
  106. // [variant] support system message
  107. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  108. bool support_system_message = tmpl_contains("<<SYS>>");
  109. bool add_bos_inside_history = tmpl_contains("bos_token + '[INST]");
  110. bool strip_message = tmpl_contains("content.strip()");
  111. if (strip_message) {
  112. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  113. } else if (add_bos_inside_history) {
  114. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  115. } else if (support_system_message) {
  116. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS;
  117. } else {
  118. return LLM_CHAT_TEMPLATE_LLAMA_2;
  119. }
  120. }
  121. } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|end|>")) {
  122. return LLM_CHAT_TEMPLATE_PHI_3;
  123. } else if (tmpl_contains("[gMASK]<sop>")) {
  124. return LLM_CHAT_TEMPLATE_CHATGLM_4;
  125. } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|user|>")) {
  126. return tmpl_contains("</s>") ? LLM_CHAT_TEMPLATE_FALCON_3 : LLM_CHAT_TEMPLATE_GLMEDGE;
  127. } else if (tmpl_contains("<|{{ item['role'] }}|>") && tmpl_contains("<|begin_of_image|>")) {
  128. return LLM_CHAT_TEMPLATE_GLMEDGE;
  129. } else if (tmpl_contains("<|user|>") && tmpl_contains("<|endoftext|>")) {
  130. return LLM_CHAT_TEMPLATE_ZEPHYR;
  131. } else if (tmpl_contains("bos_token + message['role']")) {
  132. return LLM_CHAT_TEMPLATE_MONARCH;
  133. } else if (tmpl_contains("<start_of_turn>")) {
  134. return LLM_CHAT_TEMPLATE_GEMMA;
  135. } else if (tmpl_contains("'\\n\\nAssistant: ' + eos_token")) {
  136. // OrionStarAI/Orion-14B-Chat
  137. return LLM_CHAT_TEMPLATE_ORION;
  138. } else if (tmpl_contains("GPT4 Correct ")) {
  139. // openchat/openchat-3.5-0106
  140. return LLM_CHAT_TEMPLATE_OPENCHAT;
  141. } else if (tmpl_contains("USER: ") && tmpl_contains("ASSISTANT: ")) {
  142. // eachadea/vicuna-13b-1.1 (and Orca variant)
  143. if (tmpl_contains("SYSTEM: ")) {
  144. return LLM_CHAT_TEMPLATE_VICUNA_ORCA;
  145. }
  146. return LLM_CHAT_TEMPLATE_VICUNA;
  147. } else if (tmpl_contains("### Instruction:") && tmpl_contains("<|EOT|>")) {
  148. // deepseek-ai/deepseek-coder-33b-instruct
  149. return LLM_CHAT_TEMPLATE_DEEPSEEK;
  150. } else if (tmpl_contains("<|START_OF_TURN_TOKEN|>") && tmpl_contains("<|USER_TOKEN|>")) {
  151. // CohereForAI/c4ai-command-r-plus
  152. return LLM_CHAT_TEMPLATE_COMMAND_R;
  153. } else if (tmpl_contains("<|start_header_id|>") && tmpl_contains("<|end_header_id|>")) {
  154. return LLM_CHAT_TEMPLATE_LLAMA_3;
  155. } else if (tmpl_contains("[gMASK]sop")) {
  156. // chatglm3-6b
  157. return LLM_CHAT_TEMPLATE_CHATGLM_3;
  158. } else if (tmpl_contains(LU8("<用户>"))) {
  159. // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  160. return LLM_CHAT_TEMPLATE_MINICPM;
  161. } else if (tmpl_contains("'Assistant: ' + message['content'] + eos_token")) {
  162. return LLM_CHAT_TEMPLATE_DEEPSEEK_2;
  163. } else if (tmpl_contains(LU8("<|Assistant|>")) && tmpl_contains(LU8("<|User|>")) && tmpl_contains(LU8("<|end▁of▁sentence|>"))) {
  164. return LLM_CHAT_TEMPLATE_DEEPSEEK_3;
  165. } else if (tmpl_contains("[|system|]") && tmpl_contains("[|assistant|]") && tmpl_contains("[|endofturn|]")) {
  166. if (tmpl_contains("[|tool|]")) {
  167. return LLM_CHAT_TEMPLATE_EXAONE_4;
  168. }
  169. // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  170. // EXAONE-3.0-7.8B-Instruct
  171. return LLM_CHAT_TEMPLATE_EXAONE_3;
  172. } else if (tmpl_contains("rwkv-world") || tmpl_contains("{{- 'User: ' + message['content']|trim + '\\n\\n' -}}")) {
  173. return LLM_CHAT_TEMPLATE_RWKV_WORLD;
  174. } else if (tmpl_contains("<|start_of_role|>")) {
  175. return LLM_CHAT_TEMPLATE_GRANITE;
  176. } else if (tmpl_contains("message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1]")) {
  177. return LLM_CHAT_TEMPLATE_GIGACHAT;
  178. } else if (tmpl_contains("<|role_start|>")) {
  179. return LLM_CHAT_TEMPLATE_MEGREZ;
  180. } else if (tmpl_contains(" Ассистент:")) {
  181. return LLM_CHAT_TEMPLATE_YANDEX;
  182. } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("'HUMAN'")) {
  183. return LLM_CHAT_TEMPLATE_BAILING;
  184. } else if (tmpl_contains("<|header_start|>") && tmpl_contains("<|header_end|>")) {
  185. return LLM_CHAT_TEMPLATE_LLAMA4;
  186. } else if (tmpl_contains("<|endofuserprompt|>")) {
  187. return LLM_CHAT_TEMPLATE_DOTS1;
  188. } else if (tmpl_contains("<|startoftext|>") && tmpl_contains("<|extra_4|>")) {
  189. return LLM_CHAT_TEMPLATE_HUNYUAN_MOE;
  190. } else if (tmpl_contains("<|start|>") && tmpl_contains("<|channel|>")) {
  191. return LLM_CHAT_TEMPLATE_OPENAI_MOE;
  192. } else if (tmpl_contains("<|hy_place▁holder▁no▁2|>") && tmpl_contains("<|hy_place▁holder▁no▁3|>")) {
  193. return LLM_CHAT_TEMPLATE_HUNYUAN_DENSE;
  194. } else if (tmpl_contains("<|im_assistant|>assistant<|im_middle|>")) {
  195. return LLM_CHAT_TEMPLATE_KIMI_K2;
  196. }
  197. return LLM_CHAT_TEMPLATE_UNKNOWN;
  198. }
  199. // Simple version of "llama_apply_chat_template" that only works with strings
  200. // This function uses heuristic checks to determine commonly used template. It is not a jinja parser.
  201. int32_t llm_chat_apply_template(
  202. llm_chat_template tmpl,
  203. const std::vector<const llama_chat_message *> & chat,
  204. std::string & dest, bool add_ass) {
  205. // Taken from the research: https://github.com/ggerganov/llama.cpp/issues/5527
  206. std::stringstream ss;
  207. if (tmpl == LLM_CHAT_TEMPLATE_CHATML) {
  208. // chatml template
  209. for (auto message : chat) {
  210. ss << "<|im_start|>" << message->role << "\n" << message->content << "<|im_end|>\n";
  211. }
  212. if (add_ass) {
  213. ss << "<|im_start|>assistant\n";
  214. }
  215. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7_TEKKEN) {
  216. // Official mistral 'v7' template
  217. // See: https://huggingface.co/mistralai/Mistral-Large-Instruct-2411#basic-instruct-template-v7
  218. // https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503#basic-instruct-template-v7-tekken
  219. const char * trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 ? " " : "";
  220. for (auto message : chat) {
  221. std::string role(message->role);
  222. std::string content(message->content);
  223. if (role == "system") {
  224. ss << "[SYSTEM_PROMPT]" << trailing_space << content << "[/SYSTEM_PROMPT]";
  225. } else if (role == "user") {
  226. ss << "[INST]" << trailing_space << content << "[/INST]";
  227. } else {
  228. ss << trailing_space << content << "</s>";
  229. }
  230. }
  231. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1
  232. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3
  233. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN) {
  234. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  235. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  236. std::string leading_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1 ? " " : "";
  237. std::string trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN ? "" : " ";
  238. bool trim_assistant_message = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3;
  239. bool is_inside_turn = false;
  240. for (auto message : chat) {
  241. if (!is_inside_turn) {
  242. ss << leading_space << "[INST]" << trailing_space;
  243. is_inside_turn = true;
  244. }
  245. std::string role(message->role);
  246. std::string content(message->content);
  247. if (role == "system") {
  248. ss << content << "\n\n";
  249. } else if (role == "user") {
  250. ss << content << leading_space << "[/INST]";
  251. } else {
  252. ss << trailing_space << (trim_assistant_message ? trim(content) : content) << "</s>";
  253. is_inside_turn = false;
  254. }
  255. }
  256. } else if (
  257. tmpl == LLM_CHAT_TEMPLATE_LLAMA_2
  258. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS
  259. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS
  260. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP) {
  261. // llama2 template and its variants
  262. // [variant] support system message
  263. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  264. bool support_system_message = tmpl != LLM_CHAT_TEMPLATE_LLAMA_2;
  265. // [variant] add BOS inside history
  266. bool add_bos_inside_history = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  267. // [variant] trim spaces from the input message
  268. bool strip_message = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  269. // construct the prompt
  270. bool is_inside_turn = true; // skip BOS at the beginning
  271. ss << "[INST] ";
  272. for (auto message : chat) {
  273. std::string content = strip_message ? trim(message->content) : message->content;
  274. std::string role(message->role);
  275. if (!is_inside_turn) {
  276. is_inside_turn = true;
  277. ss << (add_bos_inside_history ? "<s>[INST] " : "[INST] ");
  278. }
  279. if (role == "system") {
  280. if (support_system_message) {
  281. ss << "<<SYS>>\n" << content << "\n<</SYS>>\n\n";
  282. } else {
  283. // if the model does not support system message, we still include it in the first message, but without <<SYS>>
  284. ss << content << "\n";
  285. }
  286. } else if (role == "user") {
  287. ss << content << " [/INST]";
  288. } else {
  289. ss << content << "</s>";
  290. is_inside_turn = false;
  291. }
  292. }
  293. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_3) {
  294. // Phi 3
  295. for (auto message : chat) {
  296. std::string role(message->role);
  297. ss << "<|" << role << "|>\n" << message->content << "<|end|>\n";
  298. }
  299. if (add_ass) {
  300. ss << "<|assistant|>\n";
  301. }
  302. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_4) {
  303. // chatml template
  304. for (auto message : chat) {
  305. ss << "<|im_start|>" << message->role << "<|im_sep|>" << message->content << "<|im_end|>";
  306. }
  307. if (add_ass) {
  308. ss << "<|im_start|>assistant<|im_sep|>";
  309. }
  310. } else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
  311. // Falcon 3
  312. for (auto message : chat) {
  313. std::string role(message->role);
  314. ss << "<|" << role << "|>\n" << message->content << "\n";
  315. }
  316. if (add_ass) {
  317. ss << "<|assistant|>\n";
  318. }
  319. } else if (tmpl == LLM_CHAT_TEMPLATE_ZEPHYR) {
  320. // zephyr template
  321. for (auto message : chat) {
  322. ss << "<|" << message->role << "|>" << "\n" << message->content << "<|endoftext|>\n";
  323. }
  324. if (add_ass) {
  325. ss << "<|assistant|>\n";
  326. }
  327. } else if (tmpl == LLM_CHAT_TEMPLATE_MONARCH) {
  328. // mlabonne/AlphaMonarch-7B template (the <s> is included inside history)
  329. for (auto message : chat) {
  330. std::string bos = (message == chat.front()) ? "" : "<s>"; // skip BOS for first message
  331. ss << bos << message->role << "\n" << message->content << "</s>\n";
  332. }
  333. if (add_ass) {
  334. ss << "<s>assistant\n";
  335. }
  336. } else if (tmpl == LLM_CHAT_TEMPLATE_GEMMA) {
  337. // google/gemma-7b-it
  338. std::string system_prompt = "";
  339. for (auto message : chat) {
  340. std::string role(message->role);
  341. if (role == "system") {
  342. // there is no system message for gemma, but we will merge it with user prompt, so nothing is broken
  343. system_prompt += trim(message->content);
  344. continue;
  345. }
  346. // in gemma, "assistant" is "model"
  347. role = role == "assistant" ? "model" : message->role;
  348. ss << "<start_of_turn>" << role << "\n";
  349. if (!system_prompt.empty() && role != "model") {
  350. ss << system_prompt << "\n\n";
  351. system_prompt = "";
  352. }
  353. ss << trim(message->content) << "<end_of_turn>\n";
  354. }
  355. if (add_ass) {
  356. ss << "<start_of_turn>model\n";
  357. }
  358. } else if (tmpl == LLM_CHAT_TEMPLATE_ORION) {
  359. // OrionStarAI/Orion-14B-Chat
  360. std::string system_prompt = "";
  361. for (auto message : chat) {
  362. std::string role(message->role);
  363. if (role == "system") {
  364. // there is no system message support, we will merge it with user prompt
  365. system_prompt += message->content;
  366. continue;
  367. } else if (role == "user") {
  368. ss << "Human: ";
  369. if (!system_prompt.empty()) {
  370. ss << system_prompt << "\n\n";
  371. system_prompt = "";
  372. }
  373. ss << message->content << "\n\nAssistant: </s>";
  374. } else {
  375. ss << message->content << "</s>";
  376. }
  377. }
  378. } else if (tmpl == LLM_CHAT_TEMPLATE_OPENCHAT) {
  379. // openchat/openchat-3.5-0106,
  380. for (auto message : chat) {
  381. std::string role(message->role);
  382. if (role == "system") {
  383. ss << message->content << "<|end_of_turn|>";
  384. } else {
  385. role[0] = toupper(role[0]);
  386. ss << "GPT4 Correct " << role << ": " << message->content << "<|end_of_turn|>";
  387. }
  388. }
  389. if (add_ass) {
  390. ss << "GPT4 Correct Assistant:";
  391. }
  392. } else if (tmpl == LLM_CHAT_TEMPLATE_VICUNA || tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  393. // eachadea/vicuna-13b-1.1 (and Orca variant)
  394. for (auto message : chat) {
  395. std::string role(message->role);
  396. if (role == "system") {
  397. // Orca-Vicuna variant uses a system prefix
  398. if (tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  399. ss << "SYSTEM: " << message->content << "\n";
  400. } else {
  401. ss << message->content << "\n\n";
  402. }
  403. } else if (role == "user") {
  404. ss << "USER: " << message->content << "\n";
  405. } else if (role == "assistant") {
  406. ss << "ASSISTANT: " << message->content << "</s>\n";
  407. }
  408. }
  409. if (add_ass) {
  410. ss << "ASSISTANT:";
  411. }
  412. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK) {
  413. // deepseek-ai/deepseek-coder-33b-instruct
  414. for (auto message : chat) {
  415. std::string role(message->role);
  416. if (role == "system") {
  417. ss << message->content;
  418. } else if (role == "user") {
  419. ss << "### Instruction:\n" << message->content << "\n";
  420. } else if (role == "assistant") {
  421. ss << "### Response:\n" << message->content << "\n<|EOT|>\n";
  422. }
  423. }
  424. if (add_ass) {
  425. ss << "### Response:\n";
  426. }
  427. } else if (tmpl == LLM_CHAT_TEMPLATE_COMMAND_R) {
  428. // CohereForAI/c4ai-command-r-plus
  429. for (auto message : chat) {
  430. std::string role(message->role);
  431. if (role == "system") {
  432. ss << "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  433. } else if (role == "user") {
  434. ss << "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  435. } else if (role == "assistant") {
  436. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  437. }
  438. }
  439. if (add_ass) {
  440. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>";
  441. }
  442. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA_3) {
  443. // Llama 3
  444. for (auto message : chat) {
  445. std::string role(message->role);
  446. ss << "<|start_header_id|>" << role << "<|end_header_id|>\n\n" << trim(message->content) << "<|eot_id|>";
  447. }
  448. if (add_ass) {
  449. ss << "<|start_header_id|>assistant<|end_header_id|>\n\n";
  450. }
  451. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_3) {
  452. // chatglm3-6b
  453. ss << "[gMASK]" << "sop";
  454. for (auto message : chat) {
  455. std::string role(message->role);
  456. ss << "<|" << role << "|>" << "\n " << message->content;
  457. }
  458. if (add_ass) {
  459. ss << "<|assistant|>";
  460. }
  461. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_4) {
  462. ss << "[gMASK]" << "<sop>";
  463. for (auto message : chat) {
  464. std::string role(message->role);
  465. ss << "<|" << role << "|>" << "\n" << message->content;
  466. }
  467. if (add_ass) {
  468. ss << "<|assistant|>\n";
  469. }
  470. } else if (tmpl == LLM_CHAT_TEMPLATE_GLMEDGE) {
  471. for (auto message : chat) {
  472. std::string role(message->role);
  473. ss << "<|" << role << "|>" << "\n" << message->content;
  474. }
  475. if (add_ass) {
  476. ss << "<|assistant|>";
  477. }
  478. } else if (tmpl == LLM_CHAT_TEMPLATE_MINICPM) {
  479. // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  480. for (auto message : chat) {
  481. std::string role(message->role);
  482. if (role == "user") {
  483. ss << LU8("<用户>");
  484. ss << trim(message->content);
  485. ss << "<AI>";
  486. } else {
  487. ss << trim(message->content);
  488. }
  489. }
  490. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_2) {
  491. // DeepSeek-V2
  492. for (auto message : chat) {
  493. std::string role(message->role);
  494. if (role == "system") {
  495. ss << message->content << "\n\n";
  496. } else if (role == "user") {
  497. ss << "User: " << message->content << "\n\n";
  498. } else if (role == "assistant") {
  499. ss << "Assistant: " << message->content << LU8("<|end▁of▁sentence|>");
  500. }
  501. }
  502. if (add_ass) {
  503. ss << "Assistant:";
  504. }
  505. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_3) {
  506. // DeepSeek-V3
  507. for (auto message : chat) {
  508. std::string role(message->role);
  509. if (role == "system") {
  510. ss << message->content << "\n\n";
  511. } else if (role == "user") {
  512. ss << LU8("<|User|>") << message->content;
  513. } else if (role == "assistant") {
  514. ss << LU8("<|Assistant|>") << message->content << LU8("<|end▁of▁sentence|>");
  515. }
  516. }
  517. if (add_ass) {
  518. ss << LU8("<|Assistant|>");
  519. }
  520. } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_3) {
  521. // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  522. // EXAONE-3.0-7.8B-Instruct
  523. for (auto message : chat) {
  524. std::string role(message->role);
  525. if (role == "system") {
  526. ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  527. } else if (role == "user") {
  528. ss << "[|user|]" << trim(message->content) << "\n";
  529. } else if (role == "assistant") {
  530. ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  531. }
  532. }
  533. if (add_ass) {
  534. ss << "[|assistant|]";
  535. }
  536. } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_4) {
  537. for (auto message : chat) {
  538. std::string role(message->role);
  539. if (role == "system") {
  540. ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  541. } else if (role == "user") {
  542. ss << "[|user|]" << trim(message->content) << "\n";
  543. } else if (role == "assistant") {
  544. ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  545. } else if (role == "tool") {
  546. ss << "[|tool|]" << trim(message->content) << "[|endofturn|]\n";
  547. }
  548. }
  549. if (add_ass) {
  550. ss << "[|assistant|]";
  551. }
  552. } else if (tmpl == LLM_CHAT_TEMPLATE_RWKV_WORLD) {
  553. // this template requires the model to have "\n\n" as EOT token
  554. for (size_t i = 0; i < chat.size(); i++) {
  555. std::string role(chat[i]->role);
  556. if (role == "system") {
  557. ss << "System: " << trim(chat[i]->content) << "\n\n";
  558. } else if (role == "user") {
  559. ss << "User: " << trim(chat[i]->content) << "\n\n";
  560. if (i == chat.size() - 1) {
  561. ss << "Assistant:";
  562. }
  563. } else if (role == "assistant") {
  564. ss << "Assistant: " << trim(chat[i]->content) << "\n\n";
  565. }
  566. }
  567. } else if (tmpl == LLM_CHAT_TEMPLATE_GRANITE) {
  568. // IBM Granite template
  569. for (const auto & message : chat) {
  570. std::string role(message->role);
  571. ss << "<|start_of_role|>" << role << "<|end_of_role|>";
  572. if (role == "assistant_tool_call") {
  573. ss << "<|tool_call|>";
  574. }
  575. ss << message->content << "<|end_of_text|>\n";
  576. }
  577. if (add_ass) {
  578. ss << "<|start_of_role|>assistant<|end_of_role|>\n";
  579. }
  580. } else if (tmpl == LLM_CHAT_TEMPLATE_GIGACHAT) {
  581. // GigaChat template
  582. bool has_system = !chat.empty() && std::string(chat[0]->role) == "system";
  583. // Handle system message if present
  584. if (has_system) {
  585. ss << "<s>" << chat[0]->content << "<|message_sep|>";
  586. } else {
  587. ss << "<s>";
  588. }
  589. // Process remaining messages
  590. for (size_t i = has_system ? 1 : 0; i < chat.size(); i++) {
  591. std::string role(chat[i]->role);
  592. if (role == "user") {
  593. ss << "user<|role_sep|>" << chat[i]->content << "<|message_sep|>"
  594. << "available functions<|role_sep|>[]<|message_sep|>";
  595. } else if (role == "assistant") {
  596. ss << "assistant<|role_sep|>" << chat[i]->content << "<|message_sep|>";
  597. }
  598. }
  599. // Add generation prompt if needed
  600. if (add_ass) {
  601. ss << "assistant<|role_sep|>";
  602. }
  603. } else if (tmpl == LLM_CHAT_TEMPLATE_MEGREZ) {
  604. // Megrez template
  605. for (auto message : chat) {
  606. std::string role(message->role);
  607. ss << "<|role_start|>" << role << "<|role_end|>" << message->content << "<|turn_end|>";
  608. }
  609. if (add_ass) {
  610. ss << "<|role_start|>assistant<|role_end|>";
  611. }
  612. } else if (tmpl == LLM_CHAT_TEMPLATE_YANDEX) {
  613. // Yandex template ("\n\n" is defined as EOT token)
  614. ss << "<s>";
  615. for (size_t i = 0; i < chat.size(); i++) {
  616. std::string role(chat[i]->role);
  617. if (role == "user") {
  618. ss << " Пользователь: " << chat[i]->content << "\n\n";
  619. } else if (role == "assistant") {
  620. ss << " Ассистент: " << chat[i]->content << "\n\n";
  621. }
  622. }
  623. // Add generation prompt if needed
  624. if (add_ass) {
  625. ss << " Ассистент:[SEP]";
  626. }
  627. } else if (tmpl == LLM_CHAT_TEMPLATE_BAILING) {
  628. // Bailing (Ling) template
  629. for (auto message : chat) {
  630. std::string role(message->role);
  631. if (role == "user") {
  632. role = "HUMAN";
  633. } else {
  634. std::transform(role.begin(), role.end(), role.begin(), ::toupper);
  635. }
  636. ss << "<role>" << role << "</role>" << message->content;
  637. }
  638. if (add_ass) {
  639. ss << "<role>ASSISTANT</role>";
  640. }
  641. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA4) {
  642. // Llama 4
  643. for (auto message : chat) {
  644. std::string role(message->role);
  645. ss << "<|header_start|>" << role << "<|header_end|>\n\n" << trim(message->content) << "<|eot|>";
  646. }
  647. if (add_ass) {
  648. ss << "<|header_start|>assistant<|header_end|>\n\n";
  649. }
  650. } else if (tmpl == LLM_CHAT_TEMPLATE_SMOLVLM) {
  651. // SmolVLM
  652. ss << "<|im_start|>"; // uses <|im_start|> as BOS, but the actual content is NOT chatml
  653. for (auto message : chat) {
  654. std::string role(message->role);
  655. if (role == "system") {
  656. ss << message->content << "\n\n";
  657. } else if (role == "user") {
  658. ss << "User: " << message->content << "<end_of_utterance>\n";
  659. } else {
  660. ss << "Assistant: " << message->content << "<end_of_utterance>\n";
  661. }
  662. }
  663. if (add_ass) {
  664. ss << "Assistant:";
  665. }
  666. } else if (tmpl == LLM_CHAT_TEMPLATE_DOTS1) {
  667. // dots.llm1.inst (DOTS1)
  668. for (auto message : chat) {
  669. std::string role(message->role);
  670. if (role == "system") {
  671. ss << "<|system|>" << message->content << "<|endofsystem|>";
  672. } else if (role == "user") {
  673. ss << "<|userprompt|>" << message->content << "<|endofuserprompt|>";
  674. } else {
  675. ss << "<|response|>" << message->content << "<|endofresponse|>";
  676. }
  677. }
  678. if (add_ass) {
  679. ss << "<|response|>";
  680. }
  681. } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_MOE) {
  682. // tencent/Hunyuan-A13B-Instruct
  683. for (auto message : chat) {
  684. std::string role(message->role);
  685. if (role == "system") {
  686. ss << "<|startoftext|>" << message->content << "<|extra_4|>";
  687. } else if (role == "assistant") {
  688. ss << message->content << "<|eos|>";
  689. } else {
  690. ss << "<|startoftext|>" << message->content << "<|extra_0|>";
  691. }
  692. }
  693. } else if (tmpl == LLM_CHAT_TEMPLATE_OPENAI_MOE) {
  694. // OpenAI MoE (based on Harmony chat template)
  695. for (auto message : chat) {
  696. std::string role(message->role);
  697. ss << "<|start|>" << role << "<|message|>" << message->content;
  698. ss << (role == "assistant" ? "<|return|>" : "<|end|>");
  699. }
  700. if (add_ass) {
  701. ss << "<|start|>assistant";
  702. }
  703. } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_DENSE) {
  704. // tencent/Hunyuan-4B-Instruct
  705. for (size_t i = 0; i < chat.size(); i++) {
  706. std::string role(chat[i]->role);
  707. if (i == 0) {
  708. if (role == "system") {
  709. ss << chat[i]->content << "<|hy_place▁holder▁no▁3|>";
  710. }
  711. }
  712. if (role == "assistant") {
  713. ss << "<|hy_Assistant|>" << chat[i]->content << "<|hy_place▁holder▁no▁2|>";
  714. } else if (role == "user") {
  715. ss << "<|hy_User|>" << chat[i]->content << "<|hy_Assistant|>";
  716. }
  717. }
  718. } else if (tmpl == LLM_CHAT_TEMPLATE_KIMI_K2) {
  719. // moonshotai/Kimi-K2-Instruct
  720. for (auto message : chat) {
  721. std::string role(message->role);
  722. if (role == "system") {
  723. ss << "<|im_system|>system<|im_middle|>";
  724. } else if (role == "user") {
  725. ss << "<|im_user|>user<|im_middle|>";
  726. } else if (role == "assistant") {
  727. ss << "<|im_assistant|>assistant<|im_middle|>";
  728. } else if (role == "tool") {
  729. ss << "<|im_system|>tool<|im_middle|>";
  730. }
  731. ss << message->content << "<|im_end|>";
  732. }
  733. if (add_ass) {
  734. ss << "<|im_assistant|>assistant<|im_middle|>";
  735. }
  736. } else {
  737. // template not supported
  738. return -1;
  739. }
  740. dest = ss.str();
  741. return dest.size();
  742. }
  743. // public interface
  744. int32_t llama_chat_builtin_templates(const char ** output, size_t len) {
  745. auto it = LLM_CHAT_TEMPLATES.begin();
  746. for (size_t i = 0; i < std::min(len, LLM_CHAT_TEMPLATES.size()); i++) {
  747. output[i] = it->first.c_str();
  748. std::advance(it, 1);
  749. }
  750. return (int32_t) LLM_CHAT_TEMPLATES.size();
  751. }