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