cfg: Adjust max context size based on injected prompt Close #2424

c4f119ebf9f2e7211f8524557baf24639084c237

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +26 -7Ignore whitespace
public/script.js+17 -4
@@ -3792,6 +3792,23 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
37923792 }
37933793 }
37943794
3795+ // Fetches the combined prompt for both negative and positive prompts
3796+ const cfgGuidanceScale = getGuidanceScale();
3797+ const useCfgPrompt = cfgGuidanceScale && cfgGuidanceScale.value !== 1;
3798+
3799+ // Adjust max context based on CFG prompt to prevent overfitting
3800+ if (useCfgPrompt) {
3801+ const negativePrompt = getCfgPrompt(cfgGuidanceScale, true, true)?.value || '';
3802+ const positivePrompt = getCfgPrompt(cfgGuidanceScale, false, true)?.value || '';
3803+ if (negativePrompt || positivePrompt) {
3804+ const previousMaxContext = this_max_context;
3805+ const [negativePromptTokenCount, positivePromptTokenCount] = await Promise.all([getTokenCountAsync(negativePrompt), getTokenCountAsync(positivePrompt)]);
3806+ const decrement = Math.max(negativePromptTokenCount, positivePromptTokenCount);
3807+ this_max_context -= decrement;
3808+ console.log(`Max context reduced by ${decrement} tokens of CFG prompt (${previousMaxContext} -> ${this_max_context})`);
3809+ }
3810+ }
3811+
37953812 console.log(`Core/all messages: ${coreChat.length}/${chat.length}`);
37963813
37973814 // kingbri MARK: - Make sure the prompt bias isn't the same as the user bias
@@ -4299,10 +4316,6 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
42994316 setPromptString();
43004317 }
43014318
4302- // Fetches the combined prompt for both negative and positive prompts
4303- const cfgGuidanceScale = getGuidanceScale();
4304- const useCfgPrompt = cfgGuidanceScale && cfgGuidanceScale.value !== 1;
4305-
43064319 // For prompt bit itemization
43074320 let mesSendString = '';
43084321
public/scripts/cfg-scale.js+9 -3
@@ -451,8 +451,14 @@ function getCustomSeparator() {
451451 }
452452}
453453
454-// Gets the CFG prompt
454+/**
455-export function getCfgPrompt(guidanceScale, isNegative) {
455+ * Gets the CFG prompt based on the guidance scale.
456+ * @param {{type: number, value: number}} guidanceScale The CFG guidance scale
457+ * @param {boolean} isNegative Whether to get the negative prompt
458+ * @param {boolean} quiet Whether to suppress console output
459+ * @returns {{value: string, depth: number}} The CFG prompt and insertion depth
460+ */
461+export function getCfgPrompt(guidanceScale, isNegative, quiet = false) {
456462 let splitCfgPrompt = [];
457463
458464 const cfgPromptCombine = chat_metadata[metadataKeys.prompt_combine] ?? [];
@@ -484,7 +490,7 @@ export function getCfgPrompt(guidanceScale, isNegative) {
484490 const customSeparator = getCustomSeparator();
485491 const combinedCfgPrompt = splitCfgPrompt.filter((e) => e.length > 0).join(customSeparator);
486492 const insertionDepth = chat_metadata[metadataKeys.prompt_insertion_depth] ?? 1;
487493 !quiet && console.log(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedCfgPrompt}`);
488494
489495 return {
490496 value: combinedCfgPrompt,