mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Merge pull request #1574 from artisticMink/feature/before-combine-event
Allow extensions to alter the context order.
This commit is contained in:
@ -69,6 +69,27 @@ EventEmitter.prototype.emit = async function (event) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.emitAndWait = function (event) {
|
||||||
|
console.debug('Event emitted: ' + event);
|
||||||
|
|
||||||
|
var i, listeners, length, args = [].slice.call(arguments, 1);
|
||||||
|
|
||||||
|
if (typeof this.events[event] === 'object') {
|
||||||
|
listeners = this.events[event].slice();
|
||||||
|
length = listeners.length;
|
||||||
|
|
||||||
|
for (i = 0; i < length; i++) {
|
||||||
|
try {
|
||||||
|
listeners[i].apply(this, args);
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
console.trace('Error in event listener');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
EventEmitter.prototype.once = function (event, listener) {
|
EventEmitter.prototype.once = function (event, listener) {
|
||||||
this.on(event, function g () {
|
this.on(event, function g () {
|
||||||
this.removeListener(event, g);
|
this.removeListener(event, g);
|
||||||
|
@ -313,6 +313,7 @@ export const event_types = {
|
|||||||
FORCE_SET_BACKGROUND: 'force_set_background',
|
FORCE_SET_BACKGROUND: 'force_set_background',
|
||||||
CHAT_DELETED: 'chat_deleted',
|
CHAT_DELETED: 'chat_deleted',
|
||||||
GROUP_CHAT_DELETED: 'group_chat_deleted',
|
GROUP_CHAT_DELETED: 'group_chat_deleted',
|
||||||
|
GENERATE_BEFORE_COMBINE_PROMPTS: 'generate_before_combine_prompts',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const eventSource = new EventEmitter();
|
export const eventSource = new EventEmitter();
|
||||||
@ -3621,30 +3622,57 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
|
|||||||
generatedPromptCache = cleanupPromptCache(generatedPromptCache);
|
generatedPromptCache = cleanupPromptCache(generatedPromptCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Right now, everything is suffixed with a newline
|
// Flattens the multiple prompt objects to a string.
|
||||||
mesSendString = finalMesSend.map((e) => `${e.extensionPrompts.join('')}${e.message}`).join('');
|
const combine = () => {
|
||||||
|
// Right now, everything is suffixed with a newline
|
||||||
|
mesSendString = finalMesSend.map((e) => `${e.extensionPrompts.join('')}${e.message}`).join('');
|
||||||
|
|
||||||
// add chat preamble
|
// add a custom dingus (if defined)
|
||||||
mesSendString = addChatsPreamble(mesSendString);
|
mesSendString = addChatsSeparator(mesSendString);
|
||||||
|
|
||||||
// add a custom dingus (if defined)
|
// add chat preamble
|
||||||
mesSendString = addChatsSeparator(mesSendString);
|
mesSendString = addChatsPreamble(mesSendString);
|
||||||
|
|
||||||
let combinedPrompt =
|
let combinedPrompt = beforeScenarioAnchor +
|
||||||
beforeScenarioAnchor +
|
storyString +
|
||||||
storyString +
|
afterScenarioAnchor +
|
||||||
afterScenarioAnchor +
|
mesExmString +
|
||||||
mesExmString +
|
mesSendString +
|
||||||
mesSendString +
|
generatedPromptCache;
|
||||||
generatedPromptCache;
|
|
||||||
|
|
||||||
combinedPrompt = combinedPrompt.replace(/\r/gm, '');
|
combinedPrompt = combinedPrompt.replace(/\r/gm, '');
|
||||||
|
|
||||||
if (power_user.collapse_newlines) {
|
if (power_user.collapse_newlines) {
|
||||||
combinedPrompt = collapseNewlines(combinedPrompt);
|
combinedPrompt = collapseNewlines(combinedPrompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
return combinedPrompt;
|
return combinedPrompt;
|
||||||
|
};
|
||||||
|
|
||||||
|
let data = {
|
||||||
|
api: main_api,
|
||||||
|
combinedPrompt: null,
|
||||||
|
description,
|
||||||
|
personality,
|
||||||
|
persona,
|
||||||
|
scenario,
|
||||||
|
char: name2,
|
||||||
|
user: name1,
|
||||||
|
beforeScenarioAnchor,
|
||||||
|
afterScenarioAnchor,
|
||||||
|
mesExmString,
|
||||||
|
finalMesSend,
|
||||||
|
generatedPromptCache,
|
||||||
|
main: system,
|
||||||
|
jailbreak,
|
||||||
|
naiPreamble: nai_settings.preamble,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Before returning the combined prompt, give available context related information to all subscribers.
|
||||||
|
eventSource.emitAndWait(event_types.GENERATE_BEFORE_COMBINE_PROMPTS, data);
|
||||||
|
|
||||||
|
// If one or multiple subscribers return a value, forfeit the responsibillity of flattening the context.
|
||||||
|
return !data.combinedPrompt ? combine() : data.combinedPrompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the negative prompt first since it has the unmodified mesSend array
|
// Get the negative prompt first since it has the unmodified mesSend array
|
||||||
|
@ -8,7 +8,7 @@ import {
|
|||||||
substituteParams,
|
substituteParams,
|
||||||
} from '../script.js';
|
} from '../script.js';
|
||||||
import { getCfgPrompt } from './cfg-scale.js';
|
import { getCfgPrompt } from './cfg-scale.js';
|
||||||
import { MAX_CONTEXT_DEFAULT, MAX_RESPONSE_DEFAULT } from './power-user.js';
|
import { MAX_CONTEXT_DEFAULT, MAX_RESPONSE_DEFAULT, power_user } from './power-user.js';
|
||||||
import { getTextTokens, tokenizers } from './tokenizers.js';
|
import { getTextTokens, tokenizers } from './tokenizers.js';
|
||||||
import EventSourceStream from './sse-stream.js';
|
import EventSourceStream from './sse-stream.js';
|
||||||
import {
|
import {
|
||||||
@ -437,6 +437,10 @@ export function getNovelGenerationData(finalPrompt, settings, maxLength, isImper
|
|||||||
BIAS_CACHE.set(BIAS_KEY, logitBias);
|
BIAS_CACHE.set(BIAS_KEY, logitBias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (power_user.console_log_prompts) {
|
||||||
|
console.log(finalPrompt);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'input': finalPrompt,
|
'input': finalPrompt,
|
||||||
'model': nai_settings.model_novel,
|
'model': nai_settings.model_novel,
|
||||||
|
Reference in New Issue
Block a user