Added stop string cleanup, better stopping string param

This commit is contained in:
bmen25124
2025-03-26 22:35:10 +03:00
parent f4eb32c71c
commit c5f251c6e3
2 changed files with 81 additions and 19 deletions

View File

@ -243,9 +243,12 @@ export function autoSelectInstructPreset(modelId) {
/**
* Converts instruct mode sequences to an array of stopping strings.
* @param {{customInstruct?: InstructSettings, useStopString?: boolean}} options
* @returns {string[]} Array of instruct mode stopping strings.
*/
export function getInstructStoppingSequences() {
export function getInstructStoppingSequences({ customInstruct = null, useStopString = false } = {}) {
const instruct = structuredClone(customInstruct ?? power_user.instruct);
/**
* Adds instruct mode sequence to the result array.
* @param {string} sequence Sequence string.
@ -254,7 +257,7 @@ export function getInstructStoppingSequences() {
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;
const wrap = (s) => 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
@ -262,7 +265,7 @@ export function getInstructStoppingSequences() {
if (sequence.trim().length > 0) {
const wrappedSequence = wrap(sequence);
// Need to respect "insert macro" setting
const stopString = power_user.instruct.macro ? substituteParams(wrappedSequence) : wrappedSequence;
const stopString = instruct.macro ? substituteParams(wrappedSequence) : wrappedSequence;
result.push(stopString);
}
}
@ -270,14 +273,15 @@ export function getInstructStoppingSequences() {
const result = [];
if (power_user.instruct.enabled) {
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') || '';
const last_system_sequence = power_user.instruct.last_system_sequence?.replace(/{{name}}/gi, 'System') || '';
// Since preset's don't have "enabled", we assume it's always enabled
if (customInstruct ?? instruct.enabled) {
const stop_sequence = instruct.stop_sequence || '';
const input_sequence = instruct.input_sequence?.replace(/{{name}}/gi, name1) || '';
const output_sequence = instruct.output_sequence?.replace(/{{name}}/gi, name2) || '';
const first_output_sequence = instruct.first_output_sequence?.replace(/{{name}}/gi, name2) || '';
const last_output_sequence = instruct.last_output_sequence?.replace(/{{name}}/gi, name2) || '';
const system_sequence = instruct.system_sequence?.replace(/{{name}}/gi, 'System') || '';
const last_system_sequence = instruct.last_system_sequence?.replace(/{{name}}/gi, 'System') || '';
const combined_sequence = [
stop_sequence,
@ -292,7 +296,7 @@ export function getInstructStoppingSequences() {
combined_sequence.split('\n').filter((line, index, self) => self.indexOf(line) === index).forEach(addInstructSequence);
}
if (power_user.context.use_stop_strings) {
if (useStopString ?? power_user.context.use_stop_strings) {
if (power_user.context.chat_start) {
result.push(`\n${substituteParams(power_user.context.chat_start)}`);
}