From fbb6180bc1b4fa15afc8cd1bfa064851bbe49bff Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 29 Jun 2025 18:09:47 +0300 Subject: [PATCH] Adjust scoped variable naming, format comments --- public/scripts/chat-templates.js | 43 ++++++++++++++++---------------- public/scripts/instruct-mode.js | 19 +++++++------- public/scripts/power-user.js | 8 +++--- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/public/scripts/chat-templates.js b/public/scripts/chat-templates.js index 2408bfeb5..7d65acb5a 100644 --- a/public/scripts/chat-templates.js +++ b/public/scripts/chat-templates.js @@ -1,3 +1,5 @@ +import { t } from './i18n.js'; + // 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 hash_derivations = { @@ -122,55 +124,54 @@ export async function bindModelTemplates(power_user, online_status) { return false; } - const chat_template_hash = power_user.chat_template_hash; - - const bind_model_templates = power_user.model_templates_mappings[online_status] - ?? power_user.model_templates_mappings[chat_template_hash] + const chatTemplateHash = power_user.chat_template_hash; + const bindModelTemplates = power_user.model_templates_mappings[online_status] + ?? power_user.model_templates_mappings[chatTemplateHash] ?? {}; - 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']); - + const bindingsMatch = bindModelTemplates + && power_user.context.preset == bindModelTemplates['context'] + && (!power_user.instruct.enabled || power_user.instruct.preset === bindModelTemplates['instruct']); const bound = []; - if (bindings_match) { + if (bindingsMatch) { // unmap current preset - delete power_user.model_templates_mappings[chat_template_hash]; + delete power_user.model_templates_mappings[chatTemplateHash]; delete power_user.model_templates_mappings[online_status]; - toastr.info(`Context preset for ${online_status} will use defaults when loaded the next time.`); + toastr.info(t`Context preset for ${online_status} will use defaults when loaded the next time.`); } else { if (power_user.context_derived) { - if (power_user.context.preset !== bind_model_templates['context']) { + if (power_user.context.preset !== bindModelTemplates['context']) { bound.push(`${power_user.context.preset} context preset`); // toastr.info(`Bound ${power_user.context.preset} preset to currently loaded model and all models that share its chat template.`); // map current preset to current chat template hash - bind_model_templates['context'] = power_user.context.preset; + bindModelTemplates['context'] = power_user.context.preset; } } else { - toastr.warning('Note: Context derivation is disabled. Not including context preset.'); + toastr.warning(t`Note: Context derivation is disabled. Not including context preset.`); } if (power_user.instruct.enabled) { if (power_user.instruct_derived) { - if (power_user.instruct.preset !== bind_model_templates['instruct']) { + if (power_user.instruct.preset !== bindModelTemplates['instruct']) { bound.push(`${power_user.instruct.preset} instruct preset`); - - bind_model_templates['instruct'] = power_user.instruct.preset; + bindModelTemplates['instruct'] = power_user.instruct.preset; } } else { - toastr.warning('Note: Instruct derivation is disabled. Not including instruct preset.'); + toastr.warning(t`Note: Instruct derivation is disabled. Not including instruct preset.`); } } if (bound.length == 0) { - toastr.warning('No applicable presets available.'); + toastr.warning(t`No applicable presets available.`); return false; } - toastr.info(`Bound ${online_status} to ${bound.join(', ')}.`); + toastr.info(t`Bound ${online_status} to ${bound.join(', ')}.`); if (!online_status.startsWith('koboldcpp/ggml-model-')) { - power_user.model_templates_mappings[online_status] = bind_model_templates; + power_user.model_templates_mappings[online_status] = bindModelTemplates; } - if (chat_template_hash !== '') { - power_user.model_templates_mappings[chat_template_hash] = bind_model_templates; + if (chatTemplateHash !== '') { + power_user.model_templates_mappings[chatTemplateHash] = bindModelTemplates; } } diff --git a/public/scripts/instruct-mode.js b/public/scripts/instruct-mode.js index 2530de91a..70240831c 100644 --- a/public/scripts/instruct-mode.js +++ b/public/scripts/instruct-mode.js @@ -144,13 +144,14 @@ export async function loadInstructMode(data) { * Updates the bind model template state based on the current model, instruct and context preset. */ export function updateBindModelTemplatesState() { - const bind_model_templates = power_user.model_templates_mappings[online_status] ?? power_user.model_templates_mappings[power_user.chat_template_hash]; - 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; - const current = $('#bind_model_templates').prop('checked'); - if (bindings_match === current) { - return; // No change needed + const bindModelTemplates = power_user.model_templates_mappings[online_status] ?? power_user.model_templates_mappings[power_user.chat_template_hash]; + const bindingsMatch = (bindModelTemplates && power_user.context.preset === bindModelTemplates['context'] && (!power_user.instruct.enabled || power_user.instruct.preset === bindModelTemplates['instruct'])) ?? false; + const currentState = $('#bind_model_templates').prop('checked'); + if (bindingsMatch === currentState) { + // No change needed + return; } - $('#bind_model_templates').prop('checked', bindings_match); + $('#bind_model_templates').prop('checked', bindingsMatch); } /** @@ -217,10 +218,10 @@ export function selectInstructPreset(preset, { quiet = false, isAuto = false } = * @returns {boolean} True if instruct preset was activated by model id, false otherwise. */ export function autoSelectInstructPreset(modelId) { - const model_templates_map = power_user.model_templates_mappings[modelId]; + const modelTemplatesMap = power_user.model_templates_mappings[modelId]; - if (model_templates_map) { - const { instruct, context } = model_templates_map; + if (modelTemplatesMap) { + const { instruct, context } = modelTemplatesMap; if (instruct) { selectInstructPreset(instruct, { isAuto: true }); } diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index e575d7cbc..ace44c50c 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -262,12 +262,13 @@ let power_user = { names_as_stop_strings: true, }, - chat_template_hash: '', /** the chat template hash of the currently loaded model, if any; used when deriving mappings */ - instruct_derived: false, context_derived: false, context_size_derived: false, - model_templates_mappings: {}, /** user defined model identifier / chat template hash to instruct/context template mappings */ + /** User-defined model identifier / chat template hash to instruct/context template mappings */ + model_templates_mappings: {}, + /** The chat template hash of the currently loaded model, if any; used when deriving mappings */ + chat_template_hash: '', sysprompt: { enabled: true, @@ -1578,6 +1579,7 @@ async function loadPowerUserSettings(settings, data) { delete power_user.instruct.derived; } + // Reset the saved chat template hash power_user.chat_template_hash = ''; $('#single_line').prop('checked', power_user.single_line);