2024-11-21 11:38:57 +09:00
// 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())"
2024-11-19 11:32:44 +09:00
// note that chat templates must be trimmed to match the llama.cpp metadata value
2024-11-21 13:01:29 +09:00
const hash _derivations = {
2024-11-19 11:32:44 +09:00
// Meta
2024-11-21 11:38:57 +09:00
'e10ca381b1ccc5cf9db52e371f3b6651576caee0a630b452e2816b2d404d4b65' :
2024-11-19 11:32:44 +09:00
// Meta-Llama-3.1-8B-Instruct
// Meta-Llama-3.1-70B-Instruct
2024-11-21 11:38:57 +09:00
'Llama 3 Instruct'
,
'5816fce10444e03c2e9ee1ef8a4a1ea61ae7e69e438613f3b17b69d0426223a4' :
2024-11-19 11:32:44 +09:00
// Llama-3.2-1B-Instruct
// Llama-3.2-3B-Instruct
2024-11-21 11:38:57 +09:00
'Llama 3 Instruct'
,
'73e87b1667d87ab7d7b579107f01151b29ce7f3ccdd1018fdc397e78be76219d' :
// Nemotron 70B
'Llama 3 Instruct'
,
2024-11-19 11:32:44 +09:00
// Mistral
// Mistral Reference: https://github.com/mistralai/mistral-common
2024-11-21 11:38:57 +09:00
'e16746b40344d6c5b5265988e0328a0bf7277be86f1c335156eae07e29c82826' :
2024-11-19 11:32:44 +09:00
// Mistral-Small-Instruct-2409
// Mistral-Large-Instruct-2407
2024-11-21 11:38:57 +09:00
'Mistral V2 & V3'
,
'3c4ad5fa60dd8c7ccdf82fa4225864c903e107728fcaf859fa6052cb80c92ee9' :
2024-11-19 11:32:44 +09:00
// Mistral-Large-Instruct-2411
2024-11-21 11:38:57 +09:00
'Mistral V7' // https://huggingface.co/mistralai/Mistral-Large-Instruct-2411
,
'e4676cb56dffea7782fd3e2b577cfaf1e123537e6ef49b3ec7caa6c095c62272' :
2024-11-19 11:32:44 +09:00
// Mistral-Nemo-Instruct-2407
2024-11-21 11:38:57 +09:00
'Mistral V3-Tekken'
,
'26a59556925c987317ce5291811ba3b7f32ec4c647c400c6cc7e3a9993007ba7' :
2024-11-19 11:32:44 +09:00
// Mistral-7B-Instruct-v0.3
2024-11-21 11:38:57 +09:00
'Mistral V2 & V3'
,
2024-11-19 11:32:44 +09:00
// Gemma
2024-11-21 11:38:57 +09:00
'ecd6ae513fe103f0eb62e8ab5bfa8d0fe45c1074fa398b089c93a7e70c15cfd6' :
2024-11-19 11:32:44 +09:00
// gemma-2-9b-it
// gemma-2-27b-it
2024-11-21 11:38:57 +09:00
'Gemma 2'
,
2024-11-19 11:32:44 +09:00
// Cohere
2024-11-21 11:38:57 +09:00
'3b54f5c219ae1caa5c0bb2cdc7c001863ca6807cf888e4240e8739fa7eb9e02e' :
2024-11-19 11:32:44 +09:00
// command-r-08-2024
2024-11-21 11:38:57 +09:00
'Command R'
,
2024-11-19 11:32:44 +09:00
} ;
2024-11-21 13:01:29 +09:00
const substr _derivations = {
'<|im_start|>' : 'ChatML' , // qwen2.5, ...
2024-11-23 17:30:50 +02:00
} ;
2024-11-21 13:01:29 +09:00
const parse _derivation = derivation => ( typeof derivation === 'string' ) ? {
'context' : derivation ,
'instruct' : derivation ,
} : derivation ;
2024-11-19 20:09:29 +09:00
export async function deriveTemplatesFromChatTemplate ( chat _template , hash ) {
2024-11-21 13:01:29 +09:00
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 ) ;
2024-11-21 11:38:57 +09:00
}
2024-11-19 11:32:44 +09:00
}
2024-11-21 13:01:29 +09:00
2024-11-19 23:38:38 +09:00
console . log ( ` Unknown chat template hash: ${ hash } for [ ${ chat _template } ] ` ) ;
2024-11-19 11:32:44 +09:00
return null ;
}