| @@ -1252,7 +1252,7 @@ async function getStatusTextgen() { | |||
| 1252 | const data = await response.json(); | 1252 | const data = await response.json(); |
| 1253 | if (data) { | 1253 | if (data) { |
| 1254 | const { chat_template, chat_template_hash } = data; | 1254 | const { chat_template, chat_template_hash } = data; |
| 1255 | console.log(`${wantsContextDerivation} ${wantsInstructDerivation} We have chat template ${chat_template.split('\n')[0]}...`); | 1255 | console.log(`We have chat template ${chat_template.split('\n')[0]}...`); |
| 1256 | const templates = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash); | 1256 | const templates = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash); |
| 1257 | if (templates) { | 1257 | if (templates) { |
| 1258 | const { context, instruct } = templates; | 1258 | const { context, instruct } = templates; |
| @@ -1,6 +1,6 @@ | |||
| 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())" | 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 | // note that chat templates must be trimmed to match the llama.cpp metadata value | 2 | // note that chat templates must be trimmed to match the llama.cpp metadata value |
| 3 | const derivations = { | 3 | const hash_derivations = { |
| 4 | // Meta | 4 | // Meta |
| 5 | 'e10ca381b1ccc5cf9db52e371f3b6651576caee0a630b452e2816b2d404d4b65': | 5 | 'e10ca381b1ccc5cf9db52e371f3b6651576caee0a630b452e2816b2d404d4b65': |
| 6 | // Meta-Llama-3.1-8B-Instruct | 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 | export async function deriveTemplatesFromChatTemplate(chat_template, hash) { | 63 | export async function deriveTemplatesFromChatTemplate(chat_template, hash) { |
| 55 | if (hash in derivations) { | 64 | if (hash in hash_derivations) { |
| 56 | const derivation = derivations[hash]; | 65 | return parse_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 | console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`); | 75 | console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`); |
| 66 | return null; | 76 | return null; |
| 67 | } | 77 | } |