Merge pull request #1574 from artisticMink/feature/before-combine-event
Allow extensions to alter the context order.
This commit is contained in:
commit
b0a4341571
|
@ -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) {
|
||||
this.on(event, function g () {
|
||||
this.removeListener(event, g);
|
||||
|
|
|
@ -313,6 +313,7 @@ export const event_types = {
|
|||
FORCE_SET_BACKGROUND: 'force_set_background',
|
||||
CHAT_DELETED: 'chat_deleted',
|
||||
GROUP_CHAT_DELETED: 'group_chat_deleted',
|
||||
GENERATE_BEFORE_COMBINE_PROMPTS: 'generate_before_combine_prompts',
|
||||
};
|
||||
|
||||
export const eventSource = new EventEmitter();
|
||||
|
@ -3621,30 +3622,57 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
|
|||
generatedPromptCache = cleanupPromptCache(generatedPromptCache);
|
||||
}
|
||||
|
||||
// Right now, everything is suffixed with a newline
|
||||
mesSendString = finalMesSend.map((e) => `${e.extensionPrompts.join('')}${e.message}`).join('');
|
||||
// Flattens the multiple prompt objects to a string.
|
||||
const combine = () => {
|
||||
// Right now, everything is suffixed with a newline
|
||||
mesSendString = finalMesSend.map((e) => `${e.extensionPrompts.join('')}${e.message}`).join('');
|
||||
|
||||
// add chat preamble
|
||||
mesSendString = addChatsPreamble(mesSendString);
|
||||
// add a custom dingus (if defined)
|
||||
mesSendString = addChatsSeparator(mesSendString);
|
||||
|
||||
// add a custom dingus (if defined)
|
||||
mesSendString = addChatsSeparator(mesSendString);
|
||||
// add chat preamble
|
||||
mesSendString = addChatsPreamble(mesSendString);
|
||||
|
||||
let combinedPrompt =
|
||||
beforeScenarioAnchor +
|
||||
storyString +
|
||||
afterScenarioAnchor +
|
||||
mesExmString +
|
||||
mesSendString +
|
||||
generatedPromptCache;
|
||||
let combinedPrompt = beforeScenarioAnchor +
|
||||
storyString +
|
||||
afterScenarioAnchor +
|
||||
mesExmString +
|
||||
mesSendString +
|
||||
generatedPromptCache;
|
||||
|
||||
combinedPrompt = combinedPrompt.replace(/\r/gm, '');
|
||||
combinedPrompt = combinedPrompt.replace(/\r/gm, '');
|
||||
|
||||
if (power_user.collapse_newlines) {
|
||||
combinedPrompt = collapseNewlines(combinedPrompt);
|
||||
}
|
||||
if (power_user.collapse_newlines) {
|
||||
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
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
substituteParams,
|
||||
} from '../script.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 EventSourceStream from './sse-stream.js';
|
||||
import {
|
||||
|
@ -437,6 +437,10 @@ export function getNovelGenerationData(finalPrompt, settings, maxLength, isImper
|
|||
BIAS_CACHE.set(BIAS_KEY, logitBias);
|
||||
}
|
||||
|
||||
if (power_user.console_log_prompts) {
|
||||
console.log(finalPrompt);
|
||||
}
|
||||
|
||||
return {
|
||||
'input': finalPrompt,
|
||||
'model': nai_settings.model_novel,
|
||||
|
|
Loading…
Reference in New Issue