2023-12-02 19:04:51 +01:00
|
|
|
'use strict';
|
2023-08-20 22:29:43 +02:00
|
|
|
|
2024-03-27 23:52:20 +01:00
|
|
|
import { name1, name2, saveSettingsDebounced, substituteParams } from '../script.js';
|
2023-12-02 19:04:51 +01:00
|
|
|
import { selected_group } from './group-chats.js';
|
2024-03-27 23:52:20 +01:00
|
|
|
import { parseExampleIntoIndividual } from './openai.js';
|
2023-08-23 20:17:45 +02:00
|
|
|
import {
|
|
|
|
power_user,
|
|
|
|
context_presets,
|
2023-12-02 19:04:51 +01:00
|
|
|
} from './power-user.js';
|
2024-05-17 00:14:07 +02:00
|
|
|
import { regexFromString, resetScrollHeight } from './utils.js';
|
2023-08-20 22:29:43 +02:00
|
|
|
|
2023-08-22 13:30:49 +02:00
|
|
|
/**
|
|
|
|
* @type {any[]} Instruct mode presets.
|
|
|
|
*/
|
2023-08-20 22:29:43 +02:00
|
|
|
export let instruct_presets = [];
|
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
const controls = [
|
2023-12-02 19:04:51 +01:00
|
|
|
{ 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 },
|
2024-03-27 22:40:10 +01:00
|
|
|
{ id: 'instruct_input_suffix', property: 'input_suffix', isCheckbox: false },
|
2023-12-02 19:04:51 +01:00
|
|
|
{ id: 'instruct_output_sequence', property: 'output_sequence', isCheckbox: false },
|
2024-03-27 22:40:10 +01:00
|
|
|
{ id: 'instruct_output_suffix', property: 'output_suffix', isCheckbox: false },
|
|
|
|
{ id: 'instruct_system_sequence', property: 'system_sequence', isCheckbox: false },
|
|
|
|
{ id: 'instruct_system_suffix', property: 'system_suffix', isCheckbox: false },
|
2024-04-04 21:27:08 +02:00
|
|
|
{ id: 'instruct_last_system_sequence', property: 'last_system_sequence', isCheckbox: false },
|
2024-03-27 22:40:10 +01:00
|
|
|
{ id: 'instruct_user_alignment_message', property: 'user_alignment_message', isCheckbox: false },
|
2023-12-02 19:04:51 +01:00
|
|
|
{ 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_first_output_sequence', property: 'first_output_sequence', isCheckbox: false },
|
|
|
|
{ id: 'instruct_last_output_sequence', property: 'last_output_sequence', isCheckbox: false },
|
|
|
|
{ id: 'instruct_activation_regex', property: 'activation_regex', isCheckbox: false },
|
2023-12-21 14:12:30 +01:00
|
|
|
{ id: 'instruct_bind_to_context', property: 'bind_to_context', isCheckbox: true },
|
2024-03-03 18:12:20 +01:00
|
|
|
{ id: 'instruct_skip_examples', property: 'skip_examples', isCheckbox: true },
|
2024-03-27 23:52:20 +01:00
|
|
|
{ id: 'instruct_system_same_as_user', property: 'system_same_as_user', isCheckbox: true, trigger: true },
|
2023-08-20 23:56:53 +02:00
|
|
|
];
|
|
|
|
|
2024-03-27 22:40:10 +01:00
|
|
|
/**
|
|
|
|
* Migrates instruct mode settings into the evergreen format.
|
|
|
|
* @param {object} settings Instruct mode settings.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function migrateInstructModeSettings(settings) {
|
|
|
|
// Separator sequence => Output suffix
|
2024-03-28 20:51:02 +01:00
|
|
|
if (settings.separator_sequence !== undefined) {
|
|
|
|
settings.output_suffix = settings.separator_sequence || '';
|
|
|
|
delete settings.separator_sequence;
|
2024-03-27 22:40:10 +01:00
|
|
|
}
|
|
|
|
|
2024-03-28 20:51:02 +01:00
|
|
|
const defaults = {
|
|
|
|
input_suffix: '',
|
|
|
|
system_sequence: '',
|
|
|
|
system_suffix: '',
|
|
|
|
user_alignment_message: '',
|
2024-04-04 21:27:08 +02:00
|
|
|
last_system_sequence: '',
|
2024-03-28 20:51:02 +01:00
|
|
|
names_force_groups: true,
|
|
|
|
skip_examples: false,
|
|
|
|
system_same_as_user: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let key in defaults) {
|
|
|
|
if (settings[key] === undefined) {
|
|
|
|
settings[key] = defaults[key];
|
|
|
|
}
|
2024-03-27 23:52:20 +01:00
|
|
|
}
|
2024-03-27 22:40:10 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
/**
|
|
|
|
* Loads instruct mode settings from the given data object.
|
|
|
|
* @param {object} data Settings data object.
|
|
|
|
*/
|
|
|
|
export function loadInstructMode(data) {
|
|
|
|
if (data.instruct !== undefined) {
|
|
|
|
instruct_presets = data.instruct;
|
|
|
|
}
|
|
|
|
|
2024-03-27 22:40:10 +01:00
|
|
|
migrateInstructModeSettings(power_user.instruct);
|
2024-03-03 18:12:20 +01:00
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
controls.forEach(control => {
|
|
|
|
const $element = $(`#${control.id}`);
|
|
|
|
|
|
|
|
if (control.isCheckbox) {
|
|
|
|
$element.prop('checked', power_user.instruct[control.property]);
|
|
|
|
} else {
|
|
|
|
$element.val(power_user.instruct[control.property]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$element.on('input', function () {
|
|
|
|
power_user.instruct[control.property] = control.isCheckbox ? !!$(this).prop('checked') : $(this).val();
|
|
|
|
saveSettingsDebounced();
|
2023-08-25 21:04:06 +02:00
|
|
|
if (!control.isCheckbox) {
|
|
|
|
resetScrollHeight($element);
|
|
|
|
}
|
2023-08-20 22:29:43 +02:00
|
|
|
});
|
2024-03-27 23:52:20 +01:00
|
|
|
|
|
|
|
if (control.trigger) {
|
|
|
|
$element.trigger('input');
|
|
|
|
}
|
2023-08-20 22:29:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
instruct_presets.forEach((preset) => {
|
|
|
|
const name = preset.name;
|
|
|
|
const option = document.createElement('option');
|
|
|
|
option.value = name;
|
|
|
|
option.innerText = name;
|
|
|
|
option.selected = name === power_user.instruct.preset;
|
|
|
|
$('#instruct_presets').append(option);
|
|
|
|
});
|
|
|
|
|
|
|
|
highlightDefaultPreset();
|
2023-08-20 23:56:53 +02:00
|
|
|
}
|
2023-08-20 22:29:43 +02:00
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
function highlightDefaultPreset() {
|
|
|
|
$('#instruct_set_default').toggleClass('default', power_user.default_instruct === power_user.instruct.preset);
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 20:17:45 +02:00
|
|
|
/**
|
|
|
|
* Select context template if not already selected.
|
|
|
|
* @param {string} preset Preset name.
|
|
|
|
*/
|
2024-01-18 16:24:07 +01:00
|
|
|
export function selectContextPreset(preset) {
|
2023-08-23 22:22:52 +02:00
|
|
|
// If context template is not already selected, select it
|
|
|
|
if (preset !== power_user.context.preset) {
|
2023-08-23 20:17:45 +02:00
|
|
|
$('#context_presets').val(preset).trigger('change');
|
|
|
|
toastr.info(`Context Template: preset "${preset}" auto-selected`);
|
2023-08-23 22:22:52 +02:00
|
|
|
}
|
2023-08-23 20:17:45 +02:00
|
|
|
|
2023-08-23 22:22:52 +02:00
|
|
|
// If instruct mode is disabled, enable it, except for default context template
|
2023-08-26 12:09:47 +02:00
|
|
|
if (!power_user.instruct.enabled && preset !== power_user.default_context) {
|
2023-08-23 22:22:52 +02:00
|
|
|
power_user.instruct.enabled = true;
|
|
|
|
$('#instruct_enabled').prop('checked', true).trigger('change');
|
2023-12-02 19:04:51 +01:00
|
|
|
toastr.info('Instruct Mode enabled');
|
2023-08-23 20:17:45 +02:00
|
|
|
}
|
2023-08-23 22:22:52 +02:00
|
|
|
|
|
|
|
saveSettingsDebounced();
|
2023-08-23 20:17:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select instruct preset if not already selected.
|
|
|
|
* @param {string} preset Preset name.
|
|
|
|
*/
|
|
|
|
export function selectInstructPreset(preset) {
|
2023-08-23 22:22:52 +02:00
|
|
|
// If instruct preset is not already selected, select it
|
|
|
|
if (preset !== power_user.instruct.preset) {
|
2023-08-23 20:17:45 +02:00
|
|
|
$('#instruct_presets').val(preset).trigger('change');
|
|
|
|
toastr.info(`Instruct Mode: preset "${preset}" auto-selected`);
|
2023-08-23 22:22:52 +02:00
|
|
|
}
|
2023-08-23 20:17:45 +02:00
|
|
|
|
2023-08-23 22:22:52 +02:00
|
|
|
// If instruct mode is disabled, enable it
|
|
|
|
if (!power_user.instruct.enabled) {
|
|
|
|
power_user.instruct.enabled = true;
|
|
|
|
$('#instruct_enabled').prop('checked', true).trigger('change');
|
2023-12-02 19:04:51 +01:00
|
|
|
toastr.info('Instruct Mode enabled');
|
2023-08-23 20:17:45 +02:00
|
|
|
}
|
2023-08-23 22:22:52 +02:00
|
|
|
|
|
|
|
saveSettingsDebounced();
|
2023-08-23 20:17:45 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
/**
|
|
|
|
* Automatically select instruct preset based on model id.
|
2023-08-21 00:28:58 +02:00
|
|
|
* Otherwise, if default instruct preset is set, selects it.
|
2023-08-20 22:29:43 +02:00
|
|
|
* @param {string} modelId Model name reported by the API.
|
|
|
|
* @returns {boolean} True if instruct preset was activated by model id, false otherwise.
|
|
|
|
*/
|
|
|
|
export function autoSelectInstructPreset(modelId) {
|
2023-08-21 00:28:58 +02:00
|
|
|
// If instruct mode is disabled, don't do anything
|
|
|
|
if (!power_user.instruct.enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-23 22:22:52 +02:00
|
|
|
// Select matching instruct preset
|
|
|
|
let foundMatch = false;
|
|
|
|
for (const instruct_preset of instruct_presets) {
|
|
|
|
// If instruct preset matches the context template
|
2023-12-21 16:19:42 +01:00
|
|
|
if (power_user.instruct.bind_to_context && instruct_preset.name === power_user.context.preset) {
|
2023-08-23 22:22:52 +02:00
|
|
|
foundMatch = true;
|
|
|
|
selectInstructPreset(instruct_preset.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If no match was found, auto-select instruct preset
|
|
|
|
if (!foundMatch) {
|
|
|
|
for (const preset of instruct_presets) {
|
|
|
|
// If activation regex is set, check if it matches the model id
|
|
|
|
if (preset.activation_regex) {
|
|
|
|
try {
|
2024-05-17 00:14:07 +02:00
|
|
|
const regex = regexFromString(preset.activation_regex);
|
2023-08-23 22:22:52 +02:00
|
|
|
|
|
|
|
// Stop on first match so it won't cycle back and forth between presets if multiple regexes match
|
2024-05-17 00:14:07 +02:00
|
|
|
if (regex instanceof RegExp && regex.test(modelId)) {
|
2023-08-23 22:22:52 +02:00
|
|
|
selectInstructPreset(preset.name);
|
2023-08-20 22:29:43 +02:00
|
|
|
|
2023-08-21 00:28:58 +02:00
|
|
|
return true;
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
2023-08-23 22:22:52 +02:00
|
|
|
} catch {
|
|
|
|
// If regex is invalid, ignore it
|
|
|
|
console.warn(`Invalid instruct activation regex in preset "${preset.name}"`);
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:19:42 +01:00
|
|
|
if (power_user.instruct.bind_to_context && power_user.default_instruct && power_user.instruct.preset !== power_user.default_instruct) {
|
2023-08-23 22:22:52 +02:00
|
|
|
if (instruct_presets.some(p => p.name === power_user.default_instruct)) {
|
|
|
|
console.log(`Instruct mode: default preset "${power_user.default_instruct}" selected`);
|
|
|
|
$('#instruct_presets').val(power_user.default_instruct).trigger('change');
|
|
|
|
}
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts instruct mode sequences to an array of stopping strings.
|
|
|
|
* @returns {string[]} Array of instruct mode stopping strings.
|
|
|
|
*/
|
|
|
|
export function getInstructStoppingSequences() {
|
2023-08-22 13:30:49 +02:00
|
|
|
/**
|
|
|
|
* Adds instruct mode sequence to the result array.
|
|
|
|
* @param {string} sequence Sequence string.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-08-20 22:29:43 +02:00
|
|
|
function addInstructSequence(sequence) {
|
|
|
|
// Cohee: oobabooga's textgen always appends newline before the sequence as a stopping string
|
|
|
|
// But it's a problem for Metharme which doesn't use newlines to separate them.
|
|
|
|
const wrap = (s) => power_user.instruct.wrap ? '\n' + s : s;
|
|
|
|
// Sequence must be a non-empty string
|
|
|
|
if (typeof sequence === 'string' && sequence.length > 0) {
|
|
|
|
// If sequence is just a whitespace or newline - we don't want to make it a stopping string
|
|
|
|
// User can always add it as a custom stop string if really needed
|
|
|
|
if (sequence.trim().length > 0) {
|
|
|
|
const wrappedSequence = wrap(sequence);
|
|
|
|
// Need to respect "insert macro" setting
|
|
|
|
const stopString = power_user.instruct.macro ? substituteParams(wrappedSequence) : wrappedSequence;
|
|
|
|
result.push(stopString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
if (power_user.instruct.enabled) {
|
2024-04-02 17:34:29 +02:00
|
|
|
const stop_sequence = power_user.instruct.stop_sequence || '';
|
|
|
|
const input_sequence = power_user.instruct.input_sequence?.replace(/{{name}}/gi, name1) || '';
|
|
|
|
const output_sequence = power_user.instruct.output_sequence?.replace(/{{name}}/gi, name2) || '';
|
|
|
|
const first_output_sequence = power_user.instruct.first_output_sequence?.replace(/{{name}}/gi, name2) || '';
|
|
|
|
const last_output_sequence = power_user.instruct.last_output_sequence?.replace(/{{name}}/gi, name2) || '';
|
|
|
|
const system_sequence = power_user.instruct.system_sequence?.replace(/{{name}}/gi, 'System') || '';
|
2024-04-04 21:27:08 +02:00
|
|
|
const last_system_sequence = power_user.instruct.last_system_sequence?.replace(/{{name}}/gi, 'System') || '';
|
2023-08-20 22:29:43 +02:00
|
|
|
|
2024-04-04 21:27:08 +02:00
|
|
|
const combined_sequence = `${stop_sequence}\n${input_sequence}\n${output_sequence}\n${first_output_sequence}\n${last_output_sequence}\n${system_sequence}\n${last_system_sequence}`;
|
2023-08-20 22:29:43 +02:00
|
|
|
|
|
|
|
combined_sequence.split('\n').filter((line, index, self) => self.indexOf(line) === index).forEach(addInstructSequence);
|
|
|
|
}
|
|
|
|
|
2023-12-01 22:06:37 +01:00
|
|
|
if (power_user.context.use_stop_strings) {
|
|
|
|
if (power_user.context.chat_start) {
|
|
|
|
result.push(`\n${substituteParams(power_user.context.chat_start)}`);
|
|
|
|
}
|
2023-12-01 18:01:06 +01:00
|
|
|
|
2023-12-01 22:06:37 +01:00
|
|
|
if (power_user.context.example_separator) {
|
|
|
|
result.push(`\n${substituteParams(power_user.context.example_separator)}`);
|
|
|
|
}
|
2023-12-01 18:01:06 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:02:11 +02:00
|
|
|
export const force_output_sequence = {
|
|
|
|
FIRST: 1,
|
|
|
|
LAST: 2,
|
2023-12-02 20:11:06 +01:00
|
|
|
};
|
2023-08-25 22:02:11 +02:00
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
/**
|
|
|
|
* Formats instruct mode chat message.
|
|
|
|
* @param {string} name Character name.
|
|
|
|
* @param {string} mes Message text.
|
|
|
|
* @param {boolean} isUser Is the message from the user.
|
|
|
|
* @param {boolean} isNarrator Is the message from the narrator.
|
|
|
|
* @param {string} forceAvatar Force avatar string.
|
|
|
|
* @param {string} name1 User name.
|
|
|
|
* @param {string} name2 Character name.
|
2023-08-25 22:02:11 +02:00
|
|
|
* @param {boolean|number} forceOutputSequence Force to use first/last output sequence (if configured).
|
2023-08-20 22:29:43 +02:00
|
|
|
* @returns {string} Formatted instruct mode chat message.
|
|
|
|
*/
|
2023-08-25 22:02:11 +02:00
|
|
|
export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvatar, name1, name2, forceOutputSequence) {
|
2023-08-20 22:29:43 +02:00
|
|
|
let includeNames = isNarrator ? false : power_user.instruct.names;
|
|
|
|
|
|
|
|
if (!isNarrator && power_user.instruct.names_force_groups && (selected_group || forceAvatar)) {
|
|
|
|
includeNames = true;
|
|
|
|
}
|
|
|
|
|
2024-03-27 23:52:20 +01:00
|
|
|
function getPrefix() {
|
|
|
|
if (isNarrator) {
|
|
|
|
return power_user.instruct.system_same_as_user ? power_user.instruct.input_sequence : power_user.instruct.system_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUser) {
|
|
|
|
return power_user.instruct.input_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceOutputSequence === force_output_sequence.FIRST) {
|
|
|
|
return power_user.instruct.first_output_sequence || power_user.instruct.output_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceOutputSequence === force_output_sequence.LAST) {
|
|
|
|
return power_user.instruct.last_output_sequence || power_user.instruct.output_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
return power_user.instruct.output_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSuffix() {
|
|
|
|
if (isNarrator) {
|
|
|
|
return power_user.instruct.system_same_as_user ? power_user.instruct.input_suffix : power_user.instruct.system_suffix;
|
|
|
|
}
|
2023-08-20 22:29:43 +02:00
|
|
|
|
2024-03-27 23:52:20 +01:00
|
|
|
if (isUser) {
|
|
|
|
return power_user.instruct.input_suffix;
|
2023-08-25 14:01:25 +02:00
|
|
|
}
|
2024-03-27 23:52:20 +01:00
|
|
|
|
|
|
|
return power_user.instruct.output_suffix;
|
2023-08-24 01:22:37 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 23:52:20 +01:00
|
|
|
let prefix = getPrefix() || '';
|
|
|
|
let suffix = getSuffix() || '';
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
if (power_user.instruct.macro) {
|
2024-03-27 23:52:20 +01:00
|
|
|
prefix = substituteParams(prefix, name1, name2);
|
|
|
|
prefix = prefix.replace(/{{name}}/gi, name || 'System');
|
2024-07-12 23:16:31 +02:00
|
|
|
|
|
|
|
suffix = substituteParams(suffix, name1, name2);
|
|
|
|
suffix = suffix.replace(/{{name}}/gi, name || 'System');
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
|
2024-04-02 00:24:36 +02:00
|
|
|
if (!suffix && power_user.instruct.wrap) {
|
|
|
|
suffix = '\n';
|
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
const separator = power_user.instruct.wrap ? '\n' : '';
|
2024-05-06 01:26:37 +02:00
|
|
|
|
|
|
|
// Don't include the name if it's empty
|
|
|
|
const textArray = includeNames && name ? [prefix, `${name}: ${mes}` + suffix] : [prefix, mes + suffix];
|
2023-08-20 22:29:43 +02:00
|
|
|
const text = textArray.filter(x => x).join(separator);
|
2024-05-06 01:26:37 +02:00
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:04:22 +02:00
|
|
|
/**
|
|
|
|
* Formats instruct mode system prompt.
|
|
|
|
* @param {string} systemPrompt System prompt string.
|
|
|
|
* @returns {string} Formatted instruct mode system prompt.
|
|
|
|
*/
|
2024-03-27 23:52:20 +01:00
|
|
|
export function formatInstructModeSystemPrompt(systemPrompt) {
|
2023-08-25 14:01:25 +02:00
|
|
|
const separator = power_user.instruct.wrap ? '\n' : '';
|
|
|
|
|
2023-08-25 22:34:08 +02:00
|
|
|
if (power_user.instruct.system_sequence_prefix) {
|
2024-04-18 22:15:24 +02:00
|
|
|
// TODO: Replace with a proper 'System' prompt entity name input
|
|
|
|
const prefix = power_user.instruct.system_sequence_prefix.replace(/{{name}}/gi, 'System');
|
|
|
|
systemPrompt = prefix + separator + systemPrompt;
|
2023-08-25 14:01:25 +02:00
|
|
|
}
|
2023-08-24 19:33:04 +02:00
|
|
|
|
2023-08-25 14:01:25 +02:00
|
|
|
if (power_user.instruct.system_sequence_suffix) {
|
|
|
|
systemPrompt = systemPrompt + separator + power_user.instruct.system_sequence_suffix;
|
2023-08-23 17:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return systemPrompt;
|
|
|
|
}
|
|
|
|
|
2023-08-20 22:33:26 +02:00
|
|
|
/**
|
|
|
|
* Formats example messages according to instruct mode settings.
|
2024-03-27 23:52:20 +01:00
|
|
|
* @param {string[]} mesExamplesArray Example messages array.
|
2023-08-20 22:33:26 +02:00
|
|
|
* @param {string} name1 User name.
|
|
|
|
* @param {string} name2 Character name.
|
2024-03-27 23:52:20 +01:00
|
|
|
* @returns {string[]} Formatted example messages string.
|
2023-08-20 22:33:26 +02:00
|
|
|
*/
|
2024-03-27 23:52:20 +01:00
|
|
|
export function formatInstructModeExamples(mesExamplesArray, name1, name2) {
|
2024-04-09 10:24:16 +02:00
|
|
|
const blockHeading = power_user.context.example_separator ? `${substituteParams(power_user.context.example_separator)}\n` : '';
|
2024-03-31 09:30:06 +02:00
|
|
|
|
2024-03-03 18:12:20 +01:00
|
|
|
if (power_user.instruct.skip_examples) {
|
2024-03-31 09:30:06 +02:00
|
|
|
return mesExamplesArray.map(x => x.replace(/<START>\n/i, blockHeading));
|
2024-03-03 18:12:20 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 22:33:26 +02:00
|
|
|
const includeNames = power_user.instruct.names || (!!selected_group && power_user.instruct.names_force_groups);
|
|
|
|
|
2024-03-27 23:52:20 +01:00
|
|
|
let inputPrefix = power_user.instruct.input_sequence || '';
|
|
|
|
let outputPrefix = power_user.instruct.output_sequence || '';
|
2024-03-30 11:00:10 +01:00
|
|
|
let inputSuffix = power_user.instruct.input_suffix || '';
|
2024-03-27 23:52:20 +01:00
|
|
|
let outputSuffix = power_user.instruct.output_suffix || '';
|
2023-08-20 22:33:26 +02:00
|
|
|
|
|
|
|
if (power_user.instruct.macro) {
|
2024-03-27 23:52:20 +01:00
|
|
|
inputPrefix = substituteParams(inputPrefix, name1, name2);
|
|
|
|
outputPrefix = substituteParams(outputPrefix, name1, name2);
|
|
|
|
inputSuffix = substituteParams(inputSuffix, name1, name2);
|
|
|
|
outputSuffix = substituteParams(outputSuffix, name1, name2);
|
|
|
|
|
|
|
|
inputPrefix = inputPrefix.replace(/{{name}}/gi, name1);
|
|
|
|
outputPrefix = outputPrefix.replace(/{{name}}/gi, name2);
|
2024-07-12 23:16:31 +02:00
|
|
|
inputSuffix = inputSuffix.replace(/{{name}}/gi, name1);
|
|
|
|
outputSuffix = outputSuffix.replace(/{{name}}/gi, name2);
|
2024-04-02 00:24:36 +02:00
|
|
|
|
|
|
|
if (!inputSuffix && power_user.instruct.wrap) {
|
|
|
|
inputSuffix = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!outputSuffix && power_user.instruct.wrap) {
|
|
|
|
outputSuffix = '\n';
|
|
|
|
}
|
2023-08-20 22:33:26 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
const separator = power_user.instruct.wrap ? '\n' : '';
|
2024-03-31 09:30:06 +02:00
|
|
|
const formattedExamples = [];
|
2024-03-27 23:52:20 +01:00
|
|
|
|
|
|
|
for (const item of mesExamplesArray) {
|
|
|
|
const cleanedItem = item.replace(/<START>/i, '{Example Dialogue:}').replace(/\r/gm, '');
|
|
|
|
const blockExamples = parseExampleIntoIndividual(cleanedItem);
|
|
|
|
|
2024-03-31 09:30:06 +02:00
|
|
|
if (blockExamples.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-20 22:33:26 +02:00
|
|
|
|
2024-03-31 09:30:06 +02:00
|
|
|
if (blockHeading) {
|
2024-03-31 20:27:13 +02:00
|
|
|
formattedExamples.push(blockHeading);
|
2024-03-31 09:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const example of blockExamples) {
|
2024-04-07 01:57:59 +02:00
|
|
|
// If force group/persona names is set, we should override the include names for the user placeholder
|
|
|
|
const includeThisName = includeNames || (power_user.instruct.names_force_groups && example.name == 'example_user');
|
|
|
|
|
2024-03-31 09:30:06 +02:00
|
|
|
const prefix = example.name == 'example_user' ? inputPrefix : outputPrefix;
|
|
|
|
const suffix = example.name == 'example_user' ? inputSuffix : outputSuffix;
|
|
|
|
const name = example.name == 'example_user' ? name1 : name2;
|
2024-04-07 01:57:59 +02:00
|
|
|
const messageContent = includeThisName ? `${name}: ${example.content}` : example.content;
|
2024-03-31 09:30:06 +02:00
|
|
|
const formattedMessage = [prefix, messageContent + suffix].filter(x => x).join(separator);
|
|
|
|
formattedExamples.push(formattedMessage);
|
|
|
|
}
|
|
|
|
}
|
2023-08-20 22:33:26 +02:00
|
|
|
|
2024-03-31 09:30:06 +02:00
|
|
|
if (formattedExamples.length === 0) {
|
|
|
|
return mesExamplesArray.map(x => x.replace(/<START>\n/i, blockHeading));
|
2024-03-27 23:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return formattedExamples;
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats instruct mode last prompt line.
|
|
|
|
* @param {string} name Character name.
|
|
|
|
* @param {boolean} isImpersonate Is generation in impersonation mode.
|
|
|
|
* @param {string} promptBias Prompt bias string.
|
|
|
|
* @param {string} name1 User name.
|
|
|
|
* @param {string} name2 Character name.
|
2024-03-28 20:51:02 +01:00
|
|
|
* @param {boolean} isQuiet Is quiet mode generation.
|
|
|
|
* @param {boolean} isQuietToLoud Is quiet to loud generation.
|
2023-08-22 13:30:49 +02:00
|
|
|
* @returns {string} Formatted instruct mode last prompt line.
|
2023-08-20 22:29:43 +02:00
|
|
|
*/
|
2024-03-28 20:51:02 +01:00
|
|
|
export function formatInstructModePrompt(name, isImpersonate, promptBias, name1, name2, isQuiet, isQuietToLoud) {
|
|
|
|
const includeNames = name && (power_user.instruct.names || (!!selected_group && power_user.instruct.names_force_groups)) && !(isQuiet && !isQuietToLoud);
|
|
|
|
|
|
|
|
function getSequence() {
|
|
|
|
// User impersonation prompt
|
|
|
|
if (isImpersonate) {
|
|
|
|
return power_user.instruct.input_sequence;
|
|
|
|
}
|
|
|
|
|
2024-04-04 21:27:08 +02:00
|
|
|
// Neutral / system / quiet prompt
|
|
|
|
// Use a special quiet instruct sequence if defined, or assistant's output sequence otherwise
|
2024-03-28 20:51:02 +01:00
|
|
|
if (isQuiet && !isQuietToLoud) {
|
2024-04-04 21:27:08 +02:00
|
|
|
return power_user.instruct.last_system_sequence || power_user.instruct.output_sequence;
|
2024-03-28 20:51:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quiet in-character prompt
|
|
|
|
if (isQuiet && isQuietToLoud) {
|
|
|
|
return power_user.instruct.last_output_sequence || power_user.instruct.output_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default AI response
|
|
|
|
return power_user.instruct.last_output_sequence || power_user.instruct.output_sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
let sequence = getSequence() || '';
|
2023-08-20 22:29:43 +02:00
|
|
|
|
|
|
|
if (power_user.instruct.macro) {
|
|
|
|
sequence = substituteParams(sequence, name1, name2);
|
2024-03-12 22:26:45 +01:00
|
|
|
sequence = sequence.replace(/{{name}}/gi, name || 'System');
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const separator = power_user.instruct.wrap ? '\n' : '';
|
|
|
|
let text = includeNames ? (separator + sequence + separator + `${name}:`) : (separator + sequence);
|
|
|
|
|
2024-03-30 14:54:42 +01:00
|
|
|
// Quiet prompt already has a newline at the end
|
|
|
|
if (isQuiet && separator) {
|
|
|
|
text = text.slice(separator.length);
|
|
|
|
}
|
|
|
|
|
2023-08-20 22:29:43 +02:00
|
|
|
if (!isImpersonate && promptBias) {
|
2024-03-23 16:51:53 +01:00
|
|
|
text += (includeNames ? promptBias : (separator + promptBias.trimStart()));
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 15:02:24 +02:00
|
|
|
return (power_user.instruct.wrap ? text.trimEnd() : text) + (includeNames ? '' : separator);
|
2023-08-20 22:29:43 +02:00
|
|
|
}
|
2023-08-20 23:56:53 +02:00
|
|
|
|
2023-09-28 20:59:03 +02:00
|
|
|
/**
|
|
|
|
* Select context template matching instruct preset.
|
|
|
|
* @param {string} name Preset name.
|
|
|
|
*/
|
|
|
|
function selectMatchingContextTemplate(name) {
|
|
|
|
let foundMatch = false;
|
|
|
|
for (const context_preset of context_presets) {
|
|
|
|
// If context template matches the instruct preset
|
|
|
|
if (context_preset.name === name) {
|
|
|
|
foundMatch = true;
|
|
|
|
selectContextPreset(context_preset.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundMatch) {
|
|
|
|
// If no match was found, select default context preset
|
|
|
|
selectContextPreset(power_user.default_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 15:37:05 +01:00
|
|
|
/**
|
|
|
|
* Replaces instruct mode macros in the given input string.
|
|
|
|
* @param {string} input Input string.
|
2024-04-08 20:05:59 +02:00
|
|
|
* @param {Object<string, *>} env - Map of macro names to the values they'll be substituted with. If the param
|
2024-04-09 09:20:59 +02:00
|
|
|
* values are functions, those functions will be called and their return values are used.
|
2023-11-26 15:37:05 +01:00
|
|
|
* @returns {string} String with macros replaced.
|
|
|
|
*/
|
2024-04-08 20:05:59 +02:00
|
|
|
export function replaceInstructMacros(input, env) {
|
2023-11-26 15:37:05 +01:00
|
|
|
if (!input) {
|
|
|
|
return '';
|
|
|
|
}
|
2024-04-04 21:27:08 +02:00
|
|
|
const instructMacros = {
|
2024-04-08 20:05:59 +02:00
|
|
|
'systemPrompt': (power_user.prefer_character_prompt && env.charPrompt ? env.charPrompt : power_user.instruct.system_prompt),
|
2024-04-04 21:27:08 +02:00
|
|
|
'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,
|
|
|
|
'instructUserSuffix': power_user.instruct.input_suffix,
|
|
|
|
'instructOutput|instructAssistantPrefix': power_user.instruct.output_sequence,
|
|
|
|
'instructSeparator|instructAssistantSuffix': power_user.instruct.output_suffix,
|
|
|
|
'instructSystemPrefix': power_user.instruct.system_sequence,
|
|
|
|
'instructSystemSuffix': power_user.instruct.system_suffix,
|
|
|
|
'instructFirstOutput|instructFirstAssistantPrefix': power_user.instruct.first_output_sequence || power_user.instruct.output_sequence,
|
|
|
|
'instructLastOutput|instructLastAssistantPrefix': power_user.instruct.last_output_sequence || power_user.instruct.output_sequence,
|
|
|
|
'instructStop': power_user.instruct.stop_sequence,
|
|
|
|
'instructUserFiller': power_user.instruct.user_alignment_message,
|
|
|
|
'instructSystemInstructionPrefix': power_user.instruct.last_system_sequence,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const [placeholder, value] of Object.entries(instructMacros)) {
|
|
|
|
const regex = new RegExp(`{{(${placeholder})}}`, 'gi');
|
|
|
|
input = input.replace(regex, power_user.instruct.enabled ? value : '');
|
|
|
|
}
|
2023-11-26 15:37:05 +01:00
|
|
|
|
|
|
|
input = input.replace(/{{exampleSeparator}}/gi, power_user.context.example_separator);
|
|
|
|
input = input.replace(/{{chatStart}}/gi, power_user.context.chat_start);
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2024-03-27 23:52:20 +01:00
|
|
|
$('#instruct_system_same_as_user').on('input', function () {
|
|
|
|
const state = !!$(this).prop('checked');
|
|
|
|
$('#instruct_system_sequence').prop('disabled', state);
|
|
|
|
$('#instruct_system_suffix').prop('disabled', state);
|
|
|
|
});
|
|
|
|
|
2023-08-23 20:17:45 +02:00
|
|
|
$('#instruct_enabled').on('change', function () {
|
2023-12-21 14:12:30 +01:00
|
|
|
if (!power_user.instruct.bind_to_context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-23 20:17:45 +02:00
|
|
|
// When instruct mode gets enabled, select context template matching selected instruct preset
|
|
|
|
if (power_user.instruct.enabled) {
|
2023-09-28 20:59:03 +02:00
|
|
|
selectMatchingContextTemplate(power_user.instruct.preset);
|
2023-08-23 22:22:52 +02:00
|
|
|
} else {
|
2024-03-27 23:52:20 +01:00
|
|
|
// When instruct mode gets disabled, select default context preset
|
2023-08-26 12:09:47 +02:00
|
|
|
selectContextPreset(power_user.default_context);
|
2023-08-23 20:17:45 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
$('#instruct_presets').on('change', function () {
|
2023-09-28 20:59:03 +02:00
|
|
|
const name = String($(this).find(':selected').val());
|
2023-08-20 23:56:53 +02:00
|
|
|
const preset = instruct_presets.find(x => x.name === name);
|
|
|
|
|
|
|
|
if (!preset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-27 22:40:10 +01:00
|
|
|
migrateInstructModeSettings(preset);
|
|
|
|
|
2023-08-22 13:30:49 +02:00
|
|
|
power_user.instruct.preset = String(name);
|
2023-08-20 23:56:53 +02:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-21 14:12:30 +01:00
|
|
|
if (power_user.instruct.bind_to_context) {
|
|
|
|
// Select matching context template
|
|
|
|
selectMatchingContextTemplate(name);
|
|
|
|
}
|
2023-08-23 20:17:45 +02:00
|
|
|
|
2023-08-20 23:56:53 +02:00
|
|
|
highlightDefaultPreset();
|
|
|
|
});
|
|
|
|
});
|