From 3789381c6cbd0b495c31a3b6c5236d57d4b7eb79 Mon Sep 17 00:00:00 2001 From: Karl-Johan Alm Date: Thu, 21 Nov 2024 13:01:29 +0900 Subject: [PATCH] added substr derivations with one example --- public/script.js | 2 +- public/scripts/chat-cemplates.js | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/public/script.js b/public/script.js index be298e0a3..ced5f614e 100644 --- a/public/script.js +++ b/public/script.js @@ -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; diff --git a/public/scripts/chat-cemplates.js b/public/scripts/chat-cemplates.js index bcc136803..bad13e557 100644 --- a/public/scripts/chat-cemplates.js +++ b/public/scripts/chat-cemplates.js @@ -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 = { , }; +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 derivations) { - const derivation = derivations[hash]; - if (typeof derivation === 'string') { - return { - 'context': derivation, - 'instruct': derivation, - } - } - return derivation; + 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); + } + } + console.log(`Unknown chat template hash: ${hash} for [${chat_template}]`); return null; }