llama-chat.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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_CHATGLM_3 },
  49. { "chatglm4", LLM_CHAT_TEMPLATE_CHATGLM_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. { "smolvlm", LLM_CHAT_TEMPLATE_SMOLVLM },
  61. };
  62. llm_chat_template llm_chat_template_from_str(const std::string & name) {
  63. return LLM_CHAT_TEMPLATES.at(name);
  64. }
  65. llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
  66. try {
  67. return llm_chat_template_from_str(tmpl);
  68. } catch (const std::out_of_range &) {
  69. // ignore
  70. }
  71. auto tmpl_contains = [&tmpl](const char * haystack) -> bool {
  72. return tmpl.find(haystack) != std::string::npos;
  73. };
  74. if (tmpl_contains("<|im_start|>")) {
  75. return tmpl_contains("<|im_sep|>")
  76. ? LLM_CHAT_TEMPLATE_PHI_4
  77. : tmpl_contains("<end_of_utterance>")
  78. ? LLM_CHAT_TEMPLATE_SMOLVLM // SmolVLM uses <|im_start|> as BOS, but it is NOT chatml
  79. : LLM_CHAT_TEMPLATE_CHATML;
  80. } else if (tmpl.find("mistral") == 0 || tmpl_contains("[INST]")) {
  81. if (tmpl_contains("[SYSTEM_PROMPT]")) {
  82. return LLM_CHAT_TEMPLATE_MISTRAL_V7;
  83. } else if (
  84. // catches official 'v1' template
  85. tmpl_contains("' [INST] ' + system_message")
  86. // catches official 'v3' and 'v3-tekken' templates
  87. || tmpl_contains("[AVAILABLE_TOOLS]")
  88. ) {
  89. // Official mistral 'v1', 'v3' and 'v3-tekken' templates
  90. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  91. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  92. if (tmpl_contains(" [INST]")) {
  93. return LLM_CHAT_TEMPLATE_MISTRAL_V1;
  94. } else if (tmpl_contains("\"[INST]\"")) {
  95. return LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN;
  96. }
  97. return LLM_CHAT_TEMPLATE_MISTRAL_V3;
  98. } else {
  99. // llama2 template and its variants
  100. // [variant] support system message
  101. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  102. bool support_system_message = tmpl_contains("<<SYS>>");
  103. bool add_bos_inside_history = tmpl_contains("bos_token + '[INST]");
  104. bool strip_message = tmpl_contains("content.strip()");
  105. if (strip_message) {
  106. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  107. } else if (add_bos_inside_history) {
  108. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  109. } else if (support_system_message) {
  110. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS;
  111. } else {
  112. return LLM_CHAT_TEMPLATE_LLAMA_2;
  113. }
  114. }
  115. } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|end|>")) {
  116. return LLM_CHAT_TEMPLATE_PHI_3;
  117. } else if (tmpl_contains("[gMASK]<sop>")) {
  118. return LLM_CHAT_TEMPLATE_CHATGLM_4;
  119. } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|user|>")) {
  120. return tmpl_contains("</s>") ? LLM_CHAT_TEMPLATE_FALCON_3 : LLM_CHAT_TEMPLATE_GLMEDGE;
  121. } else if (tmpl_contains("<|{{ item['role'] }}|>") && tmpl_contains("<|begin_of_image|>")) {
  122. return LLM_CHAT_TEMPLATE_GLMEDGE;
  123. } else if (tmpl_contains("<|user|>") && tmpl_contains("<|endoftext|>")) {
  124. return LLM_CHAT_TEMPLATE_ZEPHYR;
  125. } else if (tmpl_contains("bos_token + message['role']")) {
  126. return LLM_CHAT_TEMPLATE_MONARCH;
  127. } else if (tmpl_contains("<start_of_turn>")) {
  128. return LLM_CHAT_TEMPLATE_GEMMA;
  129. } else if (tmpl_contains("'\\n\\nAssistant: ' + eos_token")) {
  130. // OrionStarAI/Orion-14B-Chat
  131. return LLM_CHAT_TEMPLATE_ORION;
  132. } else if (tmpl_contains("GPT4 Correct ")) {
  133. // openchat/openchat-3.5-0106
  134. return LLM_CHAT_TEMPLATE_OPENCHAT;
  135. } else if (tmpl_contains("USER: ") && tmpl_contains("ASSISTANT: ")) {
  136. // eachadea/vicuna-13b-1.1 (and Orca variant)
  137. if (tmpl_contains("SYSTEM: ")) {
  138. return LLM_CHAT_TEMPLATE_VICUNA_ORCA;
  139. }
  140. return LLM_CHAT_TEMPLATE_VICUNA;
  141. } else if (tmpl_contains("### Instruction:") && tmpl_contains("<|EOT|>")) {
  142. // deepseek-ai/deepseek-coder-33b-instruct
  143. return LLM_CHAT_TEMPLATE_DEEPSEEK;
  144. } else if (tmpl_contains("<|START_OF_TURN_TOKEN|>") && tmpl_contains("<|USER_TOKEN|>")) {
  145. // CohereForAI/c4ai-command-r-plus
  146. return LLM_CHAT_TEMPLATE_COMMAND_R;
  147. } else if (tmpl_contains("<|start_header_id|>") && tmpl_contains("<|end_header_id|>")) {
  148. return LLM_CHAT_TEMPLATE_LLAMA_3;
  149. } else if (tmpl_contains("[gMASK]sop")) {
  150. // chatglm3-6b
  151. return LLM_CHAT_TEMPLATE_CHATGLM_3;
  152. } else if (tmpl_contains(LU8("<用户>"))) {
  153. // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  154. return LLM_CHAT_TEMPLATE_MINICPM;
  155. } else if (tmpl_contains("'Assistant: ' + message['content'] + eos_token")) {
  156. return LLM_CHAT_TEMPLATE_DEEPSEEK_2;
  157. } else if (tmpl_contains(LU8("<|Assistant|>")) && tmpl_contains(LU8("<|User|>")) && tmpl_contains(LU8("<|end▁of▁sentence|>"))) {
  158. return LLM_CHAT_TEMPLATE_DEEPSEEK_3;
  159. } else if (tmpl_contains("[|system|]") && tmpl_contains("[|assistant|]") && tmpl_contains("[|endofturn|]")) {
  160. // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  161. // EXAONE-3.0-7.8B-Instruct
  162. return LLM_CHAT_TEMPLATE_EXAONE_3;
  163. } else if (tmpl_contains("rwkv-world")) {
  164. return LLM_CHAT_TEMPLATE_RWKV_WORLD;
  165. } else if (tmpl_contains("<|start_of_role|>")) {
  166. return LLM_CHAT_TEMPLATE_GRANITE;
  167. } else if (tmpl_contains("message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1]")) {
  168. return LLM_CHAT_TEMPLATE_GIGACHAT;
  169. } else if (tmpl_contains("<|role_start|>")) {
  170. return LLM_CHAT_TEMPLATE_MEGREZ;
  171. } else if (tmpl_contains(" Ассистент:")) {
  172. return LLM_CHAT_TEMPLATE_YANDEX;
  173. } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("'HUMAN'")) {
  174. return LLM_CHAT_TEMPLATE_BAILING;
  175. } else if (tmpl_contains("<|header_start|>") && tmpl_contains("<|header_end|>")) {
  176. return LLM_CHAT_TEMPLATE_LLAMA4;
  177. }
  178. return LLM_CHAT_TEMPLATE_UNKNOWN;
  179. }
  180. // Simple version of "llama_apply_chat_template" that only works with strings
  181. // This function uses heuristic checks to determine commonly used template. It is not a jinja parser.
  182. int32_t llm_chat_apply_template(
  183. llm_chat_template tmpl,
  184. const std::vector<const llama_chat_message *> & chat,
  185. std::string & dest, bool add_ass) {
  186. // Taken from the research: https://github.com/ggerganov/llama.cpp/issues/5527
  187. std::stringstream ss;
  188. if (tmpl == LLM_CHAT_TEMPLATE_CHATML) {
  189. // chatml template
  190. for (auto message : chat) {
  191. ss << "<|im_start|>" << message->role << "\n" << message->content << "<|im_end|>\n";
  192. }
  193. if (add_ass) {
  194. ss << "<|im_start|>assistant\n";
  195. }
  196. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7) {
  197. // Official mistral 'v7' template
  198. // See: https://huggingface.co/mistralai/Mistral-Large-Instruct-2411#basic-instruct-template-v7
  199. for (auto message : chat) {
  200. std::string role(message->role);
  201. std::string content(message->content);
  202. if (role == "system") {
  203. ss << "[SYSTEM_PROMPT] " << content << "[/SYSTEM_PROMPT]";
  204. } else if (role == "user") {
  205. ss << "[INST] " << content << "[/INST]";
  206. }
  207. else {
  208. ss << " " << content << "</s>";
  209. }
  210. }
  211. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1
  212. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3
  213. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN) {
  214. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  215. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  216. std::string leading_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1 ? " " : "";
  217. std::string trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN ? "" : " ";
  218. bool trim_assistant_message = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3;
  219. bool is_inside_turn = false;
  220. for (auto message : chat) {
  221. if (!is_inside_turn) {
  222. ss << leading_space << "[INST]" << trailing_space;
  223. is_inside_turn = true;
  224. }
  225. std::string role(message->role);
  226. std::string content(message->content);
  227. if (role == "system") {
  228. ss << content << "\n\n";
  229. } else if (role == "user") {
  230. ss << content << leading_space << "[/INST]";
  231. } else {
  232. ss << trailing_space << (trim_assistant_message ? trim(content) : content) << "</s>";
  233. is_inside_turn = false;
  234. }
  235. }
  236. } else if (
  237. tmpl == LLM_CHAT_TEMPLATE_LLAMA_2
  238. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS
  239. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS
  240. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP) {
  241. // llama2 template and its variants
  242. // [variant] support system message
  243. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  244. bool support_system_message = tmpl != LLM_CHAT_TEMPLATE_LLAMA_2;
  245. // [variant] add BOS inside history
  246. bool add_bos_inside_history = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  247. // [variant] trim spaces from the input message
  248. bool strip_message = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  249. // construct the prompt
  250. bool is_inside_turn = true; // skip BOS at the beginning
  251. ss << "[INST] ";
  252. for (auto message : chat) {
  253. std::string content = strip_message ? trim(message->content) : message->content;
  254. std::string role(message->role);
  255. if (!is_inside_turn) {
  256. is_inside_turn = true;
  257. ss << (add_bos_inside_history ? "<s>[INST] " : "[INST] ");
  258. }
  259. if (role == "system") {
  260. if (support_system_message) {
  261. ss << "<<SYS>>\n" << content << "\n<</SYS>>\n\n";
  262. } else {
  263. // if the model does not support system message, we still include it in the first message, but without <<SYS>>
  264. ss << content << "\n";
  265. }
  266. } else if (role == "user") {
  267. ss << content << " [/INST]";
  268. } else {
  269. ss << content << "</s>";
  270. is_inside_turn = false;
  271. }
  272. }
  273. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_3) {
  274. // Phi 3
  275. for (auto message : chat) {
  276. std::string role(message->role);
  277. ss << "<|" << role << "|>\n" << message->content << "<|end|>\n";
  278. }
  279. if (add_ass) {
  280. ss << "<|assistant|>\n";
  281. }
  282. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_4) {
  283. // chatml template
  284. for (auto message : chat) {
  285. ss << "<|im_start|>" << message->role << "<|im_sep|>" << message->content << "<|im_end|>";
  286. }
  287. if (add_ass) {
  288. ss << "<|im_start|>assistant<|im_sep|>";
  289. }
  290. } else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
  291. // Falcon 3
  292. for (auto message : chat) {
  293. std::string role(message->role);
  294. ss << "<|" << role << "|>\n" << message->content << "\n";
  295. }
  296. if (add_ass) {
  297. ss << "<|assistant|>\n";
  298. }
  299. } else if (tmpl == LLM_CHAT_TEMPLATE_ZEPHYR) {
  300. // zephyr template
  301. for (auto message : chat) {
  302. ss << "<|" << message->role << "|>" << "\n" << message->content << "<|endoftext|>\n";
  303. }
  304. if (add_ass) {
  305. ss << "<|assistant|>\n";
  306. }
  307. } else if (tmpl == LLM_CHAT_TEMPLATE_MONARCH) {
  308. // mlabonne/AlphaMonarch-7B template (the <s> is included inside history)
  309. for (auto message : chat) {
  310. std::string bos = (message == chat.front()) ? "" : "<s>"; // skip BOS for first message
  311. ss << bos << message->role << "\n" << message->content << "</s>\n";
  312. }
  313. if (add_ass) {
  314. ss << "<s>assistant\n";
  315. }
  316. } else if (tmpl == LLM_CHAT_TEMPLATE_GEMMA) {
  317. // google/gemma-7b-it
  318. std::string system_prompt = "";
  319. for (auto message : chat) {
  320. std::string role(message->role);
  321. if (role == "system") {
  322. // there is no system message for gemma, but we will merge it with user prompt, so nothing is broken
  323. system_prompt = trim(message->content);
  324. continue;
  325. }
  326. // in gemma, "assistant" is "model"
  327. role = role == "assistant" ? "model" : message->role;
  328. ss << "<start_of_turn>" << role << "\n";
  329. if (!system_prompt.empty() && role != "model") {
  330. ss << system_prompt << "\n\n";
  331. system_prompt = "";
  332. }
  333. ss << trim(message->content) << "<end_of_turn>\n";
  334. }
  335. if (add_ass) {
  336. ss << "<start_of_turn>model\n";
  337. }
  338. } else if (tmpl == LLM_CHAT_TEMPLATE_ORION) {
  339. // OrionStarAI/Orion-14B-Chat
  340. std::string system_prompt = "";
  341. for (auto message : chat) {
  342. std::string role(message->role);
  343. if (role == "system") {
  344. // there is no system message support, we will merge it with user prompt
  345. system_prompt = message->content;
  346. continue;
  347. } else if (role == "user") {
  348. ss << "Human: ";
  349. if (!system_prompt.empty()) {
  350. ss << system_prompt << "\n\n";
  351. system_prompt = "";
  352. }
  353. ss << message->content << "\n\nAssistant: </s>";
  354. } else {
  355. ss << message->content << "</s>";
  356. }
  357. }
  358. } else if (tmpl == LLM_CHAT_TEMPLATE_OPENCHAT) {
  359. // openchat/openchat-3.5-0106,
  360. for (auto message : chat) {
  361. std::string role(message->role);
  362. if (role == "system") {
  363. ss << message->content << "<|end_of_turn|>";
  364. } else {
  365. role[0] = toupper(role[0]);
  366. ss << "GPT4 Correct " << role << ": " << message->content << "<|end_of_turn|>";
  367. }
  368. }
  369. if (add_ass) {
  370. ss << "GPT4 Correct Assistant:";
  371. }
  372. } else if (tmpl == LLM_CHAT_TEMPLATE_VICUNA || tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  373. // eachadea/vicuna-13b-1.1 (and Orca variant)
  374. for (auto message : chat) {
  375. std::string role(message->role);
  376. if (role == "system") {
  377. // Orca-Vicuna variant uses a system prefix
  378. if (tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  379. ss << "SYSTEM: " << message->content << "\n";
  380. } else {
  381. ss << message->content << "\n\n";
  382. }
  383. } else if (role == "user") {
  384. ss << "USER: " << message->content << "\n";
  385. } else if (role == "assistant") {
  386. ss << "ASSISTANT: " << message->content << "</s>\n";
  387. }
  388. }
  389. if (add_ass) {
  390. ss << "ASSISTANT:";
  391. }
  392. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK) {
  393. // deepseek-ai/deepseek-coder-33b-instruct
  394. for (auto message : chat) {
  395. std::string role(message->role);
  396. if (role == "system") {
  397. ss << message->content;
  398. } else if (role == "user") {
  399. ss << "### Instruction:\n" << message->content << "\n";
  400. } else if (role == "assistant") {
  401. ss << "### Response:\n" << message->content << "\n<|EOT|>\n";
  402. }
  403. }
  404. if (add_ass) {
  405. ss << "### Response:\n";
  406. }
  407. } else if (tmpl == LLM_CHAT_TEMPLATE_COMMAND_R) {
  408. // CohereForAI/c4ai-command-r-plus
  409. for (auto message : chat) {
  410. std::string role(message->role);
  411. if (role == "system") {
  412. ss << "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  413. } else if (role == "user") {
  414. ss << "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  415. } else if (role == "assistant") {
  416. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  417. }
  418. }
  419. if (add_ass) {
  420. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>";
  421. }
  422. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA_3) {
  423. // Llama 3
  424. for (auto message : chat) {
  425. std::string role(message->role);
  426. ss << "<|start_header_id|>" << role << "<|end_header_id|>\n\n" << trim(message->content) << "<|eot_id|>";
  427. }
  428. if (add_ass) {
  429. ss << "<|start_header_id|>assistant<|end_header_id|>\n\n";
  430. }
  431. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_3) {
  432. // chatglm3-6b
  433. ss << "[gMASK]" << "sop";
  434. for (auto message : chat) {
  435. std::string role(message->role);
  436. ss << "<|" << role << "|>" << "\n " << message->content;
  437. }
  438. if (add_ass) {
  439. ss << "<|assistant|>";
  440. }
  441. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_4 || tmpl == LLM_CHAT_TEMPLATE_GLMEDGE) {
  442. ss << "[gMASK]" << "<sop>";
  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 if (tmpl == LLM_CHAT_TEMPLATE_LLAMA4) {
  593. // Llama 4
  594. for (auto message : chat) {
  595. std::string role(message->role);
  596. ss << "<|header_start|>" << role << "<|header_end|>\n\n" << trim(message->content) << "<|eot|>";
  597. }
  598. if (add_ass) {
  599. ss << "<|header_start|>assistant<|header_end|>\n\n";
  600. }
  601. } else if (tmpl == LLM_CHAT_TEMPLATE_SMOLVLM) {
  602. // SmolVLM
  603. ss << "<|im_start|>"; // uses <|im_start|> as BOS, but the actual content is NOT chatml
  604. for (auto message : chat) {
  605. std::string role(message->role);
  606. if (role == "system") {
  607. ss << message->content << "\n\n";
  608. } else if (role == "user") {
  609. ss << "User: " << message->content << "<end_of_utterance>\n";
  610. } else {
  611. ss << "Assistant: " << message->content << "<end_of_utterance>\n";
  612. }
  613. }
  614. if (add_ass) {
  615. ss << "Assistant:";
  616. }
  617. } else {
  618. // template not supported
  619. return -1;
  620. }
  621. dest = ss.str();
  622. return dest.size();
  623. }
  624. // public interface
  625. int32_t llama_chat_builtin_templates(const char ** output, size_t len) {
  626. auto it = LLM_CHAT_TEMPLATES.begin();
  627. for (size_t i = 0; i < std::min(len, LLM_CHAT_TEMPLATES.size()); i++) {
  628. output[i] = it->first.c_str();
  629. std::advance(it, 1);
  630. }
  631. return (int32_t) LLM_CHAT_TEMPLATES.size();
  632. }