llama-chat.cpp 27 KB

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