Apply preset manager for instruct presets

This commit is contained in:
Cohee 2023-08-21 00:56:53 +03:00
parent 8bb59a9e22
commit 897ad28477
17 changed files with 146 additions and 99 deletions

View File

@ -149,7 +149,6 @@
<div class="scrollableInner">
<div class="flex-container" id="ai_response_configuration">
<div id="respective-presets-block" class="width100p">
<input type="file" hidden data-preset-manager-file="" accept=".json, .settings">
<div id="kobold_api-presets">
<h3><span data-i18n="kobldpresets">Kobold Presets</span>
<a href="https://docs.sillytavern.app/usage/api-connections/koboldai/" class="notes-link" target="_blank">
@ -161,6 +160,7 @@
<select id="settings_perset" data-preset-manager-for="kobold">
<option value="gui" data-i18n="guikoboldaisettings">GUI KoboldAI Settings</option>
</select>
<input type="file" hidden data-preset-manager-file="kobold" accept=".json, .settings">
<i data-preset-manager-update="kobold" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
<i data-preset-manager-new="kobold" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
<i data-preset-manager-import="kobold" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
@ -179,6 +179,7 @@
<select id="settings_perset_novel" data-preset-manager-for="novel">
<option value="gui" data-i18n="default">Default</option>
</select>
<input type="file" hidden data-preset-manager-file="novel" accept=".json, .settings">
<i data-preset-manager-update="novel" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
<i data-preset-manager-new="novel" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
<i data-preset-manager-import="novel" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
@ -208,6 +209,7 @@
<div class="preset_buttons">
<select id="settings_preset_textgenerationwebui" data-preset-manager-for="textgenerationwebui">
</select>
<input type="file" hidden data-preset-manager-file="textgenerationwebui" accept=".json, .settings">
<i data-preset-manager-update="textgenerationwebui" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
<i data-preset-manager-new="textgenerationwebui" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
<i data-preset-manager-import="textgenerationwebui" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
@ -2141,12 +2143,15 @@
<label for="instruct_presets">
<span data-i18n="Presets">Presets</span>
</label>
<div class="flex-container">
<select id="instruct_presets" class="flex1 margin0"></select>
<div id="instruct_set_default" class="menu_button menu_button_icon margin0" title="Auto-select this preset on API connection.">
<i class="fa-solid fa-xs fa-fw fa-heart"></i>
<span data-i18n="Default">Default</span>
</div>
<div class="preset_buttons">
<select id="instruct_presets" data-preset-manager-for="instruct" class="flex1"></select>
<input type="file" hidden data-preset-manager-file="instruct" accept=".json, .settings">
<i id="instruct_set_default" class="menu_button fa-solid fa-heart" title="Auto-select this preset on API connection."></i>
<i data-preset-manager-update="instruct" class="menu_button fa-solid fa-save" title="Update current preset" data-i18n="[title]Update current preset"></i>
<i data-preset-manager-new="instruct" class="menu_button fa-solid fa-plus" title="Create new preset" data-i18n="[title]Create new preset"></i>
<i data-preset-manager-import="instruct" class="menu_button fa-solid fa-file-import" title="Import preset" data-i18n="[title]Import preset"></i>
<i data-preset-manager-export="instruct" class="menu_button fa-solid fa-file-export" title="Export preset" data-i18n="[title]Export preset"></i>
<i data-preset-manager-delete="instruct" class="menu_button fa-solid fa-trash-can" title="Delete the preset" data-i18n="[title]Delete the preset"></i>
</div>
<label>
<small data-i18n="Activation Regex">

View File

@ -10,5 +10,6 @@
"wrap": true,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": false,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": false,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": false,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": false,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": true,
"macro": true,
"names": true,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": true,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": false,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": true,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": true,
"macro": true,
"names": false,
"names_force_groups": true
"names_force_groups": true,
"activation_regex": ""
}

View File

@ -10,5 +10,6 @@
"wrap": true,
"macro": true,
"names": false,
"names_force_groups": false
"names_force_groups": false,
"activation_regex": ""
}

View File

@ -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();
});
});

View File

@ -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 = '***';

View File

@ -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;
}

View File

@ -1260,10 +1260,6 @@ select option:not(:checked) {
color: var(--active) !important;
}
#instruct_set_default {
font-size: smaller;
}
#instruct_set_default.default {
color: var(--preferred) !important;
}

View File

@ -3679,14 +3679,14 @@ app.post("/save_preset", jsonParser, function (request, response) {
return response.sendStatus(400);
}
const filename = `${name}.settings`;
const directory = getPresetFolderByApiId(request.body.apiId);
const settings = getPresetSettingsByAPI(request.body.apiId);
const filename = name + settings.extension;
if (!directory) {
if (!settings.folder) {
return response.sendStatus(400);
}
const fullpath = path.join(directory, filename);
const fullpath = path.join(settings.folder, filename);
writeFileAtomicSync(fullpath, JSON.stringify(request.body.preset, null, 4), 'utf-8');
return response.send({ name });
});
@ -3697,16 +3697,16 @@ app.post("/delete_preset", jsonParser, function (request, response) {
return response.sendStatus(400);
}
const filename = `${name}.settings`;
const directory = getPresetFolderByApiId(request.body.apiId);
const settings = getPresetSettingsByAPI(request.body.apiId);
const filename = name + settings.extension;
if (!directory) {
if (!settings.folder) {
return response.sendStatus(400);
}
const fullpath = path.join(directory, filename);
const fullpath = path.join(settings.folder, filename);
if (fs.existsSync) {
if (fs.existsSync(fullpath)) {
fs.unlinkSync(fullpath);
return response.sendStatus(200);
} else {
@ -3726,17 +3726,19 @@ app.post("/savepreset_openai", jsonParser, function (request, response) {
return response.send({ name });
});
function getPresetFolderByApiId(apiId) {
function getPresetSettingsByAPI(apiId) {
switch (apiId) {
case 'kobold':
case 'koboldhorde':
return directories.koboldAI_Settings;
return { folder: directories.koboldAI_Settings, extension: '.settings' };
case 'novel':
return directories.novelAI_Settings;
return { folder: directories.novelAI_Settings, extension: '.settings' };
case 'textgenerationwebui':
return directories.textGen_Settings;
return { folder: directories.textGen_Settings, extension: '.settings' };
case 'instruct':
return { folder: directories.instruct, extension: '.json' };
default:
return null;
return { folder: null, extension: null };
}
}