llama-chat.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. } else if (tmpl_contains("<|endofuserprompt|>")) {
  179. return LLM_CHAT_TEMPLATE_DOTS1;
  180. }
  181. return LLM_CHAT_TEMPLATE_UNKNOWN;
  182. }
  183. // Simple version of "llama_apply_chat_template" that only works with strings
  184. // This function uses heuristic checks to determine commonly used template. It is not a jinja parser.
  185. int32_t llm_chat_apply_template(
  186. llm_chat_template tmpl,
  187. const std::vector<const llama_chat_message *> & chat,
  188. std::string & dest, bool add_ass) {
  189. // Taken from the research: https://github.com/ggerganov/llama.cpp/issues/5527
  190. std::stringstream ss;
  191. if (tmpl == LLM_CHAT_TEMPLATE_CHATML) {
  192. // chatml template
  193. for (auto message : chat) {
  194. ss << "<|im_start|>" << message->role << "\n" << message->content << "<|im_end|>\n";
  195. }
  196. if (add_ass) {
  197. ss << "<|im_start|>assistant\n";
  198. }
  199. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7_TEKKEN) {
  200. // Official mistral 'v7' template
  201. // See: https://huggingface.co/mistralai/Mistral-Large-Instruct-2411#basic-instruct-template-v7
  202. // https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503#basic-instruct-template-v7-tekken
  203. const char * trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 ? " " : "";
  204. for (auto message : chat) {
  205. std::string role(message->role);
  206. std::string content(message->content);
  207. if (role == "system") {
  208. ss << "[SYSTEM_PROMPT]" << trailing_space << content << "[/SYSTEM_PROMPT]";
  209. } else if (role == "user") {
  210. ss << "[INST]" << trailing_space << content << "[/INST]";
  211. } else {
  212. ss << trailing_space << content << "</s>";
  213. }
  214. }
  215. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1
  216. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3
  217. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN) {
  218. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  219. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  220. std::string leading_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1 ? " " : "";
  221. std::string trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN ? "" : " ";
  222. bool trim_assistant_message = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3;
  223. bool is_inside_turn = false;
  224. for (auto message : chat) {
  225. if (!is_inside_turn) {
  226. ss << leading_space << "[INST]" << trailing_space;
  227. is_inside_turn = true;
  228. }
  229. std::string role(message->role);
  230. std::string content(message->content);
  231. if (role == "system") {
  232. ss << content << "\n\n";
  233. } else if (role == "user") {
  234. ss << content << leading_space << "[/INST]";
  235. } else {
  236. ss << trailing_space << (trim_assistant_message ? trim(content) : content) << "</s>";
  237. is_inside_turn = false;
  238. }
  239. }
  240. } else if (
  241. tmpl == LLM_CHAT_TEMPLATE_LLAMA_2
  242. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS
  243. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS
  244. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP) {
  245. // llama2 template and its variants
  246. // [variant] support system message
  247. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  248. bool support_system_message = tmpl != LLM_CHAT_TEMPLATE_LLAMA_2;
  249. // [variant] add BOS inside history
  250. bool add_bos_inside_history = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  251. // [variant] trim spaces from the input message
  252. bool strip_message = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  253. // construct the prompt
  254. bool is_inside_turn = true; // skip BOS at the beginning
  255. ss << "[INST] ";
  256. for (auto message : chat) {
  257. std::string content = strip_message ? trim(message->content) : message->content;
  258. std::string role(message->role);
  259. if (!is_inside_turn) {
  260. is_inside_turn = true;
  261. ss << (add_bos_inside_history ? "<s>[INST] " : "[INST] ");
  262. }
  263. if (role == "system") {
  264. if (support_system_message) {
  265. ss << "<<SYS>>\n" << content << "\n<</SYS>>\n\n";
  266. } else {
  267. // if the model does not support system message, we still include it in the first message, but without <<SYS>>
  268. ss << content << "\n";
  269. }
  270. } else if (role == "user") {
  271. ss << content << " [/INST]";
  272. } else {
  273. ss << content << "</s>";
  274. is_inside_turn = false;
  275. }
  276. }
  277. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_3) {
  278. // Phi 3
  279. for (auto message : chat) {
  280. std::string role(message->role);
  281. ss << "<|" << role << "|>\n" << message->content << "<|end|>\n";
  282. }
  283. if (add_ass) {
  284. ss << "<|assistant|>\n";
  285. }
  286. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_4) {
  287. // chatml template
  288. for (auto message : chat) {
  289. ss << "<|im_start|>" << message->role << "<|im_sep|>" << message->content << "<|im_end|>";
  290. }
  291. if (add_ass) {
  292. ss << "<|im_start|>assistant<|im_sep|>";
  293. }
  294. } else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
  295. // Falcon 3
  296. for (auto message : chat) {
  297. std::string role(message->role);
  298. ss << "<|" << role << "|>\n" << message->content << "\n";
  299. }
  300. if (add_ass) {
  301. ss << "<|assistant|>\n";
  302. }
  303. } else if (tmpl == LLM_CHAT_TEMPLATE_ZEPHYR) {
  304. // zephyr template
  305. for (auto message : chat) {
  306. ss << "<|" << message->role << "|>" << "\n" << message->content << "<|endoftext|>\n";
  307. }
  308. if (add_ass) {
  309. ss << "<|assistant|>\n";
  310. }
  311. } else if (tmpl == LLM_CHAT_TEMPLATE_MONARCH) {
  312. // mlabonne/AlphaMonarch-7B template (the <s> is included inside history)
  313. for (auto message : chat) {
  314. std::string bos = (message == chat.front()) ? "" : "<s>"; // skip BOS for first message
  315. ss << bos << message->role << "\n" << message->content << "</s>\n";
  316. }
  317. if (add_ass) {
  318. ss << "<s>assistant\n";
  319. }
  320. } else if (tmpl == LLM_CHAT_TEMPLATE_GEMMA) {
  321. // google/gemma-7b-it
  322. std::string system_prompt = "";
  323. for (auto message : chat) {
  324. std::string role(message->role);
  325. if (role == "system") {
  326. // there is no system message for gemma, but we will merge it with user prompt, so nothing is broken
  327. system_prompt += trim(message->content);
  328. continue;
  329. }
  330. // in gemma, "assistant" is "model"
  331. role = role == "assistant" ? "model" : message->role;
  332. ss << "<start_of_turn>" << role << "\n";
  333. if (!system_prompt.empty() && role != "model") {
  334. ss << system_prompt << "\n\n";
  335. system_prompt = "";
  336. }
  337. ss << trim(message->content) << "<end_of_turn>\n";
  338. }
  339. if (add_ass) {
  340. ss << "<start_of_turn>model\n";
  341. }
  342. } else if (tmpl == LLM_CHAT_TEMPLATE_ORION) {
  343. // OrionStarAI/Orion-14B-Chat
  344. std::string system_prompt = "";
  345. for (auto message : chat) {
  346. std::string role(message->role);
  347. if (role == "system") {
  348. // there is no system message support, we will merge it with user prompt
  349. system_prompt += message->content;
  350. continue;
  351. } else if (role == "user") {
  352. ss << "Human: ";
  353. if (!system_prompt.empty()) {
  354. ss << system_prompt << "\n\n";
  355. system_prompt = "";
  356. }
  357. ss << message->content << "\n\nAssistant: </s>";
  358. } else {
  359. ss << message->content << "</s>";
  360. }
  361. }
  362. } else if (tmpl == LLM_CHAT_TEMPLATE_OPENCHAT) {
  363. // openchat/openchat-3.5-0106,
  364. for (auto message : chat) {
  365. std::string role(message->role);
  366. if (role == "system") {
  367. ss << message->content << "<|end_of_turn|>";
  368. } else {
  369. role[0] = toupper(role[0]);
  370. ss << "GPT4 Correct " << role << ": " << message->content << "<|end_of_turn|>";
  371. }
  372. }
  373. if (add_ass) {
  374. ss << "GPT4 Correct Assistant:";
  375. }
  376. } else if (tmpl == LLM_CHAT_TEMPLATE_VICUNA || tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  377. // eachadea/vicuna-13b-1.1 (and Orca variant)
  378. for (auto message : chat) {
  379. std::string role(message->role);
  380. if (role == "system") {
  381. // Orca-Vicuna variant uses a system prefix
  382. if (tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  383. ss << "SYSTEM: " << message->content << "\n";
  384. } else {
  385. ss << message->content << "\n\n";
  386. }
  387. } else if (role == "user") {
  388. ss << "USER: " << message->content << "\n";
  389. } else if (role == "assistant") {
  390. ss << "ASSISTANT: " << message->content << "</s>\n";
  391. }
  392. }
  393. if (add_ass) {
  394. ss << "ASSISTANT:";
  395. }
  396. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK) {
  397. // deepseek-ai/deepseek-coder-33b-instruct
  398. for (auto message : chat) {
  399. std::string role(message->role);
  400. if (role == "system") {
  401. ss << message->content;
  402. } else if (role == "user") {
  403. ss << "### Instruction:\n" << message->content << "\n";
  404. } else if (role == "assistant") {
  405. ss << "### Response:\n" << message->content << "\n<|EOT|>\n";
  406. }
  407. }
  408. if (add_ass) {
  409. ss << "### Response:\n";
  410. }
  411. } else if (tmpl == LLM_CHAT_TEMPLATE_COMMAND_R) {
  412. // CohereForAI/c4ai-command-r-plus
  413. for (auto message : chat) {
  414. std::string role(message->role);
  415. if (role == "system") {
  416. ss << "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  417. } else if (role == "user") {
  418. ss << "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  419. } else if (role == "assistant") {
  420. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  421. }
  422. }
  423. if (add_ass) {
  424. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>";
  425. }
  426. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA_3) {
  427. // Llama 3
  428. for (auto message : chat) {
  429. std::string role(message->role);
  430. ss << "<|start_header_id|>" << role << "<|end_header_id|>\n\n" << trim(message->content) << "<|eot_id|>";
  431. }
  432. if (add_ass) {
  433. ss << "<|start_header_id|>assistant<|end_header_id|>\n\n";
  434. }
  435. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_3) {
  436. // chatglm3-6b
  437. ss << "[gMASK]" << "sop";
  438. for (auto message : chat) {
  439. std::string role(message->role);
  440. ss << "<|" << role << "|>" << "\n " << message->content;
  441. }
  442. if (add_ass) {
  443. ss << "<|assistant|>";
  444. }
  445. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_4) {
  446. ss << "[gMASK]" << "<sop>";
  447. for (auto message : chat) {
  448. std::string role(message->role);
  449. ss << "<|" << role << "|>" << "\n" << message->content;
  450. }
  451. if (add_ass) {
  452. ss << "<|assistant|>\n";
  453. }
  454. } else if (tmpl == LLM_CHAT_TEMPLATE_GLMEDGE) {
  455. for (auto message : chat) {
  456. std::string role(message->role);
  457. ss << "<|" << role << "|>" << "\n" << message->content;
  458. }
  459. if (add_ass) {
  460. ss << "<|assistant|>";
  461. }
  462. } else if (tmpl == LLM_CHAT_TEMPLATE_MINICPM) {
  463. // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  464. for (auto message : chat) {
  465. std::string role(message->role);
  466. if (role == "user") {
  467. ss << LU8("<用户>");
  468. ss << trim(message->content);
  469. ss << "<AI>";
  470. } else {
  471. ss << trim(message->content);
  472. }
  473. }
  474. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_2) {
  475. // DeepSeek-V2
  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 << "User: " << message->content << "\n\n";
  482. } else if (role == "assistant") {
  483. ss << "Assistant: " << message->content << LU8("<|end▁of▁sentence|>");
  484. }
  485. }
  486. if (add_ass) {
  487. ss << "Assistant:";
  488. }
  489. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_3) {
  490. // DeepSeek-V3
  491. for (auto message : chat) {
  492. std::string role(message->role);
  493. if (role == "system") {
  494. ss << message->content << "\n\n";
  495. } else if (role == "user") {
  496. ss << LU8("<|User|>") << message->content;
  497. } else if (role == "assistant") {
  498. ss << LU8("<|Assistant|>") << message->content << LU8("<|end▁of▁sentence|>");
  499. }
  500. }
  501. if (add_ass) {
  502. ss << LU8("<|Assistant|>");
  503. }
  504. } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_3) {
  505. // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  506. // EXAONE-3.0-7.8B-Instruct
  507. for (auto message : chat) {
  508. std::string role(message->role);
  509. if (role == "system") {
  510. ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  511. } else if (role == "user") {
  512. ss << "[|user|]" << trim(message->content) << "\n";
  513. } else if (role == "assistant") {
  514. ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  515. }
  516. }
  517. if (add_ass) {
  518. ss << "[|assistant|]";
  519. }
  520. } else if (tmpl == LLM_CHAT_TEMPLATE_RWKV_WORLD) {
  521. // this template requires the model to have "\n\n" as EOT token
  522. for (size_t i = 0; i < chat.size(); i++) {
  523. std::string role(chat[i]->role);
  524. if (role == "system") {
  525. ss << "System: " << trim(chat[i]->content) << "\n\n";
  526. } else if (role == "user") {
  527. ss << "User: " << trim(chat[i]->content) << "\n\n";
  528. if (i == chat.size() - 1) {
  529. ss << "Assistant:";
  530. }
  531. } else if (role == "assistant") {
  532. ss << "Assistant: " << trim(chat[i]->content) << "\n\n";
  533. }
  534. }
  535. } else if (tmpl == LLM_CHAT_TEMPLATE_GRANITE) {
  536. // IBM Granite template
  537. for (const auto & message : chat) {
  538. std::string role(message->role);
  539. ss << "<|start_of_role|>" << role << "<|end_of_role|>";
  540. if (role == "assistant_tool_call") {
  541. ss << "<|tool_call|>";
  542. }
  543. ss << message->content << "<|end_of_text|>\n";
  544. }
  545. if (add_ass) {
  546. ss << "<|start_of_role|>assistant<|end_of_role|>\n";
  547. }
  548. } else if (tmpl == LLM_CHAT_TEMPLATE_GIGACHAT) {
  549. // GigaChat template
  550. bool has_system = !chat.empty() && std::string(chat[0]->role) == "system";
  551. // Handle system message if present
  552. if (has_system) {
  553. ss << "<s>" << chat[0]->content << "<|message_sep|>";
  554. } else {
  555. ss << "<s>";
  556. }
  557. // Process remaining messages
  558. for (size_t i = has_system ? 1 : 0; i < chat.size(); i++) {
  559. std::string role(chat[i]->role);
  560. if (role == "user") {
  561. ss << "user<|role_sep|>" << chat[i]->content << "<|message_sep|>"
  562. << "available functions<|role_sep|>[]<|message_sep|>";
  563. } else if (role == "assistant") {
  564. ss << "assistant<|role_sep|>" << chat[i]->content << "<|message_sep|>";
  565. }
  566. }
  567. // Add generation prompt if needed
  568. if (add_ass) {
  569. ss << "assistant<|role_sep|>";
  570. }
  571. } else if (tmpl == LLM_CHAT_TEMPLATE_MEGREZ) {
  572. // Megrez template
  573. for (auto message : chat) {
  574. std::string role(message->role);
  575. ss << "<|role_start|>" << role << "<|role_end|>" << message->content << "<|turn_end|>";
  576. }
  577. if (add_ass) {
  578. ss << "<|role_start|>assistant<|role_end|>";
  579. }
  580. } else if (tmpl == LLM_CHAT_TEMPLATE_YANDEX) {
  581. // Yandex template ("\n\n" is defined as EOT token)
  582. ss << "<s>";
  583. for (size_t i = 0; i < chat.size(); i++) {
  584. std::string role(chat[i]->role);
  585. if (role == "user") {
  586. ss << " Пользователь: " << chat[i]->content << "\n\n";
  587. } else if (role == "assistant") {
  588. ss << " Ассистент: " << chat[i]->content << "\n\n";
  589. }
  590. }
  591. // Add generation prompt if needed
  592. if (add_ass) {
  593. ss << " Ассистент:[SEP]";
  594. }
  595. } else if (tmpl == LLM_CHAT_TEMPLATE_BAILING) {
  596. // Bailing (Ling) template
  597. for (auto message : chat) {
  598. std::string role(message->role);
  599. if (role == "user") {
  600. role = "HUMAN";
  601. } else {
  602. std::transform(role.begin(), role.end(), role.begin(), ::toupper);
  603. }
  604. ss << "<role>" << role << "</role>" << message->content;
  605. }
  606. if (add_ass) {
  607. ss << "<role>ASSISTANT</role>";
  608. }
  609. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA4) {
  610. // Llama 4
  611. for (auto message : chat) {
  612. std::string role(message->role);
  613. ss << "<|header_start|>" << role << "<|header_end|>\n\n" << trim(message->content) << "<|eot|>";
  614. }
  615. if (add_ass) {
  616. ss << "<|header_start|>assistant<|header_end|>\n\n";
  617. }
  618. } else if (tmpl == LLM_CHAT_TEMPLATE_SMOLVLM) {
  619. // SmolVLM
  620. ss << "<|im_start|>"; // uses <|im_start|> as BOS, but the actual content is NOT chatml
  621. for (auto message : chat) {
  622. std::string role(message->role);
  623. if (role == "system") {
  624. ss << message->content << "\n\n";
  625. } else if (role == "user") {
  626. ss << "User: " << message->content << "<end_of_utterance>\n";
  627. } else {
  628. ss << "Assistant: " << message->content << "<end_of_utterance>\n";
  629. }
  630. }
  631. if (add_ass) {
  632. ss << "Assistant:";
  633. }
  634. } else if (tmpl == LLM_CHAT_TEMPLATE_DOTS1) {
  635. // dots.llm1.inst (DOTS1)
  636. for (auto message : chat) {
  637. std::string role(message->role);
  638. if (role == "system") {
  639. ss << "<|system|>" << message->content << "<|endofsystem|>";
  640. } else if (role == "user") {
  641. ss << "<|userprompt|>" << message->content << "<|endofuserprompt|>";
  642. } else {
  643. ss << "<|response|>" << message->content << "<|endofresponse|>";
  644. }
  645. }
  646. if (add_ass) {
  647. ss << "<|response|>";
  648. }
  649. } else {
  650. // template not supported
  651. return -1;
  652. }
  653. dest = ss.str();
  654. return dest.size();
  655. }
  656. // public interface
  657. int32_t llama_chat_builtin_templates(const char ** output, size_t len) {
  658. auto it = LLM_CHAT_TEMPLATES.begin();
  659. for (size_t i = 0; i < std::min(len, LLM_CHAT_TEMPLATES.size()); i++) {
  660. output[i] = it->first.c_str();
  661. std::advance(it, 1);
  662. }
  663. return (int32_t) LLM_CHAT_TEMPLATES.size();
  664. }