mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
World Info: Add optimizations and fixes
Use a switch instead of if/else chain, fix un-needed addition when determining token counts, and remove newline spacing for AN WI formatting. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@ -822,11 +822,12 @@ async function checkWorldInfo(chat, maxContext) {
|
|||||||
const newEntries = [...activatedNow]
|
const newEntries = [...activatedNow]
|
||||||
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
|
.sort((a, b) => sortedEntries.indexOf(a) - sortedEntries.indexOf(b));
|
||||||
let newContent = "";
|
let newContent = "";
|
||||||
|
const textToScanTokens = getTokenCount(textToScan);
|
||||||
|
|
||||||
for (const entry of newEntries) {
|
for (const entry of newEntries) {
|
||||||
newContent += `${substituteParams(entry.content)}\n`;
|
newContent += `${substituteParams(entry.content)}\n`;
|
||||||
|
|
||||||
if (getTokenCount(textToScan + newContent) >= budget) {
|
if (textToScanTokens + getTokenCount(newContent) >= budget) {
|
||||||
console.debug(`WI budget reached, stopping`);
|
console.debug(`WI budget reached, stopping`);
|
||||||
needsToScan = false;
|
needsToScan = false;
|
||||||
break;
|
break;
|
||||||
@ -847,20 +848,27 @@ async function checkWorldInfo(chat, maxContext) {
|
|||||||
const ANBottomInjection = [];
|
const ANBottomInjection = [];
|
||||||
|
|
||||||
[...allActivatedEntries].sort(sortFn).forEach((entry) => {
|
[...allActivatedEntries].sort(sortFn).forEach((entry) => {
|
||||||
if (entry.position === world_info_position.before) {
|
switch (entry.position) {
|
||||||
worldInfoBefore = `${substituteParams(entry.content)}\n${worldInfoBefore}`;
|
case world_info_position.before:
|
||||||
} else if (entry.position === world_info_position.after) {
|
worldInfoBefore = `${substituteParams(entry.content)}\n${worldInfoBefore}`;
|
||||||
worldInfoAfter = `${substituteParams(entry.content)}\n${worldInfoAfter}`;
|
break;
|
||||||
} else if (entry.position === world_info_position.ANTop) {
|
case world_info_position.after:
|
||||||
ANTopInjection.push(entry.content);
|
worldInfoAfter = `${substituteParams(entry.content)}\n${worldInfoAfter}`;
|
||||||
} else if (entry.position === world_info_position.ANBottom) {
|
break;
|
||||||
ANBottomInjection.push(entry.content);
|
case world_info_position.ANTop:
|
||||||
|
ANTopInjection.push(entry.content);
|
||||||
|
break;
|
||||||
|
case world_info_position.ANBottom:
|
||||||
|
ANBottomInjection.push(entry.content);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (shouldWIAddPrompt) {
|
if (shouldWIAddPrompt) {
|
||||||
const originalAN = context.extensionPrompts['2_floating_prompt'].value;
|
const originalAN = context.extensionPrompts['2_floating_prompt'].value;
|
||||||
const ANWithWI = `\n${ANTopInjection.join("\n")} \n${originalAN} \n${ANBottomInjection.reverse().join("\n")}`
|
const ANWithWI = `\n${ANTopInjection.join("\n")}\n${originalAN}\n${ANBottomInjection.reverse().join("\n")}`
|
||||||
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
context.setExtensionPrompt('2_floating_prompt', ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user