added substr derivations with one example

3789381c6cbd0b495c31a3b6c5236d57d4b7eb79

Karl-Johan Alm <karljohan-alm@garage.co.jp>

Signed
2 files changed, +18 -8Showing whitespace changes
public/script.js+1 -1
@@ -1252,7 +1252,7 @@ async function getStatusTextgen() {
12521252 const data = await response.json();
12531253 if (data) {
12541254 const { chat_template, chat_template_hash } = data;
12551255 console.log(`${wantsContextDerivation} ${wantsInstructDerivation} We have chat template ${chat_template.split('\n')[0]}...`);
12561256 const templates = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash);
12571257 if (templates) {
12581258 const { context, instruct } = templates;
public/scripts/chat-cemplates.js+17 -7
@@ -1,6 +1,6 @@
11// the hash can be obtained from command line e.g. via: MODEL=path_to_model; python -c "import json, hashlib, sys; print(hashlib.sha256(json.load(open('"$MODEL"/tokenizer_config.json'))['chat_template'].encode()).hexdigest())"
22// note that chat templates must be trimmed to match the llama.cpp metadata value
33const derivationshash_derivations = {
44 // Meta
55 'e10ca381b1ccc5cf9db52e371f3b6651576caee0a630b452e2816b2d404d4b65':
66 // Meta-Llama-3.1-8B-Instruct
@@ -51,17 +51,27 @@ const derivations = {
5151 ,
5252};
5353
54-export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
54+const substr_derivations = {
55- if (hash in derivations) {
55+ '<|im_start|>': 'ChatML', // qwen2.5, ...
56- const derivation = derivations[hash];
56+}
57- if (typeof derivation === 'string') {
57+
58- return {
58+const parse_derivation = derivation => (typeof derivation === 'string') ? {
5959 'context': derivation,
6060 'instruct': derivation,
61+} : derivation;
62+
63+export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
64+ if (hash in hash_derivations) {
65+ return parse_derivation(hash_derivations[hash]);
6166 }
67+
68+ // heuristics
69+ for (const [substr, derivation] of Object.entries(substr_derivations) ) {
70+ if (chat_template.includes(substr)) {
71+ return parse_derivation(derivation);
6272 }
63- return derivation;
6473 }
74+
6575 console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`);
6676 return null;
6777}