Migrate system prompts from imported instruct template
| @@ -25,7 +25,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashComma | |||
| 25 | import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; | 25 | import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 26 | import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js'; | 26 | import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js'; |
| 27 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; | 27 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 28 | import { system_prompts } from './sysprompt.js'; | 28 | import { checkForSystemPromptInInstructTemplate, system_prompts } from './sysprompt.js'; |
| 29 | import { | 29 | import { |
| 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 id | 72 | * @param {string} apiId API id |
| 73 | * @returns {PresetManager} Preset manager | 73 | * @returns {PresetManager} Preset manager |
| 74 | */ | 74 | */ |
| 75 | function getPresetManager(apiId = '') { | 75 | export 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 | } |
| 184 | 184 | ||
| 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); |
| 187 | 191 | ||
| 188 | const response = await fetch('/api/presets/save', { | 192 | const response = await fetch('/api/presets/save', { |
| @@ -1,10 +1,13 @@ | |||
| 1 | import { saveSettingsDebounced } from '../script.js'; | 1 | import { saveSettingsDebounced } from '../script.js'; |
| 2 | import { callGenericPopup, POPUP_TYPE } from './popup.js'; | ||
| 2 | import { power_user } from './power-user.js'; | 3 | import { power_user } from './power-user.js'; |
| 4 | import { getPresetManager } from './preset-manager.js'; | ||
| 3 | import { SlashCommand } from './slash-commands/SlashCommand.js'; | 5 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 4 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; | 6 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; |
| 5 | import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; | 7 | import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 6 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; | 8 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; |
| 7 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; | 9 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 10 | import { renderTemplateAsync } from './templates.js'; | ||
| 8 | import { isTrueBoolean, resetScrollHeight } from './utils.js'; | 11 | import { isTrueBoolean, resetScrollHeight } from './utils.js'; |
| 9 | 12 | ||
| 10 | export let system_prompts = []; | 13 | export let system_prompts = []; |
| @@ -39,7 +42,7 @@ export async function loadSystemPrompts(data) { | |||
| 39 | } | 42 | } |
| 40 | 43 | ||
| 41 | migrateSystemPromptFromInstructMode(); | 44 | migrateSystemPromptFromInstructMode(); |
| 42 | toggleSyspromptDisabledControls(); | 45 | toggleSystemPromptDisabledControls(); |
| 43 | 46 | ||
| 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 | } |
| 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 | $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() { | |||
| 61 | function enableSystemPromptCallback() { | 86 | function 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() { | |||
| 69 | function disableSystemPromptCallback() { | 94 | function 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) { | |||
| 112 | jQuery(function () { | 137 | jQuery(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 | }); |
| 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> | ||