Merge pull request #1017 from StefanDanielSchwarz/improved-instruct-mode-sequences

improved instruct mode sequences and UI
This commit is contained in:
Cohee
2023-08-26 01:03:38 +03:00
committed by GitHub
17 changed files with 214 additions and 104 deletions

View File

@@ -6,6 +6,7 @@ import {
power_user,
context_presets,
} from "./power-user.js";
import { resetScrollHeight } from "./utils.js";
/**
* @type {any[]} Instruct mode presets.
@@ -16,7 +17,8 @@ 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", property: "system_sequence", 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_separator_sequence", property: "separator_sequence", isCheckbox: false },
{ id: "instruct_input_sequence", property: "input_sequence", isCheckbox: false },
{ id: "instruct_output_sequence", property: "output_sequence", isCheckbox: false },
@@ -24,6 +26,7 @@ const controls = [
{ 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 },
];
@@ -53,6 +56,9 @@ export function loadInstructMode(data) {
$element.on('input', function () {
power_user.instruct[control.property] = control.isCheckbox ? !!$(this).prop('checked') : $(this).val();
saveSettingsDebounced();
if (!control.isCheckbox) {
resetScrollHeight($element);
}
});
});
@@ -200,9 +206,10 @@ export function getInstructStoppingSequences() {
if (power_user.instruct.enabled) {
const input_sequence = power_user.instruct.input_sequence;
const output_sequence = power_user.instruct.output_sequence;
const first_output_sequence = power_user.instruct.first_output_sequence;
const last_output_sequence = power_user.instruct.last_output_sequence;
const combined_sequence = `${input_sequence}\n${output_sequence}\n${last_output_sequence}`;
const combined_sequence = `${input_sequence}\n${output_sequence}\n${first_output_sequence}\n${last_output_sequence}`;
combined_sequence.split('\n').filter((line, index, self) => self.indexOf(line) === index).forEach(addInstructSequence);
}
@@ -210,6 +217,11 @@ export function getInstructStoppingSequences() {
return result;
}
export const force_output_sequence = {
FIRST: 1,
LAST: 2,
}
/**
* Formats instruct mode chat message.
* @param {string} name Character name.
@@ -219,10 +231,10 @@ export function getInstructStoppingSequences() {
* @param {string} forceAvatar Force avatar string.
* @param {string} name1 User name.
* @param {string} name2 Character name.
* @param {boolean} forceLastOutputSequence Force to use last outline sequence (if configured).
* @param {boolean|number} forceOutputSequence Force to use first/last output sequence (if configured).
* @returns {string} Formatted instruct mode chat message.
*/
export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvatar, name1, name2, forceLastOutputSequence) {
export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvatar, name1, name2, forceOutputSequence) {
let includeNames = isNarrator ? false : power_user.instruct.names;
if (!isNarrator && power_user.instruct.names_force_groups && (selected_group || forceAvatar)) {
@@ -231,8 +243,12 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata
let sequence = (isUser || isNarrator) ? power_user.instruct.input_sequence : power_user.instruct.output_sequence;
if (sequence === power_user.instruct.output_sequence && forceLastOutputSequence && power_user.instruct.last_output_sequence) {
sequence = power_user.instruct.last_output_sequence;
if (forceOutputSequence && sequence === power_user.instruct.output_sequence) {
if (forceOutputSequence === force_output_sequence.FIRST && power_user.instruct.first_output_sequence) {
sequence = power_user.instruct.first_output_sequence;
} else if (forceOutputSequence === force_output_sequence.LAST && power_user.instruct.last_output_sequence) {
sequence = power_user.instruct.last_output_sequence;
}
}
if (power_user.instruct.macro) {
@@ -254,14 +270,14 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata
* @returns {string} Formatted instruct mode system prompt.
*/
export function formatInstructModeSystemPrompt(systemPrompt){
if (power_user.instruct.system_sequence) {
const separator = power_user.instruct.wrap ? '\n' : '';
const separator = power_user.instruct.wrap ? '\n' : '';
if (power_user.instruct.system_sequence.includes("{{sys}}")) {
return power_user.instruct.system_sequence.replace(/{{sys}}/gi, systemPrompt);
} else {
return power_user.instruct.system_sequence + separator + systemPrompt;
}
if (power_user.instruct.system_sequence_prefix) {
systemPrompt = power_user.instruct.system_sequence_prefix + separator + systemPrompt;
}
if (power_user.instruct.system_sequence_suffix) {
systemPrompt = systemPrompt + separator + power_user.instruct.system_sequence_suffix;
}
return systemPrompt;

View File

@@ -29,7 +29,7 @@ import {
import { registerSlashCommand } from "./slash-commands.js";
import { tokenizers } from "./tokenizers.js";
import { delay } from "./utils.js";
import { delay, resetScrollHeight } from "./utils.js";
export {
loadPowerUserSettings,
@@ -159,19 +159,21 @@ let power_user = {
default_instruct: '',
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:",
output_sequence: "### Response:",
first_output_sequence: "",
last_output_sequence: "",
system_sequence_prefix: "",
system_sequence_suffix: "",
stop_sequence: "",
separator_sequence: "",
wrap: true,
macro: true,
names: false,
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}}. Write 1 reply only.",
system_sequence: '',
stop_sequence: '',
input_sequence: '### Instruction:',
output_sequence: '### Response:',
last_output_sequence: '',
preset: 'Alpaca',
separator_sequence: '',
macro: false,
names_force_groups: true,
activation_regex: '',
activation_regex: "",
},
context: {
@@ -904,6 +906,9 @@ function loadContextSettings() {
$element.on('input', function () {
power_user.context[control.property] = control.isCheckbox ? !!$(this).prop('checked') : $(this).val();
saveSettingsDebounced();
if (!control.isCheckbox) {
resetScrollHeight($element);
}
});
});