llama-chat.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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(static_cast<unsigned char>(str[start]))) {
  16. start += 1;
  17. }
  18. while (end > start && isspace(static_cast<unsigned char>(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. { "exaone4", LLM_CHAT_TEMPLATE_EXAONE_4 },
  55. { "rwkv-world", LLM_CHAT_TEMPLATE_RWKV_WORLD },
  56. { "granite", LLM_CHAT_TEMPLATE_GRANITE },
  57. { "gigachat", LLM_CHAT_TEMPLATE_GIGACHAT },
  58. { "megrez", LLM_CHAT_TEMPLATE_MEGREZ },
  59. { "yandex", LLM_CHAT_TEMPLATE_YANDEX },
  60. { "bailing", LLM_CHAT_TEMPLATE_BAILING },
  61. { "bailing-think", LLM_CHAT_TEMPLATE_BAILING_THINK },
  62. { "bailing2", LLM_CHAT_TEMPLATE_BAILING2 },
  63. { "llama4", LLM_CHAT_TEMPLATE_LLAMA4 },
  64. { "smolvlm", LLM_CHAT_TEMPLATE_SMOLVLM },
  65. { "hunyuan-moe", LLM_CHAT_TEMPLATE_HUNYUAN_MOE },
  66. { "gpt-oss", LLM_CHAT_TEMPLATE_OPENAI_MOE },
  67. { "hunyuan-dense", LLM_CHAT_TEMPLATE_HUNYUAN_DENSE },
  68. { "kimi-k2", LLM_CHAT_TEMPLATE_KIMI_K2 },
  69. { "seed_oss", LLM_CHAT_TEMPLATE_SEED_OSS },
  70. { "grok-2", LLM_CHAT_TEMPLATE_GROK_2 },
  71. { "pangu-embedded", LLM_CHAT_TEMPLATE_PANGU_EMBED },
  72. };
  73. llm_chat_template llm_chat_template_from_str(const std::string & name) {
  74. return LLM_CHAT_TEMPLATES.at(name);
  75. }
  76. llm_chat_template llm_chat_detect_template(const std::string & tmpl) {
  77. try {
  78. return llm_chat_template_from_str(tmpl);
  79. } catch (const std::out_of_range &) {
  80. // ignore
  81. }
  82. auto tmpl_contains = [&tmpl](const char * haystack) -> bool {
  83. return tmpl.find(haystack) != std::string::npos;
  84. };
  85. if (tmpl_contains("<|im_start|>")) {
  86. return tmpl_contains("<|im_sep|>")
  87. ? LLM_CHAT_TEMPLATE_PHI_4
  88. : tmpl_contains("<end_of_utterance>")
  89. ? LLM_CHAT_TEMPLATE_SMOLVLM // SmolVLM uses <|im_start|> as BOS, but it is NOT chatml
  90. : LLM_CHAT_TEMPLATE_CHATML;
  91. } else if (tmpl.find("mistral") == 0 || tmpl_contains("[INST]")) {
  92. if (tmpl_contains("[SYSTEM_PROMPT]")) {
  93. return LLM_CHAT_TEMPLATE_MISTRAL_V7;
  94. } else if (
  95. // catches official 'v1' template
  96. tmpl_contains("' [INST] ' + system_message")
  97. // catches official 'v3' and 'v3-tekken' templates
  98. || tmpl_contains("[AVAILABLE_TOOLS]")
  99. ) {
  100. // Official mistral 'v1', 'v3' and 'v3-tekken' templates
  101. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  102. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  103. if (tmpl_contains(" [INST]")) {
  104. return LLM_CHAT_TEMPLATE_MISTRAL_V1;
  105. } else if (tmpl_contains("\"[INST]\"")) {
  106. return LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN;
  107. }
  108. return LLM_CHAT_TEMPLATE_MISTRAL_V3;
  109. } else {
  110. // llama2 template and its variants
  111. // [variant] support system message
  112. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  113. bool support_system_message = tmpl_contains("<<SYS>>");
  114. bool add_bos_inside_history = tmpl_contains("bos_token + '[INST]");
  115. bool strip_message = tmpl_contains("content.strip()");
  116. if (strip_message) {
  117. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  118. } else if (add_bos_inside_history) {
  119. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  120. } else if (support_system_message) {
  121. return LLM_CHAT_TEMPLATE_LLAMA_2_SYS;
  122. } else {
  123. return LLM_CHAT_TEMPLATE_LLAMA_2;
  124. }
  125. }
  126. } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|end|>")) {
  127. return LLM_CHAT_TEMPLATE_PHI_3;
  128. } else if (tmpl_contains("[gMASK]<sop>")) {
  129. return LLM_CHAT_TEMPLATE_CHATGLM_4;
  130. } else if (tmpl_contains("<|assistant|>") && tmpl_contains("<|user|>")) {
  131. return tmpl_contains("</s>") ? LLM_CHAT_TEMPLATE_FALCON_3 : LLM_CHAT_TEMPLATE_GLMEDGE;
  132. } else if (tmpl_contains("<|{{ item['role'] }}|>") && tmpl_contains("<|begin_of_image|>")) {
  133. return LLM_CHAT_TEMPLATE_GLMEDGE;
  134. } else if (tmpl_contains("<|user|>") && tmpl_contains("<|endoftext|>")) {
  135. return LLM_CHAT_TEMPLATE_ZEPHYR;
  136. } else if (tmpl_contains("bos_token + message['role']")) {
  137. return LLM_CHAT_TEMPLATE_MONARCH;
  138. } else if (tmpl_contains("<start_of_turn>")) {
  139. return LLM_CHAT_TEMPLATE_GEMMA;
  140. } else if (tmpl_contains("'\\n\\nAssistant: ' + eos_token")) {
  141. // OrionStarAI/Orion-14B-Chat
  142. return LLM_CHAT_TEMPLATE_ORION;
  143. } else if (tmpl_contains("GPT4 Correct ")) {
  144. // openchat/openchat-3.5-0106
  145. return LLM_CHAT_TEMPLATE_OPENCHAT;
  146. } else if (tmpl_contains("USER: ") && tmpl_contains("ASSISTANT: ")) {
  147. // eachadea/vicuna-13b-1.1 (and Orca variant)
  148. if (tmpl_contains("SYSTEM: ")) {
  149. return LLM_CHAT_TEMPLATE_VICUNA_ORCA;
  150. }
  151. return LLM_CHAT_TEMPLATE_VICUNA;
  152. } else if (tmpl_contains("### Instruction:") && tmpl_contains("<|EOT|>")) {
  153. // deepseek-ai/deepseek-coder-33b-instruct
  154. return LLM_CHAT_TEMPLATE_DEEPSEEK;
  155. } else if (tmpl_contains("<|START_OF_TURN_TOKEN|>") && tmpl_contains("<|USER_TOKEN|>")) {
  156. // CohereForAI/c4ai-command-r-plus
  157. return LLM_CHAT_TEMPLATE_COMMAND_R;
  158. } else if (tmpl_contains("<|start_header_id|>") && tmpl_contains("<|end_header_id|>")) {
  159. return LLM_CHAT_TEMPLATE_LLAMA_3;
  160. } else if (tmpl_contains("[gMASK]sop")) {
  161. // chatglm3-6b
  162. return LLM_CHAT_TEMPLATE_CHATGLM_3;
  163. } else if (tmpl_contains(LU8("<用户>"))) {
  164. // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  165. return LLM_CHAT_TEMPLATE_MINICPM;
  166. } else if (tmpl_contains("'Assistant: ' + message['content'] + eos_token")) {
  167. return LLM_CHAT_TEMPLATE_DEEPSEEK_2;
  168. } else if (tmpl_contains(LU8("<|Assistant|>")) && tmpl_contains(LU8("<|User|>")) && tmpl_contains(LU8("<|end▁of▁sentence|>"))) {
  169. return LLM_CHAT_TEMPLATE_DEEPSEEK_3;
  170. } else if (tmpl_contains("[|system|]") && tmpl_contains("[|assistant|]") && tmpl_contains("[|endofturn|]")) {
  171. if (tmpl_contains("[|tool|]")) {
  172. return LLM_CHAT_TEMPLATE_EXAONE_4;
  173. }
  174. // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  175. // EXAONE-3.0-7.8B-Instruct
  176. return LLM_CHAT_TEMPLATE_EXAONE_3;
  177. } else if (tmpl_contains("rwkv-world") || tmpl_contains("{{- 'User: ' + message['content']|trim + '\\n\\n' -}}")) {
  178. return LLM_CHAT_TEMPLATE_RWKV_WORLD;
  179. } else if (tmpl_contains("<|start_of_role|>")) {
  180. return LLM_CHAT_TEMPLATE_GRANITE;
  181. } else if (tmpl_contains("message['role'] + additional_special_tokens[0] + message['content'] + additional_special_tokens[1]")) {
  182. return LLM_CHAT_TEMPLATE_GIGACHAT;
  183. } else if (tmpl_contains("<|role_start|>")) {
  184. return LLM_CHAT_TEMPLATE_MEGREZ;
  185. } else if (tmpl_contains(" Ассистент:")) {
  186. return LLM_CHAT_TEMPLATE_YANDEX;
  187. } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("'HUMAN'")) {
  188. return LLM_CHAT_TEMPLATE_BAILING;
  189. } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("\"HUMAN\"") && tmpl_contains("<think>")) {
  190. return LLM_CHAT_TEMPLATE_BAILING_THINK;
  191. } else if (tmpl_contains("<role>ASSISTANT</role>") && tmpl_contains("<role>HUMAN</role>") && tmpl_contains("<|role_end|>")) {
  192. return LLM_CHAT_TEMPLATE_BAILING2;
  193. } else if (tmpl_contains("<|header_start|>") && tmpl_contains("<|header_end|>")) {
  194. return LLM_CHAT_TEMPLATE_LLAMA4;
  195. } else if (tmpl_contains("<|endofuserprompt|>")) {
  196. return LLM_CHAT_TEMPLATE_DOTS1;
  197. } else if (tmpl_contains("<|extra_0|>") && tmpl_contains("<|extra_4|>")) {
  198. return LLM_CHAT_TEMPLATE_HUNYUAN_MOE;
  199. } else if (tmpl_contains("<|start|>") && tmpl_contains("<|channel|>")) {
  200. return LLM_CHAT_TEMPLATE_OPENAI_MOE;
  201. } else if (tmpl_contains("<|hy_Assistant|>") && tmpl_contains("<|hy_place▁holder▁no▁3|>")) {
  202. return LLM_CHAT_TEMPLATE_HUNYUAN_DENSE;
  203. } else if (tmpl_contains("<|im_assistant|>assistant<|im_middle|>")) {
  204. return LLM_CHAT_TEMPLATE_KIMI_K2;
  205. } else if (tmpl_contains("<seed:bos>")) {
  206. return LLM_CHAT_TEMPLATE_SEED_OSS;
  207. } else if (tmpl_contains("'Assistant: ' + message['content'] + '<|separator|>")) {
  208. return LLM_CHAT_TEMPLATE_GROK_2;
  209. } else if (tmpl_contains(LU8("[unused9]系统:[unused10]"))) {
  210. return LLM_CHAT_TEMPLATE_PANGU_EMBED;
  211. }
  212. return LLM_CHAT_TEMPLATE_UNKNOWN;
  213. }
  214. // Simple version of "llama_apply_chat_template" that only works with strings
  215. // This function uses heuristic checks to determine commonly used template. It is not a jinja parser.
  216. int32_t llm_chat_apply_template(
  217. llm_chat_template tmpl,
  218. const std::vector<const llama_chat_message *> & chat,
  219. std::string & dest, bool add_ass) {
  220. // Taken from the research: https://github.com/ggerganov/llama.cpp/issues/5527
  221. std::stringstream ss;
  222. if (tmpl == LLM_CHAT_TEMPLATE_CHATML) {
  223. // chatml template
  224. for (auto message : chat) {
  225. ss << "<|im_start|>" << message->role << "\n" << message->content << "<|im_end|>\n";
  226. }
  227. if (add_ass) {
  228. ss << "<|im_start|>assistant\n";
  229. }
  230. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7_TEKKEN) {
  231. // Official mistral 'v7' template
  232. // See: https://huggingface.co/mistralai/Mistral-Large-Instruct-2411#basic-instruct-template-v7
  233. // https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503#basic-instruct-template-v7-tekken
  234. const char * trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V7 ? " " : "";
  235. for (auto message : chat) {
  236. std::string role(message->role);
  237. std::string content(message->content);
  238. if (role == "system") {
  239. ss << "[SYSTEM_PROMPT]" << trailing_space << content << "[/SYSTEM_PROMPT]";
  240. } else if (role == "user") {
  241. ss << "[INST]" << trailing_space << content << "[/INST]";
  242. } else {
  243. ss << trailing_space << content << "</s>";
  244. }
  245. }
  246. } else if (tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1
  247. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3
  248. || tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN) {
  249. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/chat_templates.md
  250. // See: https://github.com/mistralai/cookbook/blob/main/concept-deep-dive/tokenization/templates.md
  251. std::string leading_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V1 ? " " : "";
  252. std::string trailing_space = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3_TEKKEN ? "" : " ";
  253. bool trim_assistant_message = tmpl == LLM_CHAT_TEMPLATE_MISTRAL_V3;
  254. bool is_inside_turn = false;
  255. for (auto message : chat) {
  256. if (!is_inside_turn) {
  257. ss << leading_space << "[INST]" << trailing_space;
  258. is_inside_turn = true;
  259. }
  260. std::string role(message->role);
  261. std::string content(message->content);
  262. if (role == "system") {
  263. ss << content << "\n\n";
  264. } else if (role == "user") {
  265. ss << content << leading_space << "[/INST]";
  266. } else {
  267. ss << trailing_space << (trim_assistant_message ? trim(content) : content) << "</s>";
  268. is_inside_turn = false;
  269. }
  270. }
  271. } else if (
  272. tmpl == LLM_CHAT_TEMPLATE_LLAMA_2
  273. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS
  274. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS
  275. || tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP) {
  276. // llama2 template and its variants
  277. // [variant] support system message
  278. // See: https://huggingface.co/blog/llama2#how-to-prompt-llama-2
  279. bool support_system_message = tmpl != LLM_CHAT_TEMPLATE_LLAMA_2;
  280. // [variant] add BOS inside history
  281. bool add_bos_inside_history = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_BOS;
  282. // [variant] trim spaces from the input message
  283. bool strip_message = tmpl == LLM_CHAT_TEMPLATE_LLAMA_2_SYS_STRIP;
  284. // construct the prompt
  285. bool is_inside_turn = true; // skip BOS at the beginning
  286. ss << "[INST] ";
  287. for (auto message : chat) {
  288. std::string content = strip_message ? trim(message->content) : message->content;
  289. std::string role(message->role);
  290. if (!is_inside_turn) {
  291. is_inside_turn = true;
  292. ss << (add_bos_inside_history ? "<s>[INST] " : "[INST] ");
  293. }
  294. if (role == "system") {
  295. if (support_system_message) {
  296. ss << "<<SYS>>\n" << content << "\n<</SYS>>\n\n";
  297. } else {
  298. // if the model does not support system message, we still include it in the first message, but without <<SYS>>
  299. ss << content << "\n";
  300. }
  301. } else if (role == "user") {
  302. ss << content << " [/INST]";
  303. } else {
  304. ss << content << "</s>";
  305. is_inside_turn = false;
  306. }
  307. }
  308. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_3) {
  309. // Phi 3
  310. for (auto message : chat) {
  311. std::string role(message->role);
  312. ss << "<|" << role << "|>\n" << message->content << "<|end|>\n";
  313. }
  314. if (add_ass) {
  315. ss << "<|assistant|>\n";
  316. }
  317. } else if (tmpl == LLM_CHAT_TEMPLATE_PHI_4) {
  318. // chatml template
  319. for (auto message : chat) {
  320. ss << "<|im_start|>" << message->role << "<|im_sep|>" << message->content << "<|im_end|>";
  321. }
  322. if (add_ass) {
  323. ss << "<|im_start|>assistant<|im_sep|>";
  324. }
  325. } else if (tmpl == LLM_CHAT_TEMPLATE_FALCON_3) {
  326. // Falcon 3
  327. for (auto message : chat) {
  328. std::string role(message->role);
  329. ss << "<|" << role << "|>\n" << message->content << "\n";
  330. }
  331. if (add_ass) {
  332. ss << "<|assistant|>\n";
  333. }
  334. } else if (tmpl == LLM_CHAT_TEMPLATE_ZEPHYR) {
  335. // zephyr template
  336. for (auto message : chat) {
  337. ss << "<|" << message->role << "|>" << "\n" << message->content << "<|endoftext|>\n";
  338. }
  339. if (add_ass) {
  340. ss << "<|assistant|>\n";
  341. }
  342. } else if (tmpl == LLM_CHAT_TEMPLATE_MONARCH) {
  343. // mlabonne/AlphaMonarch-7B template (the <s> is included inside history)
  344. for (auto message : chat) {
  345. std::string bos = (message == chat.front()) ? "" : "<s>"; // skip BOS for first message
  346. ss << bos << message->role << "\n" << message->content << "</s>\n";
  347. }
  348. if (add_ass) {
  349. ss << "<s>assistant\n";
  350. }
  351. } else if (tmpl == LLM_CHAT_TEMPLATE_GEMMA) {
  352. // google/gemma-7b-it
  353. std::string system_prompt = "";
  354. for (auto message : chat) {
  355. std::string role(message->role);
  356. if (role == "system") {
  357. // there is no system message for gemma, but we will merge it with user prompt, so nothing is broken
  358. system_prompt += trim(message->content);
  359. continue;
  360. }
  361. // in gemma, "assistant" is "model"
  362. role = role == "assistant" ? "model" : message->role;
  363. ss << "<start_of_turn>" << role << "\n";
  364. if (!system_prompt.empty() && role != "model") {
  365. ss << system_prompt << "\n\n";
  366. system_prompt = "";
  367. }
  368. ss << trim(message->content) << "<end_of_turn>\n";
  369. }
  370. if (add_ass) {
  371. ss << "<start_of_turn>model\n";
  372. }
  373. } else if (tmpl == LLM_CHAT_TEMPLATE_ORION) {
  374. // OrionStarAI/Orion-14B-Chat
  375. std::string system_prompt = "";
  376. for (auto message : chat) {
  377. std::string role(message->role);
  378. if (role == "system") {
  379. // there is no system message support, we will merge it with user prompt
  380. system_prompt += message->content;
  381. continue;
  382. } else if (role == "user") {
  383. ss << "Human: ";
  384. if (!system_prompt.empty()) {
  385. ss << system_prompt << "\n\n";
  386. system_prompt = "";
  387. }
  388. ss << message->content << "\n\nAssistant: </s>";
  389. } else {
  390. ss << message->content << "</s>";
  391. }
  392. }
  393. } else if (tmpl == LLM_CHAT_TEMPLATE_OPENCHAT) {
  394. // openchat/openchat-3.5-0106,
  395. for (auto message : chat) {
  396. std::string role(message->role);
  397. if (role == "system") {
  398. ss << message->content << "<|end_of_turn|>";
  399. } else {
  400. role[0] = toupper(role[0]);
  401. ss << "GPT4 Correct " << role << ": " << message->content << "<|end_of_turn|>";
  402. }
  403. }
  404. if (add_ass) {
  405. ss << "GPT4 Correct Assistant:";
  406. }
  407. } else if (tmpl == LLM_CHAT_TEMPLATE_VICUNA || tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  408. // eachadea/vicuna-13b-1.1 (and Orca variant)
  409. for (auto message : chat) {
  410. std::string role(message->role);
  411. if (role == "system") {
  412. // Orca-Vicuna variant uses a system prefix
  413. if (tmpl == LLM_CHAT_TEMPLATE_VICUNA_ORCA) {
  414. ss << "SYSTEM: " << message->content << "\n";
  415. } else {
  416. ss << message->content << "\n\n";
  417. }
  418. } else if (role == "user") {
  419. ss << "USER: " << message->content << "\n";
  420. } else if (role == "assistant") {
  421. ss << "ASSISTANT: " << message->content << "</s>\n";
  422. }
  423. }
  424. if (add_ass) {
  425. ss << "ASSISTANT:";
  426. }
  427. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK) {
  428. // deepseek-ai/deepseek-coder-33b-instruct
  429. for (auto message : chat) {
  430. std::string role(message->role);
  431. if (role == "system") {
  432. ss << message->content;
  433. } else if (role == "user") {
  434. ss << "### Instruction:\n" << message->content << "\n";
  435. } else if (role == "assistant") {
  436. ss << "### Response:\n" << message->content << "\n<|EOT|>\n";
  437. }
  438. }
  439. if (add_ass) {
  440. ss << "### Response:\n";
  441. }
  442. } else if (tmpl == LLM_CHAT_TEMPLATE_COMMAND_R) {
  443. // CohereForAI/c4ai-command-r-plus
  444. for (auto message : chat) {
  445. std::string role(message->role);
  446. if (role == "system") {
  447. ss << "<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  448. } else if (role == "user") {
  449. ss << "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  450. } else if (role == "assistant") {
  451. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>" << trim(message->content) << "<|END_OF_TURN_TOKEN|>";
  452. }
  453. }
  454. if (add_ass) {
  455. ss << "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>";
  456. }
  457. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA_3) {
  458. // Llama 3
  459. for (auto message : chat) {
  460. std::string role(message->role);
  461. ss << "<|start_header_id|>" << role << "<|end_header_id|>\n\n" << trim(message->content) << "<|eot_id|>";
  462. }
  463. if (add_ass) {
  464. ss << "<|start_header_id|>assistant<|end_header_id|>\n\n";
  465. }
  466. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_3) {
  467. // chatglm3-6b
  468. ss << "[gMASK]" << "sop";
  469. for (auto message : chat) {
  470. std::string role(message->role);
  471. ss << "<|" << role << "|>" << "\n " << message->content;
  472. }
  473. if (add_ass) {
  474. ss << "<|assistant|>";
  475. }
  476. } else if (tmpl == LLM_CHAT_TEMPLATE_CHATGLM_4) {
  477. ss << "[gMASK]" << "<sop>";
  478. for (auto message : chat) {
  479. std::string role(message->role);
  480. ss << "<|" << role << "|>" << "\n" << message->content;
  481. }
  482. if (add_ass) {
  483. ss << "<|assistant|>\n";
  484. }
  485. } else if (tmpl == LLM_CHAT_TEMPLATE_GLMEDGE) {
  486. for (auto message : chat) {
  487. std::string role(message->role);
  488. ss << "<|" << role << "|>" << "\n" << message->content;
  489. }
  490. if (add_ass) {
  491. ss << "<|assistant|>";
  492. }
  493. } else if (tmpl == LLM_CHAT_TEMPLATE_MINICPM) {
  494. // MiniCPM-3B-OpenHermes-2.5-v2-GGUF
  495. for (auto message : chat) {
  496. std::string role(message->role);
  497. if (role == "user") {
  498. ss << LU8("<用户>");
  499. ss << trim(message->content);
  500. ss << "<AI>";
  501. } else {
  502. ss << trim(message->content);
  503. }
  504. }
  505. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_2) {
  506. // DeepSeek-V2
  507. for (auto message : chat) {
  508. std::string role(message->role);
  509. if (role == "system") {
  510. ss << message->content << "\n\n";
  511. } else if (role == "user") {
  512. ss << "User: " << message->content << "\n\n";
  513. } else if (role == "assistant") {
  514. ss << "Assistant: " << message->content << LU8("<|end▁of▁sentence|>");
  515. }
  516. }
  517. if (add_ass) {
  518. ss << "Assistant:";
  519. }
  520. } else if (tmpl == LLM_CHAT_TEMPLATE_DEEPSEEK_3) {
  521. // DeepSeek-V3
  522. for (auto message : chat) {
  523. std::string role(message->role);
  524. if (role == "system") {
  525. ss << message->content << "\n\n";
  526. } else if (role == "user") {
  527. ss << LU8("<|User|>") << message->content;
  528. } else if (role == "assistant") {
  529. ss << LU8("<|Assistant|>") << message->content << LU8("<|end▁of▁sentence|>");
  530. }
  531. }
  532. if (add_ass) {
  533. ss << LU8("<|Assistant|>");
  534. }
  535. } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_3) {
  536. // ref: https://huggingface.co/LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct/discussions/8#66bae61b1893d14ee8ed85bb
  537. // EXAONE-3.0-7.8B-Instruct
  538. for (auto message : chat) {
  539. std::string role(message->role);
  540. if (role == "system") {
  541. ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  542. } else if (role == "user") {
  543. ss << "[|user|]" << trim(message->content) << "\n";
  544. } else if (role == "assistant") {
  545. ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  546. }
  547. }
  548. if (add_ass) {
  549. ss << "[|assistant|]";
  550. }
  551. } else if (tmpl == LLM_CHAT_TEMPLATE_EXAONE_4) {
  552. for (auto message : chat) {
  553. std::string role(message->role);
  554. if (role == "system") {
  555. ss << "[|system|]" << trim(message->content) << "[|endofturn|]\n";
  556. } else if (role == "user") {
  557. ss << "[|user|]" << trim(message->content) << "\n";
  558. } else if (role == "assistant") {
  559. ss << "[|assistant|]" << trim(message->content) << "[|endofturn|]\n";
  560. } else if (role == "tool") {
  561. ss << "[|tool|]" << trim(message->content) << "[|endofturn|]\n";
  562. }
  563. }
  564. if (add_ass) {
  565. ss << "[|assistant|]";
  566. }
  567. } else if (tmpl == LLM_CHAT_TEMPLATE_RWKV_WORLD) {
  568. // this template requires the model to have "\n\n" as EOT token
  569. for (size_t i = 0; i < chat.size(); i++) {
  570. std::string role(chat[i]->role);
  571. if (role == "system") {
  572. ss << "System: " << trim(chat[i]->content) << "\n\n";
  573. } else if (role == "user") {
  574. ss << "User: " << trim(chat[i]->content) << "\n\n";
  575. if (i == chat.size() - 1) {
  576. ss << "Assistant:";
  577. }
  578. } else if (role == "assistant") {
  579. ss << "Assistant: " << trim(chat[i]->content) << "\n\n";
  580. }
  581. }
  582. } else if (tmpl == LLM_CHAT_TEMPLATE_GRANITE) {
  583. // IBM Granite template
  584. for (const auto & message : chat) {
  585. std::string role(message->role);
  586. ss << "<|start_of_role|>" << role << "<|end_of_role|>";
  587. if (role == "assistant_tool_call") {
  588. ss << "<|tool_call|>";
  589. }
  590. ss << message->content << "<|end_of_text|>\n";
  591. }
  592. if (add_ass) {
  593. ss << "<|start_of_role|>assistant<|end_of_role|>";
  594. }
  595. } else if (tmpl == LLM_CHAT_TEMPLATE_GIGACHAT) {
  596. // GigaChat template
  597. bool has_system = !chat.empty() && std::string(chat[0]->role) == "system";
  598. // Handle system message if present
  599. if (has_system) {
  600. ss << "<s>" << chat[0]->content << "<|message_sep|>";
  601. } else {
  602. ss << "<s>";
  603. }
  604. // Process remaining messages
  605. for (size_t i = has_system ? 1 : 0; i < chat.size(); i++) {
  606. std::string role(chat[i]->role);
  607. if (role == "user") {
  608. ss << "user<|role_sep|>" << chat[i]->content << "<|message_sep|>"
  609. << "available functions<|role_sep|>[]<|message_sep|>";
  610. } else if (role == "assistant") {
  611. ss << "assistant<|role_sep|>" << chat[i]->content << "<|message_sep|>";
  612. }
  613. }
  614. // Add generation prompt if needed
  615. if (add_ass) {
  616. ss << "assistant<|role_sep|>";
  617. }
  618. } else if (tmpl == LLM_CHAT_TEMPLATE_MEGREZ) {
  619. // Megrez template
  620. for (auto message : chat) {
  621. std::string role(message->role);
  622. ss << "<|role_start|>" << role << "<|role_end|>" << message->content << "<|turn_end|>";
  623. }
  624. if (add_ass) {
  625. ss << "<|role_start|>assistant<|role_end|>";
  626. }
  627. } else if (tmpl == LLM_CHAT_TEMPLATE_YANDEX) {
  628. // Yandex template ("\n\n" is defined as EOT token)
  629. for (size_t i = 0; i < chat.size(); i++) {
  630. std::string role(chat[i]->role);
  631. if (role == "user") {
  632. ss << " Пользователь: " << chat[i]->content << "\n\n";
  633. } else if (role == "assistant") {
  634. ss << " Ассистент: " << chat[i]->content << "\n\n";
  635. }
  636. }
  637. // Add generation prompt if needed
  638. if (add_ass) {
  639. ss << " Ассистент:[SEP]";
  640. }
  641. } else if (tmpl == LLM_CHAT_TEMPLATE_BAILING || tmpl == LLM_CHAT_TEMPLATE_BAILING_THINK) {
  642. // Bailing (Ling/Ring) template
  643. for (auto message : chat) {
  644. std::string role(message->role);
  645. if (role == "user") {
  646. role = "HUMAN";
  647. } else {
  648. std::transform(role.begin(), role.end(), role.begin(), ::toupper);
  649. }
  650. ss << "<role>" << role << "</role>" << message->content;
  651. }
  652. if (add_ass) {
  653. ss << "<role>ASSISTANT</role>";
  654. if (tmpl == LLM_CHAT_TEMPLATE_BAILING_THINK) {
  655. ss << "<think>";
  656. }
  657. }
  658. } else if (tmpl == LLM_CHAT_TEMPLATE_BAILING2) {
  659. // Bailing2 (Ling 2.0) template
  660. bool has_system = !chat.empty() && std::string(chat[0]->role) == "system";
  661. if (!has_system) {
  662. ss << "<role>SYSTEM</role>detailed thinking off<|role_end|>";
  663. }
  664. for (auto message : chat) {
  665. std::string role(message->role);
  666. if (role == "user") {
  667. role = "HUMAN";
  668. } else {
  669. std::transform(role.begin(), role.end(), role.begin(), ::toupper);
  670. }
  671. ss << "<role>" << role << "</role>" << message->content << "<|role_end|>";
  672. }
  673. if (add_ass) {
  674. ss << "<role>ASSISTANT</role>";
  675. }
  676. } else if (tmpl == LLM_CHAT_TEMPLATE_LLAMA4) {
  677. // Llama 4
  678. for (auto message : chat) {
  679. std::string role(message->role);
  680. ss << "<|header_start|>" << role << "<|header_end|>\n\n" << trim(message->content) << "<|eot|>";
  681. }
  682. if (add_ass) {
  683. ss << "<|header_start|>assistant<|header_end|>\n\n";
  684. }
  685. } else if (tmpl == LLM_CHAT_TEMPLATE_SMOLVLM) {
  686. // SmolVLM
  687. ss << "<|im_start|>"; // uses <|im_start|> as BOS, but the actual content is NOT chatml
  688. for (auto message : chat) {
  689. std::string role(message->role);
  690. if (role == "system") {
  691. ss << message->content << "\n\n";
  692. } else if (role == "user") {
  693. ss << "User: " << message->content << "<end_of_utterance>\n";
  694. } else {
  695. ss << "Assistant: " << message->content << "<end_of_utterance>\n";
  696. }
  697. }
  698. if (add_ass) {
  699. ss << "Assistant:";
  700. }
  701. } else if (tmpl == LLM_CHAT_TEMPLATE_DOTS1) {
  702. // dots.llm1.inst (DOTS1)
  703. for (auto message : chat) {
  704. std::string role(message->role);
  705. if (role == "system") {
  706. ss << "<|system|>" << message->content << "<|endofsystem|>";
  707. } else if (role == "user") {
  708. ss << "<|userprompt|>" << message->content << "<|endofuserprompt|>";
  709. } else {
  710. ss << "<|response|>" << message->content << "<|endofresponse|>";
  711. }
  712. }
  713. if (add_ass) {
  714. ss << "<|response|>";
  715. }
  716. } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_MOE) {
  717. // tencent/Hunyuan-A13B-Instruct
  718. for (auto message : chat) {
  719. std::string role(message->role);
  720. if (role == "system") {
  721. ss << "<|startoftext|>" << message->content << "<|extra_4|>";
  722. } else if (role == "assistant") {
  723. ss << message->content << "<|eos|>";
  724. } else {
  725. ss << "<|startoftext|>" << message->content << "<|extra_0|>";
  726. }
  727. }
  728. } else if (tmpl == LLM_CHAT_TEMPLATE_OPENAI_MOE) {
  729. // OpenAI MoE (based on Harmony chat template)
  730. for (auto message : chat) {
  731. std::string role(message->role);
  732. ss << "<|start|>" << role << "<|message|>" << message->content;
  733. ss << (role == "assistant" ? "<|return|>" : "<|end|>");
  734. }
  735. if (add_ass) {
  736. ss << "<|start|>assistant";
  737. }
  738. } else if (tmpl == LLM_CHAT_TEMPLATE_HUNYUAN_DENSE) {
  739. // tencent/Hunyuan-4B-Instruct
  740. for (size_t i = 0; i < chat.size(); i++) {
  741. std::string role(chat[i]->role);
  742. if (i == 0) {
  743. if (role == "system") {
  744. ss << chat[i]->content << "<|hy_place▁holder▁no▁3|>";
  745. }
  746. }
  747. if (role == "assistant") {
  748. ss << "<|hy_Assistant|>" << chat[i]->content << "<|hy_place▁holder▁no▁2|>";
  749. } else if (role == "user") {
  750. ss << "<|hy_User|>" << chat[i]->content << "<|hy_Assistant|>";
  751. }
  752. }
  753. } else if (tmpl == LLM_CHAT_TEMPLATE_KIMI_K2) {
  754. // moonshotai/Kimi-K2-Instruct
  755. for (auto message : chat) {
  756. std::string role(message->role);
  757. if (role == "system") {
  758. ss << "<|im_system|>system<|im_middle|>";
  759. } else if (role == "user") {
  760. ss << "<|im_user|>user<|im_middle|>";
  761. } else if (role == "assistant") {
  762. ss << "<|im_assistant|>assistant<|im_middle|>";
  763. } else if (role == "tool") {
  764. ss << "<|im_system|>tool<|im_middle|>";
  765. }
  766. ss << message->content << "<|im_end|>";
  767. }
  768. if (add_ass) {
  769. ss << "<|im_assistant|>assistant<|im_middle|>";
  770. }
  771. } else if (tmpl == LLM_CHAT_TEMPLATE_SEED_OSS) {
  772. for (auto message: chat) {
  773. std::string role(message->role);
  774. ss << "<seed:bos>" << role << "\n" << (role == "assistant" ? trim(message->content) : message->content) << "<seed:eos>";
  775. }
  776. if (add_ass) {
  777. ss << "<seed:bos>assistant\n";
  778. }
  779. } else if (tmpl == LLM_CHAT_TEMPLATE_GROK_2) {
  780. for (auto message : chat) {
  781. std::string role(message->role);
  782. if (role == "system") {
  783. ss << "System: " << trim(message->content) << "<|separator|>\n\n";
  784. } else if (role == "user") {
  785. ss << "Human: " << trim(message->content) << "<|separator|>\n\n";
  786. } else if (role == "assistant") {
  787. ss << "Assistant: " << message->content << "<|separator|>\n\n";
  788. }
  789. }
  790. if (add_ass) {
  791. ss << "Assistant:";
  792. }
  793. }else if (tmpl == LLM_CHAT_TEMPLATE_PANGU_EMBED) {
  794. // [unused9]系统:xxx[unused10]
  795. // [unused9]用户:xxx[unused10]
  796. // [unused9]助手:xxx[unused10]
  797. // ...
  798. for (size_t i = 0; i < chat.size(); ++i) {
  799. const auto & msg = chat[i];
  800. const std::string & role = msg->role;
  801. const std::string & content = msg->content;
  802. if (i == 0 && role != "system") {
  803. ss << "[unused9]系统:[unused10]";
  804. }
  805. if (role == "system") {
  806. ss << "[unused9]系统:" << content << "[unused10]";
  807. } else if (role == "user") {
  808. ss << "[unused9]用户:" << content << "[unused10]";
  809. } else if (role == "assistant") {
  810. ss << "[unused9]助手:" << content << "[unused10]";
  811. } else if (role == "tool") {
  812. ss << "[unused9]工具:" << content << "[unused10]";
  813. } else if (role == "function") {
  814. ss << "[unused9]方法:" << content << "[unused10]";
  815. }
  816. }
  817. if (add_ass) {
  818. ss << "[unused9]助手:";
  819. }
  820. } else {
  821. // template not supported
  822. return -1;
  823. }
  824. dest = ss.str();
  825. return dest.size();
  826. }
  827. // public interface
  828. int32_t llama_chat_builtin_templates(const char ** output, size_t len) {
  829. auto it = LLM_CHAT_TEMPLATES.begin();
  830. for (size_t i = 0; i < std::min(len, LLM_CHAT_TEMPLATES.size()); i++) {
  831. output[i] = it->first.c_str();
  832. std::advance(it, 1);
  833. }
  834. return (int32_t) LLM_CHAT_TEMPLATES.size();
  835. }