llama-chat.cpp 30 KB

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