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 -5Ignore whitespace
public/scripts/chat-templates.js+13 -5
@@ -85,12 +85,20 @@ const hash_derivations = {
85 '854b703e44ca06bdb196cc471c728d15dbab61e744fe6cdce980086b61646ed1':85 '854b703e44ca06bdb196cc471c728d15dbab61e744fe6cdce980086b61646ed1':
86 'GLM-4'86 'GLM-4'
87 ,87 ,
88};
8988
90const substr_derivations = {89 // Kimi K2, ...
91 '<|im_start|>': 'ChatML', // qwen2.5, ...90 'aab20feb9bc6881f941ea649356130ffbc4943b3c2577c0991e1fba90de5a0fc':
91 'Moonshot AI'
92 ,
92};93};
9394
95const 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
94const parse_derivation = derivation => (typeof derivation === 'string') ? {102const parse_derivation = derivation => (typeof derivation === 'string') ? {
95 'context': derivation,103 'context': derivation,
96 'instruct': derivation,104 'instruct': derivation,
@@ -109,8 +117,8 @@ export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
109 }117 }
110118
111 // heuristics119 // heuristics
112 for (const [substr, derivation] of Object.entries(substr_derivations) ) {120 for (const [derivation, substr] of substr_derivations) {
113 if (chat_template.includes(substr)) {121 if ([substr].flat().every(str => chat_template.includes(str))) {
114 return parse_derivation(derivation);122 return parse_derivation(derivation);
115 }123 }
116 }124 }