Generate: Fix appends on continue

Continue was in a semi-working state. However, the generated prompt
by continue was a mess and didn't remove essential parts such as
character name and prompt bias. This caused duplication and bad
generations.

Now, append the prompt bias after the CFG has been appended and then
clean up the continued cache before adding it to the final prompt.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2023-08-20 17:36:35 -04:00
parent ac319dbd30
commit ad2be21531
2 changed files with 50 additions and 36 deletions

View File

@ -2720,7 +2720,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
is_send_press = true; is_send_press = true;
} }
console.log(cycleGenerationPromt)
generatedPromtCache += cycleGenerationPromt; generatedPromtCache += cycleGenerationPromt;
if (generatedPromtCache.length == 0 || type === 'continue') { if (generatedPromtCache.length == 0 || type === 'continue') {
if (main_api === 'openai') { if (main_api === 'openai') {
@ -2813,30 +2812,33 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
} }
// Add character's name // Add character's name
if (!isInstruct && force_name2 && tokens_already_generated === 0) { // Force name append on continue
if (!isInstruct && force_name2 && (tokens_already_generated === 0 || isContinue)) {
if (!lastMesString.endsWith('\n')) { if (!lastMesString.endsWith('\n')) {
lastMesString += '\n'; lastMesString += '\n';
} }
// Add a leading space to the prompt bias if applicable lastMesString += `${name2}:`;
if (!promptBias || promptBias.length === 0) { }
console.debug("No prompt bias was found.");
lastMesString += `${name2}:`;
} else if (promptBias.startsWith(' ')) {
console.debug(`A prompt bias with a leading space was found: ${promptBias}`);
lastMesString += `${name2}:${promptBias}`
} else {
console.debug(`A prompt bias was found: ${promptBias}`);
lastMesString += `${name2}: ${promptBias}`;
}
} else if (power_user.user_prompt_bias && !isImpersonate && !isInstruct) {
console.debug(`A prompt bias was found without character's name appended: ${promptBias}`);
lastMesString += substituteParams(power_user.user_prompt_bias);
}
return lastMesString; return lastMesString;
} }
// Clean up the already generated prompt for seamless addition
function cleanupPromptCache(promptCache) {
// Remove the first occurrance of character's name
if (promptCache.trimStart().startsWith(`${name2}:`)) {
promptCache = promptCache.replace(`${name2}:`, '').trimStart();
}
// Remove the first occurrance of prompt bias
if (promptCache.trimStart().startsWith(promptBias)) {
promptCache = promptCache.replace(promptBias, '');
}
return promptCache;
}
function checkPromtSize() { function checkPromtSize() {
console.debug('---checking Prompt size'); console.debug('---checking Prompt size');
setPromtString(); setPromtString();
@ -2875,25 +2877,45 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
// Fetches the combined prompt for both negative and positive prompts // Fetches the combined prompt for both negative and positive prompts
const cfgGuidanceScale = getGuidanceScale(); const cfgGuidanceScale = getGuidanceScale();
function getCombinedPrompt(isNegative) { function getCombinedPrompt(isNegative) {
// Use a negative mesSend if present let finalMesSend = [...mesSend];
let negativeMesSend = [];
let cfgPrompt = {}; let cfgPrompt = {};
if (cfgGuidanceScale && cfgGuidanceScale?.value !== 1) { if (cfgGuidanceScale && cfgGuidanceScale?.value !== 1) {
cfgPrompt = getCfgPrompt(cfgGuidanceScale, isNegative); cfgPrompt = getCfgPrompt(cfgGuidanceScale, isNegative);
} }
if (cfgPrompt && cfgPrompt?.value && cfgPrompt?.depth !== 0) { if (cfgPrompt && cfgPrompt?.value) {
const cfgPromptValue = `${cfgPrompt.value}\n` if (cfgPrompt?.depth === 0) {
// TODO: kingbri: use the insertion depth method instead of splicing finalMesSend[finalMesSend.length - 1] +=
if (isNegative) { /\s/.test(finalMesSend[finalMesSend.length - 1].slice(-1))
negativeMesSend = [...mesSend]; ? cfgPrompt.value
negativeMesSend.splice(mesSend.length - cfgPrompt.depth, 0, cfgPromptValue); : ` ${cfgPrompt.value}`;
} else { } else {
mesSend.splice(mesSend.length - cfgPrompt.depth, 0, cfgPromptValue); // TODO: Switch from splice method to insertion depth method
finalMesSend.splice(mesSend.length - cfgPrompt.depth, 0, `${cfgPrompt.value}\n`);
} }
} }
let mesSendString = isNegative ? negativeMesSend.join('') : mesSend.join(''); // Add prompt bias after everything else
// Always run with continue
if (!isInstruct && !isImpersonate && (tokens_already_generated === 0 || isContinue)) {
const trimmedBias = promptBias.trimStart();
finalMesSend[finalMesSend.length - 1] +=
/\s/.test(finalMesSend[finalMesSend.length - 1].slice(-1))
? trimmedBias
: ` ${trimmedBias}`;
}
// Prune from prompt cache if it exists
if (generatedPromtCache.length !== 0) {
generatedPromtCache = cleanupPromptCache(generatedPromtCache);
}
// Override for prompt bits data
if (!isNegative) {
mesSend = finalMesSend;
}
let mesSendString = finalMesSend.join('');
// add chat preamble // add chat preamble
mesSendString = addChatsPreamble(mesSendString); mesSendString = addChatsPreamble(mesSendString);
@ -2908,19 +2930,13 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
mesSendString + mesSendString +
generatedPromtCache; generatedPromtCache;
// TODO: Move zero-depth anchor append to work like CFG and bias appends
if (zeroDepthAnchor && zeroDepthAnchor.length) { if (zeroDepthAnchor && zeroDepthAnchor.length) {
if (!isMultigenEnabled() || tokens_already_generated == 0) { if (!isMultigenEnabled() || tokens_already_generated == 0) {
combinedPrompt = appendZeroDepthAnchor(force_name2, zeroDepthAnchor, combinedPrompt); combinedPrompt = appendZeroDepthAnchor(force_name2, zeroDepthAnchor, combinedPrompt);
} }
} }
// Append zero-depth anchor for CFG
if (cfgPrompt && cfgPrompt?.value && cfgPrompt?.depth === 0) {
if (!isMultigenEnabled() || tokens_already_generated == 0) {
combinedPrompt = appendZeroDepthAnchor(force_name2, cfgPrompt.value, combinedPrompt);
}
}
combinedPrompt = combinedPrompt.replace(/\r/gm, ''); combinedPrompt = combinedPrompt.replace(/\r/gm, '');
if (power_user.collapse_newlines) { if (power_user.collapse_newlines) {

View File

@ -69,8 +69,6 @@ export function getCfgPrompt(guidanceScale, isNegative) {
); );
} }
console.log(guidanceScale.type);
console.log(cfgPromptCombine);
if (guidanceScale.type === cfgType.global || cfgPromptCombine.includes(cfgType.global)) { if (guidanceScale.type === cfgType.global || cfgPromptCombine.includes(cfgType.global)) {
splitCfgPrompt.unshift( splitCfgPrompt.unshift(
substituteParams( substituteParams(