Migrate system prompts from imported instruct template
| @@ -25,7 +25,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashComma | ||
| 25 | 25 | import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 26 | 26 | import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js'; |
| 27 | 27 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 28 | 28 | import { checkForSystemPromptInInstructTemplate, system_prompts } from './sysprompt.js'; |
| 29 | 29 | import { |
| 30 | 30 | textgenerationwebui_preset_names, |
| 31 | 31 | textgenerationwebui_presets, |
| @@ -72,7 +72,7 @@ function autoSelectPreset() { | ||
| 72 | 72 | * @param {string} apiId API id |
| 73 | 73 | * @returns {PresetManager} Preset manager |
| 74 | 74 | */ |
| 75 | 75 | export function getPresetManager(apiId = '') { |
| 76 | 76 | if (!apiId) { |
| 77 | 77 | apiId = main_api == 'koboldhorde' ? 'kobold' : main_api; |
| 78 | 78 | } |
| @@ -183,6 +183,10 @@ class PresetManager { | ||
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | async savePreset(name, settings) { |
| 186 | + if (this.apiId === 'instruct') { | |
| 187 | + await checkForSystemPromptInInstructTemplate(name, settings); | |
| 188 | + } | |
| 189 | + | |
| 186 | 190 | const preset = settings ?? this.getPresetSettings(name); |
| 187 | 191 | |
| 188 | 192 | const response = await fetch('/api/presets/save', { |
| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | import { saveSettingsDebounced } from '../script.js'; |
| 2 | +import { callGenericPopup, POPUP_TYPE } from './popup.js'; | |
| 2 | 3 | import { power_user } from './power-user.js'; |
| 4 | +import { getPresetManager } from './preset-manager.js'; | |
| 3 | 5 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 4 | 6 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; |
| 5 | 7 | import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 6 | 8 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; |
| 7 | 9 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 10 | +import { renderTemplateAsync } from './templates.js'; | |
| 8 | 11 | import { isTrueBoolean, resetScrollHeight } from './utils.js'; |
| 9 | 12 | |
| 10 | 13 | export let system_prompts = []; |
| @@ -39,7 +42,7 @@ export async function loadSystemPrompts(data) { | ||
| 39 | 42 | } |
| 40 | 43 | |
| 41 | 44 | migrateSystemPromptFromInstructMode(); |
| 42 | 45 | toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls(); |
| 43 | 46 | |
| 44 | 47 | for (const prompt of system_prompts) { |
| 45 | 48 | $('<option>').val(prompt.name).text(prompt.name).appendTo($select); |
| @@ -53,7 +56,29 @@ export async function loadSystemPrompts(data) { | ||
| 53 | 56 | } |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | -function 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 | + */ | |
| 64 | +export 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 | + | |
| 81 | +function toggleSystemPromptDisabledControls() { | |
| 57 | 82 | $enabled.parent().find('i').toggleClass('toggleEnabled', !!power_user.sysprompt.enabled); |
| 58 | 83 | $contentBlock.toggleClass('disabled', !power_user.sysprompt.enabled); |
| 59 | 84 | } |
| @@ -61,7 +86,7 @@ function toggleSyspromptDisabledControls() { | ||
| 61 | 86 | function enableSystemPromptCallback() { |
| 62 | 87 | power_user.sysprompt.enabled = true; |
| 63 | 88 | $enabled.prop('checked', true); |
| 64 | 89 | toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls(); |
| 65 | 90 | saveSettingsDebounced(); |
| 66 | 91 | return ''; |
| 67 | 92 | } |
| @@ -69,7 +94,7 @@ function enableSystemPromptCallback() { | ||
| 69 | 94 | function disableSystemPromptCallback() { |
| 70 | 95 | power_user.sysprompt.enabled = false; |
| 71 | 96 | $enabled.prop('checked', false); |
| 72 | 97 | toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls(); |
| 73 | 98 | saveSettingsDebounced(); |
| 74 | 99 | return ''; |
| 75 | 100 | } |
| @@ -112,7 +137,7 @@ function selectSystemPromptCallback(args, name) { | ||
| 112 | 137 | jQuery(function () { |
| 113 | 138 | $enabled.on('input', function () { |
| 114 | 139 | power_user.sysprompt.enabled = !!$(this).prop('checked'); |
| 115 | 140 | toggleSyspromptDisabledControlstoggleSystemPromptDisabledControls(); |
| 116 | 141 | saveSettingsDebounced(); |
| 117 | 142 | }); |
| 118 | 143 | |
| @@ -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> | |