Decouple system prompts from instruct mode

This commit is contained in:
Cohee
2024-09-17 10:37:36 +00:00
parent 0b0bd27321
commit b377a2b7d3
51 changed files with 324 additions and 183 deletions

View File

@@ -23,7 +23,6 @@ export const names_behavior_types = {
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_prefix', property: 'system_sequence_prefix', isCheckbox: false },
{ id: 'instruct_system_sequence_suffix', property: 'system_sequence_suffix', isCheckbox: false },
{ id: 'instruct_input_sequence', property: 'input_sequence', isCheckbox: false },
@@ -43,7 +42,7 @@ const controls = [
{ id: 'instruct_activation_regex', property: 'activation_regex', isCheckbox: false },
{ id: 'instruct_bind_to_context', property: 'bind_to_context', isCheckbox: true },
{ id: 'instruct_skip_examples', property: 'skip_examples', isCheckbox: true },
{ id: 'instruct_names_behavior input[name="names_behavior"]', property: 'names_behavior', isCheckbox: false },
{ id: 'instruct_names_behavior', property: 'names_behavior', isCheckbox: false },
{ id: 'instruct_system_same_as_user', property: 'system_same_as_user', isCheckbox: true, trigger: true },
];
@@ -109,9 +108,10 @@ export async function loadInstructMode(data) {
if (control.isCheckbox) {
$element.prop('checked', power_user.instruct[control.property]);
} else if (control.property === 'names_behavior') {
const behavior = power_user.instruct[control.property];
$element.filter(`[value="${behavior}"]`).prop('checked', true);
} else if ($element.is('select')) {
const value = power_user.instruct[control.property];
$element.val(value);
$element.filter(`[value="${value}"]`).prop('checked', true);
} else {
$element.val(power_user.instruct[control.property]);
}
@@ -584,9 +584,13 @@ export function replaceInstructMacros(input, env) {
if (!input) {
return '';
}
const syspromptMacros = {
'systemPrompt': (power_user.prefer_character_prompt && env.charPrompt ? env.charPrompt : power_user.sysprompt.content),
'defaultSystemPrompt|instructSystem|instructSystemPrompt': power_user.sysprompt.content,
};
const instructMacros = {
'systemPrompt': (power_user.prefer_character_prompt && env.charPrompt ? env.charPrompt : power_user.instruct.system_prompt),
'instructSystem|instructSystemPrompt': power_user.instruct.system_prompt,
'instructSystemPromptPrefix': power_user.instruct.system_sequence_prefix,
'instructSystemPromptSuffix': power_user.instruct.system_sequence_suffix,
'instructInput|instructUserPrefix': power_user.instruct.input_sequence,
@@ -609,6 +613,11 @@ export function replaceInstructMacros(input, env) {
input = input.replace(regex, power_user.instruct.enabled ? value : '');
}
for (const [placeholder, value] of Object.entries(syspromptMacros)) {
const regex = new RegExp(`{{(${placeholder})}}`, 'gi');
input = input.replace(regex, power_user.sysprompt.enabled ? value : '');
}
input = input.replace(/{{exampleSeparator}}/gi, power_user.context.example_separator);
input = input.replace(/{{chatStart}}/gi, power_user.context.chat_start);
@@ -686,9 +695,10 @@ jQuery(() => {
if (control.isCheckbox) {
$element.prop('checked', power_user.instruct[control.property]).trigger('input');
} else if (control.property === 'names_behavior') {
const behavior = power_user.instruct[control.property];
$element.filter(`[value="${behavior}"]`).prop('checked', true).trigger('input');
} else if ($element.is('select')) {
const value = power_user.instruct[control.property];
$element.val(value);
$element.filter(`[value="${value}"]`).prop('checked', true).trigger('input');
} else {
$element.val(power_user.instruct[control.property]);
$element.trigger('input');

View File

@@ -50,6 +50,7 @@ import { AUTOCOMPLETE_SELECT_KEY, AUTOCOMPLETE_WIDTH } from './autocomplete/Auto
import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
import { POPUP_TYPE, callGenericPopup } from './popup.js';
import { loadSystemPrompts } from './sysprompt.js';
export {
loadPowerUserSettings,
@@ -210,7 +211,6 @@ let power_user = {
instruct: {
enabled: false,
preset: 'Alpaca',
system_prompt: 'Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\nWrite {{char}}\'s next reply in a fictional roleplay chat between {{user}} and {{char}}.\n',
input_sequence: '### Instruction:',
input_suffix: '',
output_sequence: '### Response:',
@@ -245,6 +245,12 @@ let power_user = {
names_as_stop_strings: true,
},
sysprompt: {
enabled: true,
name: 'Blank',
content: '',
},
personas: {},
default_persona: null,
persona_descriptions: {},
@@ -1598,6 +1604,7 @@ async function loadPowerUserSettings(settings, data) {
reloadMarkdownProcessor(power_user.render_formulas);
await loadInstructMode(data);
await loadContextSettings();
await loadSystemPrompts(data);
loadMaxContextUnlocked();
switchWaifuMode();
switchSpoilerMode();

View File

@@ -25,6 +25,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashComma
import { enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import { system_prompts } from './sysprompt.js';
import {
textgenerationwebui_preset_names,
textgenerationwebui_presets,
@@ -228,6 +229,10 @@ class PresetManager {
presets = instruct_presets;
preset_names = instruct_presets.map(x => x.name);
break;
case 'sysprompt':
presets = system_prompts;
preset_names = system_prompts.map(x => x.name);
break;
default:
console.warn(`Unknown API ID ${this.apiId}`);
}
@@ -240,7 +245,7 @@ class PresetManager {
}
isAdvancedFormatting() {
return this.apiId == 'context' || this.apiId == 'instruct';
return this.apiId == 'context' || this.apiId == 'instruct' || this.apiId == 'sysprompt';
}
updateList(name, preset) {
@@ -298,6 +303,11 @@ class PresetManager {
instruct_preset['name'] = name || power_user.instruct.preset;
return instruct_preset;
}
case 'sysprompt': {
const sysprompt_preset = structuredClone(power_user.sysprompt);
sysprompt_preset['name'] = name || power_user.sysprompt.preset;
return sysprompt_preset;
}
default:
console.warn(`Unknown API ID ${apiId}`);
return {};

View File

@@ -0,0 +1,66 @@
import { saveSettingsDebounced } from '../script.js';
import { power_user } from './power-user.js';
import { resetScrollHeight } from './utils.js';
export let system_prompts = [];
const $enabled = $('#sysprompt_enabled');
const $select = $('#sysprompt_select');
const $content = $('#sysprompt_content');
const $contentBlock = $('#SystemPromptBlock');
/**
* Loads sysprompt settings from the given data object.
* @param {object} data Settings data object.
*/
export async function loadSystemPrompts(data) {
if (data.instruct !== undefined) {
system_prompts = data.sysprompt;
}
toggleSyspromptDisabledControls();
for (const prompt of system_prompts) {
$('<option>').val(prompt.name).text(prompt.name).appendTo($select);
}
$enabled.prop('checked', power_user.sysprompt.enabled);
$select.val(power_user.sysprompt.name);
$content.val(power_user.sysprompt.content);
if (!CSS.supports('field-sizing', 'content')) {
await resetScrollHeight($content);
}
}
function toggleSyspromptDisabledControls() {
$enabled.parent().find('i').toggleClass('toggleEnabled', !!power_user.sysprompt.enabled);
$contentBlock.toggleClass('disabled', !power_user.sysprompt.enabled);
}
jQuery(function () {
$enabled.on('input', function () {
power_user.sysprompt.enabled = !!$(this).prop('checked');
toggleSyspromptDisabledControls();
saveSettingsDebounced();
});
$select.on('input', async function () {
const name = String($(this).val());
const prompt = system_prompts.find(p => p.name === name);
if (prompt) {
$content.val(prompt.content);
if (!CSS.supports('field-sizing', 'content')) {
await resetScrollHeight($content);
}
power_user.sysprompt.name = name;
power_user.sysprompt.content = prompt.content;
}
saveSettingsDebounced();
});
$content.on('input', function () {
power_user.sysprompt.content = String($(this).val());
saveSettingsDebounced();
});
});

View File

@@ -57,8 +57,8 @@
<li><tt>&lcub;&lcub;maxPrompt&rcub;&rcub;</tt> <span data-i18n="help_macros_41">max allowed prompt length in tokens = (context size - response length)</span></li>
<li><tt>&lcub;&lcub;exampleSeparator&rcub;&rcub;</tt> <span data-i18n="help_macros_42">context template example dialogues separator</span></li>
<li><tt>&lcub;&lcub;chatStart&rcub;&rcub;</tt> <span data-i18n="help_macros_43">context template chat start line</span></li>
<li><tt>&lcub;&lcub;systemPrompt&rcub;&rcub;</tt> <span data-i18n="help_macros_44">main system prompt (either character prompt override if chosen, or instructSystemPrompt)</span></li>
<li><tt>&lcub;&lcub;instructSystemPrompt&rcub;&rcub;</tt> <span data-i18n="help_macros_45">instruct system prompt</span></li>
<li><tt>&lcub;&lcub;systemPrompt&rcub;&rcub;</tt> <span data-i18n="help_macros_44">system prompt content if enabled (either character prompt override if allowed, or defaultSystemPrompt)</span></li>
<li><tt>&lcub;&lcub;defaultSystemPrompt&rcub;&rcub;</tt> <span data-i18n="help_macros_45">system prompt content</span></li>
<li><tt>&lcub;&lcub;instructSystemPromptPrefix&rcub;&rcub;</tt> <span data-i18n="help_macros_46">instruct system prompt prefix sequence</span></li>
<li><tt>&lcub;&lcub;instructSystemPromptSuffix&rcub;&rcub;</tt> <span data-i18n="help_macros_47">instruct system prompt suffix sequence</span></li>
<li><tt>&lcub;&lcub;instructUserPrefix&rcub;&rcub;</tt> <span data-i18n="help_macros_48">instruct user prefix sequence</span></li>