mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Apply preset manager for instruct presets
This commit is contained in:
@@ -1,11 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
import { name1, name2, saveSettingsDebounced, substituteParams } from "../script.js";
|
||||
import { saveSettingsDebounced, substituteParams } from "../script.js";
|
||||
import { selected_group } from "./group-chats.js";
|
||||
import { power_user } from "./power-user.js";
|
||||
|
||||
export let instruct_presets = [];
|
||||
|
||||
const controls = [
|
||||
{ id: "instruct_enabled", property: "enabled", isCheckbox: true },
|
||||
{ id: "instruct_wrap", property: "wrap", isCheckbox: true },
|
||||
{ id: "instruct_system_prompt", property: "system_prompt", isCheckbox: false },
|
||||
{ id: "instruct_system_sequence", property: "system_sequence", isCheckbox: false },
|
||||
{ id: "instruct_separator_sequence", property: "separator_sequence", isCheckbox: false },
|
||||
{ id: "instruct_input_sequence", property: "input_sequence", isCheckbox: false },
|
||||
{ id: "instruct_output_sequence", property: "output_sequence", isCheckbox: false },
|
||||
{ id: "instruct_stop_sequence", property: "stop_sequence", isCheckbox: false },
|
||||
{ id: "instruct_names", property: "names", isCheckbox: true },
|
||||
{ id: "instruct_macro", property: "macro", isCheckbox: true },
|
||||
{ id: "instruct_names_force_groups", property: "names_force_groups", isCheckbox: true },
|
||||
{ id: "instruct_last_output_sequence", property: "last_output_sequence", isCheckbox: false },
|
||||
{ id: "instruct_activation_regex", property: "activation_regex", isCheckbox: false },
|
||||
];
|
||||
|
||||
/**
|
||||
* Loads instruct mode settings from the given data object.
|
||||
* @param {object} data Settings data object.
|
||||
@@ -19,22 +35,6 @@ export function loadInstructMode(data) {
|
||||
power_user.instruct.names_force_groups = true;
|
||||
}
|
||||
|
||||
const controls = [
|
||||
{ id: "instruct_enabled", property: "enabled", isCheckbox: true },
|
||||
{ id: "instruct_wrap", property: "wrap", isCheckbox: true },
|
||||
{ id: "instruct_system_prompt", property: "system_prompt", isCheckbox: false },
|
||||
{ id: "instruct_system_sequence", property: "system_sequence", isCheckbox: false },
|
||||
{ id: "instruct_separator_sequence", property: "separator_sequence", isCheckbox: false },
|
||||
{ id: "instruct_input_sequence", property: "input_sequence", isCheckbox: false },
|
||||
{ id: "instruct_output_sequence", property: "output_sequence", isCheckbox: false },
|
||||
{ id: "instruct_stop_sequence", property: "stop_sequence", isCheckbox: false },
|
||||
{ id: "instruct_names", property: "names", isCheckbox: true },
|
||||
{ id: "instruct_macro", property: "macro", isCheckbox: true },
|
||||
{ id: "instruct_names_force_groups", property: "names_force_groups", isCheckbox: true },
|
||||
{ id: "instruct_last_output_sequence", property: "last_output_sequence", isCheckbox: false },
|
||||
{ id: "instruct_activation_regex", property: "activation_regex", isCheckbox: false },
|
||||
];
|
||||
|
||||
controls.forEach(control => {
|
||||
const $element = $(`#${control.id}`);
|
||||
|
||||
@@ -59,43 +59,11 @@ export function loadInstructMode(data) {
|
||||
$('#instruct_presets').append(option);
|
||||
});
|
||||
|
||||
function highlightDefaultPreset() {
|
||||
$('#instruct_set_default').toggleClass('default', power_user.default_instruct === power_user.instruct.preset);
|
||||
}
|
||||
|
||||
$('#instruct_set_default').on('click', function () {
|
||||
power_user.default_instruct = power_user.instruct.preset;
|
||||
$(this).addClass('default');
|
||||
toastr.success(`Default instruct preset set to ${power_user.default_instruct}`);
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
highlightDefaultPreset();
|
||||
}
|
||||
|
||||
$('#instruct_presets').on('change', function () {
|
||||
const name = $(this).find(':selected').val();
|
||||
const preset = instruct_presets.find(x => x.name === name);
|
||||
|
||||
if (!preset) {
|
||||
return;
|
||||
}
|
||||
|
||||
power_user.instruct.preset = name;
|
||||
controls.forEach(control => {
|
||||
if (preset[control.property] !== undefined) {
|
||||
power_user.instruct[control.property] = preset[control.property];
|
||||
const $element = $(`#${control.id}`);
|
||||
|
||||
if (control.isCheckbox) {
|
||||
$element.prop('checked', power_user.instruct[control.property]).trigger('input');
|
||||
} else {
|
||||
$element.val(power_user.instruct[control.property]).trigger('input');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
highlightDefaultPreset();
|
||||
});
|
||||
function highlightDefaultPreset() {
|
||||
$('#instruct_set_default').toggleClass('default', power_user.default_instruct === power_user.instruct.preset);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,3 +235,44 @@ export function formatInstructModePrompt(name, isImpersonate, promptBias, name1,
|
||||
|
||||
return text.trimEnd() + (includeNames ? '' : separator);
|
||||
}
|
||||
|
||||
jQuery(() => {
|
||||
$('#instruct_set_default').on('click', function () {
|
||||
if (power_user.instruct.preset === power_user.default_instruct) {
|
||||
power_user.default_instruct = null;
|
||||
$(this).removeClass('default');
|
||||
toastr.info('Default instruct preset cleared');
|
||||
} else {
|
||||
power_user.default_instruct = power_user.instruct.preset;
|
||||
$(this).addClass('default');
|
||||
toastr.info(`Default instruct preset set to ${power_user.default_instruct}`);
|
||||
}
|
||||
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
$('#instruct_presets').on('change', function () {
|
||||
const name = $(this).find(':selected').val();
|
||||
const preset = instruct_presets.find(x => x.name === name);
|
||||
|
||||
if (!preset) {
|
||||
return;
|
||||
}
|
||||
|
||||
power_user.instruct.preset = name;
|
||||
controls.forEach(control => {
|
||||
if (preset[control.property] !== undefined) {
|
||||
power_user.instruct[control.property] = preset[control.property];
|
||||
const $element = $(`#${control.id}`);
|
||||
|
||||
if (control.isCheckbox) {
|
||||
$element.prop('checked', power_user.instruct[control.property]).trigger('input');
|
||||
} else {
|
||||
$element.val(power_user.instruct[control.property]).trigger('input');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
highlightDefaultPreset();
|
||||
});
|
||||
});
|
||||
|
@@ -42,7 +42,7 @@ export {
|
||||
export const MAX_CONTEXT_DEFAULT = 4096;
|
||||
const MAX_CONTEXT_UNLOCKED = 65536;
|
||||
|
||||
const defaultStoryString = "{{#if description}}{{description}}\n{{/if}}{{#if personality}}{{char}}'s personality: {{personality}}\n{{/if}}{{#if scenario}}Scenario: {{scenario}}{{/if}}";
|
||||
const defaultStoryString = "{{#if system}}{{system}}\n{{/if}}{{#if description}}{{description}}\n{{/if}}{{#if personality}}{{char}}'s personality: {{personality}}\n{{/if}}{{#if scenario}}Scenario: {{scenario}}\n{{/if}}{{#if persona}}{{persona}}\n{{/if}}";
|
||||
const defaultExampleSeparator = '***';
|
||||
const defaultChatStart = '***';
|
||||
|
||||
|
@@ -16,13 +16,15 @@ import {
|
||||
this_chid,
|
||||
} from "../script.js";
|
||||
import { groups, selected_group } from "./group-chats.js";
|
||||
import { instruct_presets } from "./instruct-mode.js";
|
||||
import { kai_settings } from "./kai-settings.js";
|
||||
import { power_user } from "./power-user.js";
|
||||
import {
|
||||
textgenerationwebui_preset_names,
|
||||
textgenerationwebui_presets,
|
||||
textgenerationwebui_settings,
|
||||
} from "./textgen-settings.js";
|
||||
import { download, parseJsonFile, waitUntilCondition } from "./utils.js";
|
||||
import { deepClone, download, parseJsonFile, waitUntilCondition } from "./utils.js";
|
||||
|
||||
const presetManagers = {};
|
||||
|
||||
@@ -164,6 +166,10 @@ class PresetManager {
|
||||
presets = textgenerationwebui_presets;
|
||||
preset_names = textgenerationwebui_preset_names;
|
||||
break;
|
||||
case "instruct":
|
||||
presets = instruct_presets;
|
||||
preset_names = instruct_presets.map(x => x.name);
|
||||
break;
|
||||
default:
|
||||
console.warn(`Unknown API ID ${this.apiId}`);
|
||||
}
|
||||
@@ -171,12 +177,20 @@ class PresetManager {
|
||||
return { presets, preset_names };
|
||||
}
|
||||
|
||||
isKeyedApi() {
|
||||
return this.apiId == "textgenerationwebui" || this.apiId == "instruct";
|
||||
}
|
||||
|
||||
isNonGenericApi() {
|
||||
return this.apiId == "instruct";
|
||||
}
|
||||
|
||||
updateList(name, preset) {
|
||||
const { presets, preset_names } = this.getPresetList();
|
||||
const presetExists = this.apiId == "textgenerationwebui" ? preset_names.includes(name) : Object.keys(preset_names).includes(name);
|
||||
const presetExists = this.isKeyedApi() ? preset_names.includes(name) : Object.keys(preset_names).includes(name);
|
||||
|
||||
if (presetExists) {
|
||||
if (this.apiId == "textgenerationwebui") {
|
||||
if (this.isKeyedApi()) {
|
||||
presets[preset_names.indexOf(name)] = preset;
|
||||
$(this.select).find(`option[value="${name}"]`).prop('selected', true);
|
||||
$(this.select).val(name).trigger("change");
|
||||
@@ -191,8 +205,8 @@ class PresetManager {
|
||||
else {
|
||||
presets.push(preset);
|
||||
const value = presets.length - 1;
|
||||
// ooba is reversed
|
||||
if (this.apiId == "textgenerationwebui") {
|
||||
|
||||
if (this.isKeyedApi()) {
|
||||
preset_names[value] = name;
|
||||
const option = $('<option></option>', { value: name, text: name, selected: true });
|
||||
$(this.select).append(option);
|
||||
@@ -216,6 +230,10 @@ class PresetManager {
|
||||
return nai_settings;
|
||||
case "textgenerationwebui":
|
||||
return textgenerationwebui_settings;
|
||||
case "instruct":
|
||||
const preset = deepClone(power_user.instruct);
|
||||
preset['name'] = power_user.instruct.preset;
|
||||
return preset;
|
||||
default:
|
||||
console.warn(`Unknown API ID ${apiId}`);
|
||||
return {};
|
||||
@@ -231,6 +249,7 @@ class PresetManager {
|
||||
'streaming_novel',
|
||||
'nai_preamble',
|
||||
'model_novel',
|
||||
"enabled",
|
||||
];
|
||||
const settings = Object.assign({}, getSettingsByApiId(this.apiId));
|
||||
|
||||
@@ -240,8 +259,10 @@ class PresetManager {
|
||||
}
|
||||
}
|
||||
|
||||
settings['genamt'] = amount_gen;
|
||||
settings['max_length'] = max_context;
|
||||
if (!this.isNonGenericApi()) {
|
||||
settings['genamt'] = amount_gen;
|
||||
settings['max_length'] = max_context;
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
@@ -258,7 +279,7 @@ class PresetManager {
|
||||
|
||||
$(this.select).find(`option[value="${value}"]`).remove();
|
||||
|
||||
if (this.apiId == "textgenerationwebui") {
|
||||
if (this.isKeyedApi()) {
|
||||
preset_names.splice(preset_names.indexOf(value), 1);
|
||||
} else {
|
||||
delete preset_names[nameToDelete];
|
||||
@@ -331,13 +352,16 @@ jQuery(async () => {
|
||||
});
|
||||
|
||||
$(document).on("click", "[data-preset-manager-import]", async function () {
|
||||
$('[data-preset-manager-file]').trigger('click');
|
||||
const apiId = $(this).data("preset-manager-import");
|
||||
$(`[data-preset-manager-file="${apiId}"]`).trigger('click');
|
||||
});
|
||||
|
||||
$(document).on("change", "[data-preset-manager-file]", async function (e) {
|
||||
const presetManager = getPresetManager();
|
||||
const apiId = $(this).data("preset-manager-file");
|
||||
const presetManager = getPresetManager(apiId);
|
||||
|
||||
if (!presetManager) {
|
||||
console.warn(`Preset Manager not found for API: ${apiId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user