llama-chat.cpp 32 KB

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