llama-chat.cpp 28 KB

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