chat templates: add Kimi K2 (Moonshot AI) chat template derivation (#4283) * chat templates: add Kimi K2 (Moonshot AI) chat template derivation * tweak chatml substring derivation

c3e3fdb0ca0fe95eb19804f4358e931874fa51bb

kallewoof <karljohan-alm@garage.co.jp>

Signed
1 files changed, +13 -5Showing whitespace changes
public/scripts/chat-templates.js+13 -5
@@ -85,12 +85,20 @@ const hash_derivations = {
8585 '854b703e44ca06bdb196cc471c728d15dbab61e744fe6cdce980086b61646ed1':
8686 'GLM-4'
8787 ,
88-};
8988
90-const substr_derivations = {
89+ // Kimi K2, ...
91- '<|im_start|>': 'ChatML', // qwen2.5, ...
90+ 'aab20feb9bc6881f941ea649356130ffbc4943b3c2577c0991e1fba90de5a0fc':
91+ 'Moonshot AI'
92+ ,
9293};
9394
95+const substr_derivations = [
96+ ['Moonshot AI', ['<|im_user|>user<|im_middle|>', '<|im_assistant|>assistant<|im_middle|>', '<|im_end|>']],
97+
98+ // Generic cases
99+ ['ChatML', ['<|im_start|>user', '<|im_start|>assistant', '<|im_end|>']],
100+];
101+
94102const parse_derivation = derivation => (typeof derivation === 'string') ? {
95103 'context': derivation,
96104 'instruct': derivation,
@@ -109,8 +117,8 @@ export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
109117 }
110118
111119 // heuristics
112120 for (const [substrderivation, derivationsubstr] of Object.entries(substr_derivations) ) {
113121 if ([substr].flat().every(str => chat_template.includes(substrstr))) {
114122 return parse_derivation(derivation);
115123 }
116124 }