Adjust scoped variable naming, format comments

fbb6180bc1b4fa15afc8cd1bfa064851bbe49bff

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +37 -33Ignore whitespace
public/scripts/chat-templates.js+22 -21
@@ -1,3 +1,5 @@
1+import { t } from './i18n.js';
2+
13// 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())"
24// note that chat templates must be trimmed to match the llama.cpp metadata value
35const hash_derivations = {
@@ -122,55 +124,54 @@ export async function bindModelTemplates(power_user, online_status) {
122124 return false;
123125 }
124126
125127 const chat_template_hashchatTemplateHash = power_user.chat_template_hash;
126-
128+ const bindModelTemplates = power_user.model_templates_mappings[online_status]
127129 const bind_model_templates = ?? power_user.model_templates_mappings[online_statuschatTemplateHash]
128- ?? power_user.model_templates_mappings[chat_template_hash]
129130 ?? {};
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']);
132134
133135 const bound = [];
134136
135137 if (bindings_matchbindingsMatch) {
136138 // unmap current preset
137139 delete power_user.model_templates_mappings[chat_template_hashchatTemplateHash];
138140 delete power_user.model_templates_mappings[online_status];
139141 toastr.info(t`Context preset for ${online_status} will use defaults when loaded the next time.`);
140142 } else {
141143 if (power_user.context_derived) {
142144 if (power_user.context.preset !== bind_model_templatesbindModelTemplates['context']) {
143145 bound.push(`${power_user.context.preset} context preset`);
144146 // toastr.info(`Bound ${power_user.context.preset} preset to currently loaded model and all models that share its chat template.`);
145147
146148 // map current preset to current chat template hash
147149 bind_model_templatesbindModelTemplates['context'] = power_user.context.preset;
148150 }
149151 } else {
150152 toastr.warning('t`Note: Context derivation is disabled. Not including context preset.'`);
151153 }
152154 if (power_user.instruct.enabled) {
153155 if (power_user.instruct_derived) {
154156 if (power_user.instruct.preset !== bind_model_templatesbindModelTemplates['instruct']) {
155157 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;
158159 }
159160 } else {
160161 toastr.warning('t`Note: Instruct derivation is disabled. Not including instruct preset.'`);
161162 }
162163 }
163164 if (bound.length == 0) {
164165 toastr.warning('t`No applicable presets available.'`);
165166 return false;
166167 }
167168
168169 toastr.info(t`Bound ${online_status} to ${bound.join(', ')}.`);
169170 if (!online_status.startsWith('koboldcpp/ggml-model-')) {
170171 power_user.model_templates_mappings[online_status] = bind_model_templatesbindModelTemplates;
171172 }
172173 if (chat_template_hashchatTemplateHash !== '') {
173174 power_user.model_templates_mappings[chat_template_hashchatTemplateHash] = bind_model_templatesbindModelTemplates;
174175 }
175176 }
176177
public/scripts/instruct-mode.js+10 -9
@@ -144,13 +144,14 @@ export async function loadInstructMode(data) {
144144 * Updates the bind model template state based on the current model, instruct and context preset.
145145 */
146146export function updateBindModelTemplatesState() {
147147 const bind_model_templatesbindModelTemplates = power_user.model_templates_mappings[online_status] ?? power_user.model_templates_mappings[power_user.chat_template_hash];
148148 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;
149149 const currentcurrentState = $('#bind_model_templates').prop('checked');
150150 if (bindings_matchbindingsMatch === currentcurrentState) {
151151 return; // No change needed
152+ return;
152153 }
153154 $('#bind_model_templates').prop('checked', bindings_matchbindingsMatch);
154155}
155156
156157/**
@@ -217,10 +218,10 @@ export function selectInstructPreset(preset, { quiet = false, isAuto = false } =
217218 * @returns {boolean} True if instruct preset was activated by model id, false otherwise.
218219 */
219220export function autoSelectInstructPreset(modelId) {
220221 const model_templates_mapmodelTemplatesMap = power_user.model_templates_mappings[modelId];
221222
222223 if (model_templates_mapmodelTemplatesMap) {
223224 const { instruct, context } = model_templates_mapmodelTemplatesMap;
224225 if (instruct) {
225226 selectInstructPreset(instruct, { isAuto: true });
226227 }
public/scripts/power-user.js+5 -3
@@ -262,12 +262,13 @@ let power_user = {
262262 names_as_stop_strings: true,
263263 },
264264
265- chat_template_hash: '', /** the chat template hash of the currently loaded model, if any; used when deriving mappings */
266-
267265 instruct_derived: false,
268266 context_derived: false,
269267 context_size_derived: false,
270268 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: '',
271272
272273 sysprompt: {
273274 enabled: true,
@@ -1578,6 +1579,7 @@ async function loadPowerUserSettings(settings, data) {
15781579 delete power_user.instruct.derived;
15791580 }
15801581
1582+ // Reset the saved chat template hash
15811583 power_user.chat_template_hash = '';
15821584
15831585 $('#single_line').prop('checked', power_user.single_line);