added substr derivations with one example

This commit is contained in:
Karl-Johan Alm 2024-11-21 13:01:29 +09:00
parent c30dde8471
commit 3789381c6c
No known key found for this signature in database
GPG Key ID: CF78C98086AB1ECA
2 changed files with 21 additions and 11 deletions

View File

@ -1252,7 +1252,7 @@ async function getStatusTextgen() {
const data = await response.json();
if (data) {
const { chat_template, chat_template_hash } = data;
console.log(`${wantsContextDerivation} ${wantsInstructDerivation} We have chat template ${chat_template.split('\n')[0]}...`);
console.log(`We have chat template ${chat_template.split('\n')[0]}...`);
const templates = await deriveTemplatesFromChatTemplate(chat_template, chat_template_hash);
if (templates) {
const { context, instruct } = templates;

View File

@ -1,6 +1,6 @@
// 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())"
// note that chat templates must be trimmed to match the llama.cpp metadata value
const derivations = {
const hash_derivations = {
// Meta
'e10ca381b1ccc5cf9db52e371f3b6651576caee0a630b452e2816b2d404d4b65':
// Meta-Llama-3.1-8B-Instruct
@ -51,17 +51,27 @@ const derivations = {
,
};
export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
if (hash in derivations) {
const derivation = derivations[hash];
if (typeof derivation === 'string') {
return {
const substr_derivations = {
'<|im_start|>': 'ChatML', // qwen2.5, ...
}
const parse_derivation = derivation => (typeof derivation === 'string') ? {
'context': derivation,
'instruct': derivation,
} : derivation;
export async function deriveTemplatesFromChatTemplate(chat_template, hash) {
if (hash in hash_derivations) {
return parse_derivation(hash_derivations[hash]);
}
// heuristics
for (const [substr, derivation] of Object.entries(substr_derivations) ) {
if (chat_template.includes(substr)) {
return parse_derivation(derivation);
}
}
return derivation;
}
console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`);
return null;
}