Migrate system prompts from imported instruct template

b0c537d014f3707e12bd32e830cf2556152aebd5

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

3 files changed, +43 -7Showing whitespace changes
public/scripts/preset-manager.js+6 -2
@@ -25,7 +25,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashComma
25import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';25import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
26import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';26import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
27import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';27import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
28import { system_prompts } from './sysprompt.js';28import { checkForSystemPromptInInstructTemplate, system_prompts } from './sysprompt.js';
29import {29import {
30 textgenerationwebui_preset_names,30 textgenerationwebui_preset_names,
31 textgenerationwebui_presets,31 textgenerationwebui_presets,
@@ -72,7 +72,7 @@ function autoSelectPreset() {
72 * @param {string} apiId API id72 * @param {string} apiId API id
73 * @returns {PresetManager} Preset manager73 * @returns {PresetManager} Preset manager
74 */74 */
75function getPresetManager(apiId = '') {75export function getPresetManager(apiId = '') {
76 if (!apiId) {76 if (!apiId) {
77 apiId = main_api == 'koboldhorde' ? 'kobold' : main_api;77 apiId = main_api == 'koboldhorde' ? 'kobold' : main_api;
78 }78 }
@@ -183,6 +183,10 @@ class PresetManager {
183 }183 }
184184
185 async savePreset(name, settings) {185 async savePreset(name, settings) {
186 if (this.apiId === 'instruct') {
187 await checkForSystemPromptInInstructTemplate(name, settings);
188 }
189
186 const preset = settings ?? this.getPresetSettings(name);190 const preset = settings ?? this.getPresetSettings(name);
187191
188 const response = await fetch('/api/presets/save', {192 const response = await fetch('/api/presets/save', {
public/scripts/sysprompt.js+30 -5
@@ -1,10 +1,13 @@
1import { saveSettingsDebounced } from '../script.js';1import { saveSettingsDebounced } from '../script.js';
2import { callGenericPopup, POPUP_TYPE } from './popup.js';
2import { power_user } from './power-user.js';3import { power_user } from './power-user.js';
4import { getPresetManager } from './preset-manager.js';
3import { SlashCommand } from './slash-commands/SlashCommand.js';5import { SlashCommand } from './slash-commands/SlashCommand.js';
4import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';6import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
5import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';7import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
6import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';8import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
7import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';9import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
10import { renderTemplateAsync } from './templates.js';
8import { isTrueBoolean, resetScrollHeight } from './utils.js';11import { isTrueBoolean, resetScrollHeight } from './utils.js';
912
10export let system_prompts = [];13export let system_prompts = [];
@@ -39,7 +42,7 @@ export async function loadSystemPrompts(data) {
39 }42 }
4043
41 migrateSystemPromptFromInstructMode();44 migrateSystemPromptFromInstructMode();
42 toggleSyspromptDisabledControls();45 toggleSystemPromptDisabledControls();
4346
44 for (const prompt of system_prompts) {47 for (const prompt of system_prompts) {
45 $('<option>').val(prompt.name).text(prompt.name).appendTo($select);48 $('<option>').val(prompt.name).text(prompt.name).appendTo($select);
@@ -53,7 +56,29 @@ export async function loadSystemPrompts(data) {
53 }56 }
54}57}
5558
56function toggleSyspromptDisabledControls() {59/**
60 * Checks if the instruct template has a system prompt and prompts the user to save it as a system prompt.
61 * @param {string} name Name of the instruct template
62 * @param {object} template Instruct template object
63 */
64export async function checkForSystemPromptInInstructTemplate(name, template) {
65 if ('system_prompt' in template && name && template.system_prompt && !system_prompts.some(x => x.name === name)) {
66 const html = await renderTemplateAsync('migrateInstructPrompt', { prompt: template.system_prompt });
67 const confirm = await callGenericPopup(html, POPUP_TYPE.CONFIRM);
68 if (confirm) {
69 const prompt = { name: name, content: template.system_prompt };
70 const presetManager = getPresetManager('sysprompt');
71 await presetManager.savePreset(prompt.name, prompt);
72 toastr.success(`System prompt "${prompt.name}" has been saved.`);
73 } else {
74 toastr.info('System prompt has been discarded.');
75 }
76
77 delete template.system_prompt;
78 }
79}
80
81function toggleSystemPromptDisabledControls() {
57 $enabled.parent().find('i').toggleClass('toggleEnabled', !!power_user.sysprompt.enabled);82 $enabled.parent().find('i').toggleClass('toggleEnabled', !!power_user.sysprompt.enabled);
58 $contentBlock.toggleClass('disabled', !power_user.sysprompt.enabled);83 $contentBlock.toggleClass('disabled', !power_user.sysprompt.enabled);
59}84}
@@ -61,7 +86,7 @@ function toggleSyspromptDisabledControls() {
61function enableSystemPromptCallback() {86function enableSystemPromptCallback() {
62 power_user.sysprompt.enabled = true;87 power_user.sysprompt.enabled = true;
63 $enabled.prop('checked', true);88 $enabled.prop('checked', true);
64 toggleSyspromptDisabledControls();89 toggleSystemPromptDisabledControls();
65 saveSettingsDebounced();90 saveSettingsDebounced();
66 return '';91 return '';
67}92}
@@ -69,7 +94,7 @@ function enableSystemPromptCallback() {
69function disableSystemPromptCallback() {94function disableSystemPromptCallback() {
70 power_user.sysprompt.enabled = false;95 power_user.sysprompt.enabled = false;
71 $enabled.prop('checked', false);96 $enabled.prop('checked', false);
72 toggleSyspromptDisabledControls();97 toggleSystemPromptDisabledControls();
73 saveSettingsDebounced();98 saveSettingsDebounced();
74 return '';99 return '';
75}100}
@@ -112,7 +137,7 @@ function selectSystemPromptCallback(args, name) {
112jQuery(function () {137jQuery(function () {
113 $enabled.on('input', function () {138 $enabled.on('input', function () {
114 power_user.sysprompt.enabled = !!$(this).prop('checked');139 power_user.sysprompt.enabled = !!$(this).prop('checked');
115 toggleSyspromptDisabledControls();140 toggleSystemPromptDisabledControls();
116 saveSettingsDebounced();141 saveSettingsDebounced();
117 });142 });
118143
public/scripts/templates/migrateInstructPrompt.html+7 -0
@@ -0,0 +1,7 @@
1<h3>
2 This instruct template also contains a system prompt.
3</h3>
4<div class="marginBot10">
5 Would you like to migrate the system prompt from the template?
6</div>
7<textarea class="wide100p textarea_compact" rows="8">{{prompt}}</textarea>