llama-chat.cpp 25 KB

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