| @@ -1252,7 +1252,7 @@ async function getStatusTextgen() { | ||
| 1252 | 1252 | const data = await response.json(); |
| 1253 | 1253 | if (data) { |
| 1254 | 1254 | const { chat_template, chat_template_hash } = data; |
| 1255 | 1255 | console.log(`${wantsContextDerivation} ${wantsInstructDerivation} We have chat template ${chat_template.split('\n')[0]}...`); |
| 1256 | 1256 | const templates = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash); |
| 1257 | 1257 | if (templates) { |
| 1258 | 1258 | const { context, instruct } = templates; |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | // 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())" |
| 2 | 2 | // note that chat templates must be trimmed to match the llama.cpp metadata value |
| 3 | 3 | const derivationshash_derivations = { |
| 4 | 4 | // Meta |
| 5 | 5 | 'e10ca381b1ccc5cf9db52e371f3b6651576caee0a630b452e2816b2d404d4b65': |
| 6 | 6 | // Meta-Llama-3.1-8B-Instruct |
| @@ -51,17 +51,27 @@ const derivations = { | ||
| 51 | 51 | , |
| 52 | 52 | }; |
| 53 | 53 | |
| 54 | +const substr_derivations = { | |
| 55 | + '<|im_start|>': 'ChatML', // qwen2.5, ... | |
| 56 | +} | |
| 57 | + | |
| 58 | +const parse_derivation = derivation => (typeof derivation === 'string') ? { | |
| 59 | + 'context': derivation, | |
| 60 | + 'instruct': derivation, | |
| 61 | +} : derivation; | |
| 62 | + | |
| 54 | 63 | export async function deriveTemplatesFromChatTemplate(chat_template, hash) { |
| 55 | 64 | if (hash in derivationshash_derivations) { |
| 56 | 65 | const derivation =return derivationsparse_derivation(hash_derivations[hash]); |
| 57 | - if (typeof derivation === 'string') { | |
| 66 | + } | |
| 58 | - return { | |
| 67 | + | |
| 59 | - 'context': derivation, | |
| 68 | + // heuristics | |
| 60 | - 'instruct': derivation, | |
| 69 | + for (const [substr, derivation] of Object.entries(substr_derivations) ) { | |
| 61 | - } | |
| 70 | + if (chat_template.includes(substr)) { | |
| 71 | + return parse_derivation(derivation); | |
| 62 | 72 | } |
| 63 | - return derivation; | |
| 64 | 73 | } |
| 74 | + | |
| 65 | 75 | console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`); |
| 66 | 76 | return null; |
| 67 | 77 | } |