cfg: Adjust max context size based on injected prompt Close #2424
| @@ -3792,6 +3792,23 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 3792 | } | 3792 | } |
| 3793 | } | 3793 | } |
| 3794 | 3794 | ||
| 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 | |||
| 3795 | console.log(`Core/all messages: ${coreChat.length}/${chat.length}`); | 3812 | console.log(`Core/all messages: ${coreChat.length}/${chat.length}`); |
| 3796 | 3813 | ||
| 3797 | // kingbri MARK: - Make sure the prompt bias isn't the same as the user bias | 3814 | // 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 | |||
| 4299 | setPromptString(); | 4316 | setPromptString(); |
| 4300 | } | 4317 | } |
| 4301 | 4318 | ||
| 4302 | // Fetches the combined prompt for both negative and positive prompts | ||
| 4303 | const cfgGuidanceScale = getGuidanceScale(); | ||
| 4304 | const useCfgPrompt = cfgGuidanceScale && cfgGuidanceScale.value !== 1; | ||
| 4305 | |||
| 4306 | // For prompt bit itemization | 4319 | // For prompt bit itemization |
| 4307 | let mesSendString = ''; | 4320 | let mesSendString = ''; |
| 4308 | 4321 | ||
| @@ -451,8 +451,14 @@ function getCustomSeparator() { | |||
| 451 | } | 451 | } |
| 452 | } | 452 | } |
| 453 | 453 | ||
| 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) { | ||
| 456 | let splitCfgPrompt = []; | 462 | let splitCfgPrompt = []; |
| 457 | 463 | ||
| 458 | const cfgPromptCombine = chat_metadata[metadataKeys.prompt_combine] ?? []; | 464 | const cfgPromptCombine = chat_metadata[metadataKeys.prompt_combine] ?? []; |
| @@ -484,7 +490,7 @@ export function getCfgPrompt(guidanceScale, isNegative) { | |||
| 484 | const customSeparator = getCustomSeparator(); | 490 | const customSeparator = getCustomSeparator(); |
| 485 | const combinedCfgPrompt = splitCfgPrompt.filter((e) => e.length > 0).join(customSeparator); | 491 | const combinedCfgPrompt = splitCfgPrompt.filter((e) => e.length > 0).join(customSeparator); |
| 486 | const insertionDepth = chat_metadata[metadataKeys.prompt_insertion_depth] ?? 1; | 492 | const insertionDepth = chat_metadata[metadataKeys.prompt_insertion_depth] ?? 1; |
| 487 | console.log(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedCfgPrompt}`); | 493 | !quiet && console.log(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedCfgPrompt}`); |
| 488 | 494 | ||
| 489 | return { | 495 | return { |
| 490 | value: combinedCfgPrompt, | 496 | value: combinedCfgPrompt, |