llama-chat.cpp 26 KB

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