llama-chat.cpp 34 KB

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