World Info: Add insertion order at depth (#1174)

Depth previously injected entries randomly. This is not ideal for
world info architectures that rely on insertion order to function
properly.

Redo depth injection to have its own parameter and redo how it's
handled in generate.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
Brian Dashore 2023-09-24 07:41:56 -04:00 committed by GitHub
parent 5421925d6c
commit 2411006fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 16 deletions

View File

@ -4004,6 +4004,10 @@
</small>
</div>
<div class="world_entry_form_control wi-enter-footer-text flex-container flexNoGap ">
<label for="depth" data-i18n="Depth:">Depth:</label>
<input class="text_pole wideMax100px margin0" type="number" name="depth" placeholder="" min="0" max="10000" />
</div>
<div class="world_entry_form_control wi-enter-footer-text flex-container flexNoGap ">
<label for="order" data-i18n="Order:">Order:</label>
@ -4017,11 +4021,6 @@
<input class="text_pole wideMax100px margin0" type="number" name="probability" placeholder="" min="0" max="100" />
</div>
<div class="flex-container flexFlowColumn flexNoGap wi-enter-footer-text ">
</div>
<input class="menu_button delete_entry_button" type="submit" data-i18n="Delete Entry" value="Delete Entry" />
</div>
<div class="wide100p">

View File

@ -2002,6 +2002,7 @@ function addPersonaDescriptionExtensionPrompt() {
const ANWithDesc = power_user.persona_description_position === persona_description_positions.TOP_AN
? `${power_user.persona_description}\n${originalAN}`
: `${originalAN}\n${power_user.persona_description}`;
setExtensionPrompt(NOTE_MODULE_NAME, ANWithDesc, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
}
}
@ -2564,7 +2565,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
// Set non-WI AN
setFloatingPrompt();
// Add WI to prompt (and also inject WI to AN value via hijack)
let { worldInfoString, worldInfoBefore, worldInfoAfter } = await getWorldInfoPrompt(chat2, this_max_context);
let { worldInfoString, worldInfoBefore, worldInfoAfter, worldInfoDepth } = await getWorldInfoPrompt(chat2, this_max_context);
// Add persona description to prompt
addPersonaDescriptionExtensionPrompt();
// Call combined AN into Generate
@ -2589,6 +2590,12 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
const storyString = renderStoryString(storyStringParams);
// Add all depth WI entries to prompt
worldInfoDepth.forEach((e) => {
const joinedEntries = e.entries.join("\n");
setExtensionPrompt(`customDepthWI-${e.depth}`, joinedEntries, 1, e.depth)
});
if (main_api === 'openai') {
message_already_generated = '';
setOpenAIMessages(coreChat);

View File

@ -85,7 +85,12 @@ async function getWorldInfoPrompt(chat2, maxContext) {
worldInfoAfter = activatedWorldInfo.worldInfoAfter;
worldInfoString = worldInfoBefore + worldInfoAfter;
return { worldInfoString, worldInfoBefore, worldInfoAfter };
return {
worldInfoString,
worldInfoBefore,
worldInfoAfter,
worldInfoDepth: activatedWorldInfo.WIDepthEntries
};
}
function setWorldInfoSettings(settings, data) {
@ -675,6 +680,24 @@ function getWorldEntry(name, data, entry) {
entry.probability = null;
}
// depth
const depthInput = template.find('input[name="depth"]');
depthInput.data("uid", entry.uid);
depthInput.on("input", function() {
const uid = $(this).data("uid");
const value = Number($(this).val());
data.entries[uid].depth = !isNaN(value) ? value : 0;
setOriginalDataValue(data, uid, "depth", data.entries[uid].depth);
saveWorldInfo(name, data);
});
depthInput.val(entry.depth ?? 4).trigger("input");
// Hide by default unless depth is specified
if (entry.position === 4) {
depthInput.parent().hide();
}
const probabilityInput = template.find('input[name="probability"]');
probabilityInput.data("uid", entry.uid);
probabilityInput.on("input", function () {
@ -740,8 +763,10 @@ function getWorldEntry(name, data, entry) {
const value = Number($(this).val());
data.entries[uid].position = !isNaN(value) ? value : 0;
if (value === 4) {
template.find('label[for="order"').text('Depth:')
} else { template.find('label[for="order"').text('Order:') }
depthInput.parent().show();
} else {
depthInput.parent().hide();
}
// Spec v2 only supports before_char and after_char
setOriginalDataValue(data, uid, "position", data.entries[uid].position == 0 ? 'before_char' : 'after_char');
// Write the original value as extensions field
@ -1233,6 +1258,7 @@ async function checkWorldInfo(chat, maxContext) {
const WIAfterEntries = [];
const ANTopEntries = [];
const ANBottomEntries = [];
const WIDepthEntries = [];
// Appends from insertion order 999 to 1. Use unshift for this purpose
[...allActivatedEntries].sort(sortFn).forEach((entry) => {
@ -1250,12 +1276,15 @@ async function checkWorldInfo(chat, maxContext) {
ANBottomEntries.unshift(entry.content);
break;
case world_info_position.atDepth:
//inserted one by one, unrelated to any array of items
//must have a unique value for 'key' argument
//uses the order input to specify depth
var randomNumber = Math.floor(Math.random() * 99999) + 1;
context.setExtensionPrompt(`customDepthWI-${entry.keywords}-${entry.uid}-${randomNumber}`, entry.content, 1, entry.order);
break;
const existingDepthIndex = WIDepthEntries.findIndex((e) => e.depth === entry.depth ?? 4);
if (existingDepthIndex !== -1) {
WIDepthEntries[existingDepthIndex].entries.unshift(entry.content);
} else {
WIDepthEntries.push({
depth: entry.depth,
entries: [entry.content]
});
}
default:
break;
}
@ -1270,7 +1299,7 @@ async function checkWorldInfo(chat, maxContext) {
context.setExtensionPrompt(NOTE_MODULE_NAME, ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
}
return { worldInfoBefore, worldInfoAfter };
return { worldInfoBefore, worldInfoAfter, WIDepthEntries };
}
function matchKeys(haystack, needle) {