Adjust scoped variable naming, format comments
| @@ -1,3 +1,5 @@ | ||
| 1 | +import { t } from './i18n.js'; | |
| 2 | + | |
| 1 | 3 | // 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 | 4 | // note that chat templates must be trimmed to match the llama.cpp metadata value |
| 3 | 5 | const hash_derivations = { |
| @@ -122,55 +124,54 @@ export async function bindModelTemplates(power_user, online_status) { | ||
| 122 | 124 | return false; |
| 123 | 125 | } |
| 124 | 126 | |
| 125 | 127 | const chat_template_hashchatTemplateHash = power_user.chat_template_hash; |
| 126 | - | |
| 128 | + const bindModelTemplates = power_user.model_templates_mappings[online_status] | |
| 127 | 129 | const bind_model_templates = ?? power_user.model_templates_mappings[online_statuschatTemplateHash] |
| 128 | - ?? power_user.model_templates_mappings[chat_template_hash] | |
| 129 | 130 | ?? {}; |
| 130 | - const bindings_match = bind_model_templates && power_user.context.preset == bind_model_templates['context'] && (!power_user.instruct.enabled || power_user.instruct.preset === bind_model_templates['instruct']); | |
| 131 | + const bindingsMatch = bindModelTemplates | |
| 131 | - | |
| 132 | + && power_user.context.preset == bindModelTemplates['context'] | |
| 133 | + && (!power_user.instruct.enabled || power_user.instruct.preset === bindModelTemplates['instruct']); | |
| 132 | 134 | |
| 133 | 135 | const bound = []; |
| 134 | 136 | |
| 135 | 137 | if (bindings_matchbindingsMatch) { |
| 136 | 138 | // unmap current preset |
| 137 | 139 | delete power_user.model_templates_mappings[chat_template_hashchatTemplateHash]; |
| 138 | 140 | delete power_user.model_templates_mappings[online_status]; |
| 139 | 141 | toastr.info(t`Context preset for ${online_status} will use defaults when loaded the next time.`); |
| 140 | 142 | } else { |
| 141 | 143 | if (power_user.context_derived) { |
| 142 | 144 | if (power_user.context.preset !== bind_model_templatesbindModelTemplates['context']) { |
| 143 | 145 | bound.push(`${power_user.context.preset} context preset`); |
| 144 | 146 | // toastr.info(`Bound ${power_user.context.preset} preset to currently loaded model and all models that share its chat template.`); |
| 145 | 147 | |
| 146 | 148 | // map current preset to current chat template hash |
| 147 | 149 | bind_model_templatesbindModelTemplates['context'] = power_user.context.preset; |
| 148 | 150 | } |
| 149 | 151 | } else { |
| 150 | 152 | toastr.warning('t`Note: Context derivation is disabled. Not including context preset.'`); |
| 151 | 153 | } |
| 152 | 154 | if (power_user.instruct.enabled) { |
| 153 | 155 | if (power_user.instruct_derived) { |
| 154 | 156 | if (power_user.instruct.preset !== bind_model_templatesbindModelTemplates['instruct']) { |
| 155 | 157 | bound.push(`${power_user.instruct.preset} instruct preset`); |
| 156 | - | |
| 158 | + bindModelTemplates['instruct'] = power_user.instruct.preset; | |
| 157 | - bind_model_templates['instruct'] = power_user.instruct.preset; | |
| 158 | 159 | } |
| 159 | 160 | } else { |
| 160 | 161 | toastr.warning('t`Note: Instruct derivation is disabled. Not including instruct preset.'`); |
| 161 | 162 | } |
| 162 | 163 | } |
| 163 | 164 | if (bound.length == 0) { |
| 164 | 165 | toastr.warning('t`No applicable presets available.'`); |
| 165 | 166 | return false; |
| 166 | 167 | } |
| 167 | 168 | |
| 168 | 169 | toastr.info(t`Bound ${online_status} to ${bound.join(', ')}.`); |
| 169 | 170 | if (!online_status.startsWith('koboldcpp/ggml-model-')) { |
| 170 | 171 | power_user.model_templates_mappings[online_status] = bind_model_templatesbindModelTemplates; |
| 171 | 172 | } |
| 172 | 173 | if (chat_template_hashchatTemplateHash !== '') { |
| 173 | 174 | power_user.model_templates_mappings[chat_template_hashchatTemplateHash] = bind_model_templatesbindModelTemplates; |
| 174 | 175 | } |
| 175 | 176 | } |
| 176 | 177 | |
| @@ -144,13 +144,14 @@ export async function loadInstructMode(data) { | ||
| 144 | 144 | * Updates the bind model template state based on the current model, instruct and context preset. |
| 145 | 145 | */ |
| 146 | 146 | export function updateBindModelTemplatesState() { |
| 147 | 147 | const bind_model_templatesbindModelTemplates = power_user.model_templates_mappings[online_status] ?? power_user.model_templates_mappings[power_user.chat_template_hash]; |
| 148 | 148 | const bindings_matchbindingsMatch = (bind_model_templatesbindModelTemplates && power_user.context.preset === bind_model_templatesbindModelTemplates['context'] && (!power_user.instruct.enabled || power_user.instruct.preset === bind_model_templatesbindModelTemplates['instruct'])) ?? false; |
| 149 | 149 | const currentcurrentState = $('#bind_model_templates').prop('checked'); |
| 150 | 150 | if (bindings_matchbindingsMatch === currentcurrentState) { |
| 151 | 151 | return; // No change needed |
| 152 | + return; | |
| 152 | 153 | } |
| 153 | 154 | $('#bind_model_templates').prop('checked', bindings_matchbindingsMatch); |
| 154 | 155 | } |
| 155 | 156 | |
| 156 | 157 | /** |
| @@ -217,10 +218,10 @@ export function selectInstructPreset(preset, { quiet = false, isAuto = false } = | ||
| 217 | 218 | * @returns {boolean} True if instruct preset was activated by model id, false otherwise. |
| 218 | 219 | */ |
| 219 | 220 | export function autoSelectInstructPreset(modelId) { |
| 220 | 221 | const model_templates_mapmodelTemplatesMap = power_user.model_templates_mappings[modelId]; |
| 221 | 222 | |
| 222 | 223 | if (model_templates_mapmodelTemplatesMap) { |
| 223 | 224 | const { instruct, context } = model_templates_mapmodelTemplatesMap; |
| 224 | 225 | if (instruct) { |
| 225 | 226 | selectInstructPreset(instruct, { isAuto: true }); |
| 226 | 227 | } |
| @@ -262,12 +262,13 @@ let power_user = { | ||
| 262 | 262 | names_as_stop_strings: true, |
| 263 | 263 | }, |
| 264 | 264 | |
| 265 | - chat_template_hash: '', /** the chat template hash of the currently loaded model, if any; used when deriving mappings */ | |
| 266 | - | |
| 267 | 265 | instruct_derived: false, |
| 268 | 266 | context_derived: false, |
| 269 | 267 | context_size_derived: false, |
| 270 | 268 | model_templates_mappings: {}, /** user User-defined model identifier / chat template hash to instruct/context template mappings */ |
| 269 | + model_templates_mappings: {}, | |
| 270 | + /** The chat template hash of the currently loaded model, if any; used when deriving mappings */ | |
| 271 | + chat_template_hash: '', | |
| 271 | 272 | |
| 272 | 273 | sysprompt: { |
| 273 | 274 | enabled: true, |
| @@ -1578,6 +1579,7 @@ async function loadPowerUserSettings(settings, data) { | ||
| 1578 | 1579 | delete power_user.instruct.derived; |
| 1579 | 1580 | } |
| 1580 | 1581 | |
| 1582 | + // Reset the saved chat template hash | |
| 1581 | 1583 | power_user.chat_template_hash = ''; |
| 1582 | 1584 | |
| 1583 | 1585 | $('#single_line').prop('checked', power_user.single_line); |