Merge pull request #968 from bdashore3/staging

CFG: Improvements
This commit is contained in:
Cohee
2023-08-22 19:00:39 +03:00
committed by GitHub
6 changed files with 330 additions and 153 deletions

View File

@@ -163,6 +163,7 @@ import { deviceInfo } from "./scripts/RossAscends-mods.js";
import { registerPromptManagerMigration } from "./scripts/PromptManager.js"; import { registerPromptManagerMigration } from "./scripts/PromptManager.js";
import { getRegexedString, regex_placement } from "./scripts/extensions/regex/engine.js"; import { getRegexedString, regex_placement } from "./scripts/extensions/regex/engine.js";
import { FILTER_TYPES, FilterHelper } from "./scripts/filters.js"; import { FILTER_TYPES, FilterHelper } from "./scripts/filters.js";
import { getCfgPrompt, getGuidanceScale } from "./scripts/extensions/cfg/util.js";
import { import {
formatInstructModeChat, formatInstructModeChat,
formatInstructModePrompt, formatInstructModePrompt,
@@ -2729,6 +2730,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
} }
const anchorDepth = Math.abs(i - arrMes.length + 1); const anchorDepth = Math.abs(i - arrMes.length + 1);
// NOTE: Depth injected here!
const extensionAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, anchorDepth); const extensionAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, anchorDepth);
if (anchorDepth > 0 && extensionAnchor && extensionAnchor.length) { if (anchorDepth > 0 && extensionAnchor && extensionAnchor.length) {
@@ -2740,7 +2742,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
} }
let mesExmString = ''; let mesExmString = '';
let mesSendString = '';
function setPromtString() { function setPromtString() {
if (main_api == 'openai') { if (main_api == 'openai') {
@@ -2749,65 +2750,60 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
console.debug('--setting Prompt string'); console.debug('--setting Prompt string');
mesExmString = pinExmString ?? mesExamplesArray.slice(0, count_exm_add).join(''); mesExmString = pinExmString ?? mesExamplesArray.slice(0, count_exm_add).join('');
mesSendString = ''; mesSend[mesSend.length - 1] = modifyLastPromptLine(mesSend[mesSend.length - 1]);
for (let j = 0; j < mesSend.length; j++) {
const isBottom = j === mesSend.length - 1;
mesSendString += mesSend[j];
if (isBottom) {
mesSendString = modifyLastPromptLine(mesSendString);
}
}
} }
function modifyLastPromptLine(mesSendString) { function modifyLastPromptLine(lastMesString) {
// Add quiet generation prompt at depth 0 // Add quiet generation prompt at depth 0
if (quiet_prompt && quiet_prompt.length) { if (quiet_prompt && quiet_prompt.length) {
const name = is_pygmalion ? 'You' : name1; const name = is_pygmalion ? 'You' : name1;
const quietAppend = isInstruct ? formatInstructModeChat(name, quiet_prompt, false, true, false, name1, name2) : `\n${name}: ${quiet_prompt}`; const quietAppend = isInstruct ? formatInstructModeChat(name, quiet_prompt, false, true, false, name1, name2) : `\n${name}: ${quiet_prompt}`;
mesSendString += quietAppend; lastMesString += quietAppend;
// Bail out early // Bail out early
return mesSendString; return lastMesString;
} }
// Get instruct mode line // Get instruct mode line
if (isInstruct && tokens_already_generated === 0) { if (isInstruct && tokens_already_generated === 0) {
const name = isImpersonate ? (is_pygmalion ? 'You' : name1) : name2; const name = isImpersonate ? (is_pygmalion ? 'You' : name1) : name2;
mesSendString += formatInstructModePrompt(name, isImpersonate, promptBias, name1, name2); lastMesString += formatInstructModePrompt(name, isImpersonate, promptBias, name1, name2);
} }
// Get non-instruct impersonation line // Get non-instruct impersonation line
if (!isInstruct && isImpersonate && tokens_already_generated === 0) { if (!isInstruct && isImpersonate && tokens_already_generated === 0) {
const name = is_pygmalion ? 'You' : name1; const name = is_pygmalion ? 'You' : name1;
if (!mesSendString.endsWith('\n')) { if (!lastMesString.endsWith('\n')) {
mesSendString += '\n'; lastMesString += '\n';
} }
mesSendString += name + ':'; lastMesString += name + ':';
} }
// Add character's name // Add character's name
if (!isInstruct && force_name2 && tokens_already_generated === 0) { // Force name append on continue
if (!mesSendString.endsWith('\n')) { if (!isInstruct && force_name2 && (tokens_already_generated === 0 || isContinue)) {
mesSendString += '\n'; if (!lastMesString.endsWith('\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.");
mesSendString += `${name2}:`; return lastMesString;
} else if (promptBias.startsWith(' ')) { }
console.debug(`A prompt bias with a leading space was found: ${promptBias}`);
mesSendString += `${name2}:${promptBias}` // Clean up the already generated prompt for seamless addition
} else { function cleanupPromptCache(promptCache) {
console.debug(`A prompt bias was found: ${promptBias}`); // Remove the first occurrance of character's name
mesSendString += `${name2}: ${promptBias}`; if (promptCache.trimStart().startsWith(`${name2}:`)) {
} promptCache = promptCache.replace(`${name2}:`, '').trimStart();
} else if (power_user.user_prompt_bias && !isImpersonate && !isInstruct) {
console.debug(`A prompt bias was found without character's name appended: ${promptBias}`);
mesSendString += substituteParams(power_user.user_prompt_bias);
} }
return mesSendString; // Remove the first occurrance of prompt bias
if (promptCache.trimStart().startsWith(promptBias)) {
promptCache = promptCache.replace(promptBias, '');
}
return promptCache;
} }
function checkPromtSize() { function checkPromtSize() {
@@ -2816,7 +2812,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
const prompt = [ const prompt = [
storyString, storyString,
mesExmString, mesExmString,
mesSendString, mesSend.join(''),
generatedPromtCache, generatedPromtCache,
allAnchors, allAnchors,
quiet_prompt, quiet_prompt,
@@ -2845,30 +2841,90 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
setPromtString(); setPromtString();
} }
// add chat preamble // Fetches the combined prompt for both negative and positive prompts
mesSendString = addChatsPreamble(mesSendString); const cfgGuidanceScale = getGuidanceScale();
function getCombinedPrompt(isNegative) {
// add a custom dingus (if defined) let finalMesSend = [...mesSend];
mesSendString = addChatsSeparator(mesSendString); let cfgPrompt = {};
if (cfgGuidanceScale && cfgGuidanceScale?.value !== 1) {
let finalPromt = cfgPrompt = getCfgPrompt(cfgGuidanceScale, isNegative);
storyString +
afterScenarioAnchor +
mesExmString +
mesSendString +
generatedPromtCache;
if (zeroDepthAnchor && zeroDepthAnchor.length) {
if (!isMultigenEnabled() || tokens_already_generated == 0) {
finalPromt = appendZeroDepthAnchor(force_name2, zeroDepthAnchor, finalPromt);
} }
if (cfgPrompt && cfgPrompt?.value) {
if (cfgPrompt?.depth === 0) {
finalMesSend[finalMesSend.length - 1] +=
/\s/.test(finalMesSend[finalMesSend.length - 1].slice(-1))
? cfgPrompt.value
: ` ${cfgPrompt.value}`;
} else {
// TODO: Switch from splice method to insertion depth method
finalMesSend.splice(mesSend.length - cfgPrompt.depth, 0, `${cfgPrompt.value}\n`);
}
}
// Add prompt bias after everything else
// Always run with continue
if (!isInstruct && !isImpersonate && (tokens_already_generated === 0 || isContinue)) {
if (promptBias.trim().length !== 0) {
finalMesSend[finalMesSend.length - 1] +=
/\s/.test(finalMesSend[finalMesSend.length - 1].slice(-1))
? promptBias.trimStart()
: ` ${promptBias.trimStart()}`;
}
}
// 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
mesSendString = addChatsPreamble(mesSendString);
// add a custom dingus (if defined)
mesSendString = addChatsSeparator(mesSendString);
let combinedPrompt =
storyString +
afterScenarioAnchor +
mesExmString +
mesSendString +
generatedPromtCache;
// TODO: Move zero-depth anchor append to work like CFG and bias appends
if (zeroDepthAnchor && zeroDepthAnchor.length) {
if (!isMultigenEnabled() || tokens_already_generated == 0) {
combinedPrompt = appendZeroDepthAnchor(force_name2, zeroDepthAnchor, combinedPrompt);
}
}
combinedPrompt = combinedPrompt.replace(/\r/gm, '');
if (power_user.collapse_newlines) {
combinedPrompt = collapseNewlines(combinedPrompt);
}
return combinedPrompt;
} }
finalPromt = finalPromt.replace(/\r/gm, ''); // Get the negative prompt first since it has the unmodified mesSend array
let negativePrompt = main_api == 'textgenerationwebui' ? getCombinedPrompt(true) : undefined;
let finalPromt = getCombinedPrompt(false);
// Include the entire guidance scale object
const cfgValues = {
guidanceScale: cfgGuidanceScale,
negativePrompt: negativePrompt
};
if (power_user.collapse_newlines) {
finalPromt = collapseNewlines(finalPromt);
}
let this_amount_gen = parseInt(amount_gen); // how many tokens the AI will be requested to generate let this_amount_gen = parseInt(amount_gen); // how many tokens the AI will be requested to generate
let this_settings = koboldai_settings[koboldai_setting_names[preset_settings]]; let this_settings = koboldai_settings[koboldai_setting_names[preset_settings]];
@@ -2879,6 +2935,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
let thisPromptBits = []; let thisPromptBits = [];
// TODO: Make this a switch
if (main_api == 'koboldhorde' && horde_settings.auto_adjust_response_length) { if (main_api == 'koboldhorde' && horde_settings.auto_adjust_response_length) {
this_amount_gen = Math.min(this_amount_gen, adjustedParams.maxLength); this_amount_gen = Math.min(this_amount_gen, adjustedParams.maxLength);
this_amount_gen = Math.max(this_amount_gen, MIN_AMOUNT_GEN); // prevent validation errors this_amount_gen = Math.max(this_amount_gen, MIN_AMOUNT_GEN); // prevent validation errors
@@ -2901,12 +2958,12 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
} }
} }
else if (main_api == 'textgenerationwebui') { else if (main_api == 'textgenerationwebui') {
generate_data = getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate); generate_data = getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate, cfgValues);
generate_data.use_mancer = api_use_mancer_webui; generate_data.use_mancer = api_use_mancer_webui;
} }
else if (main_api == 'novel') { else if (main_api == 'novel') {
const this_settings = novelai_settings[novelai_setting_names[nai_settings.preset_settings_novel]]; const this_settings = novelai_settings[novelai_setting_names[nai_settings.preset_settings_novel]];
generate_data = getNovelGenerationData(finalPromt, this_settings, this_amount_gen, isImpersonate); generate_data = getNovelGenerationData(finalPromt, this_settings, this_amount_gen, isImpersonate, cfgValues);
} }
else if (main_api == 'openai') { else if (main_api == 'openai') {
let [prompt, counts] = prepareOpenAIMessages({ let [prompt, counts] = prepareOpenAIMessages({
@@ -2961,7 +3018,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
storyString: storyString, storyString: storyString,
afterScenarioAnchor: afterScenarioAnchor, afterScenarioAnchor: afterScenarioAnchor,
examplesString: examplesString, examplesString: examplesString,
mesSendString: mesSendString, mesSendString: mesSend.join(''),
generatedPromtCache: generatedPromtCache, generatedPromtCache: generatedPromtCache,
promptBias: promptBias, promptBias: promptBias,
finalPromt: finalPromt, finalPromt: finalPromt,

View File

@@ -23,7 +23,8 @@ const defaultSettings = {
}; };
const settingType = { const settingType = {
guidance_scale: 0, guidance_scale: 0,
negative_prompt: 1 negative_prompt: 1,
positive_prompt: 2
} }
// Used for character and chat CFG values // Used for character and chat CFG values
@@ -36,19 +37,19 @@ function setCharCfg(tempValue, setting) {
const avatarName = getCharaFilename(); const avatarName = getCharaFilename();
// Assign temp object // Assign temp object
let tempCharaCfg; let tempCharaCfg = {
name: avatarName
};
switch(setting) { switch(setting) {
case settingType.guidance_scale: case settingType.guidance_scale:
tempCharaCfg = { tempCharaCfg["guidance_scale"] = Number(tempValue);
"name": avatarName,
"guidance_scale": Number(tempValue)
}
break; break;
case settingType.negative_prompt: case settingType.negative_prompt:
tempCharaCfg = { tempCharaCfg["negative_prompt"] = tempValue;
"name": avatarName, break;
"negative_prompt": tempValue case settingType.positive_prompt:
} tempCharaCfg["positive_prompt"] = tempValue;
break; break;
default: default:
return false; return false;
@@ -66,7 +67,11 @@ function setCharCfg(tempValue, setting) {
const tempAssign = Object.assign(existingCharaCfg, tempCharaCfg); const tempAssign = Object.assign(existingCharaCfg, tempCharaCfg);
// If both values are default, remove the entry // If both values are default, remove the entry
if (!existingCharaCfg.useChara && (tempAssign.guidance_scale ?? 1.00) === 1.00 && (tempAssign.negative_prompt?.length ?? 0) === 0) { if (!existingCharaCfg.useChara &&
(tempAssign.guidance_scale ?? 1.00) === 1.00 &&
(tempAssign.negative_prompt?.length ?? 0) === 0 &&
(tempAssign.positive_prompt?.length ?? 0) === 0)
{
extension_settings.cfg.chara.splice(existingCharaCfgIndex, 1); extension_settings.cfg.chara.splice(existingCharaCfgIndex, 1);
} }
} else if (avatarName && tempValue.length > 0) { } else if (avatarName && tempValue.length > 0) {
@@ -95,6 +100,9 @@ function setChatCfg(tempValue, setting) {
case settingType.negative_prompt: case settingType.negative_prompt:
chat_metadata[metadataKeys.negative_prompt] = tempValue; chat_metadata[metadataKeys.negative_prompt] = tempValue;
break; break;
case settingType.positive_prompt:
chat_metadata[metadataKeys.positive_prompt] = tempValue;
break;
default: default:
return false; return false;
} }
@@ -174,20 +182,40 @@ function loadSettings() {
$('#chat_cfg_guidance_scale').val(chat_metadata[metadataKeys.guidance_scale] ?? 1.0.toFixed(2)); $('#chat_cfg_guidance_scale').val(chat_metadata[metadataKeys.guidance_scale] ?? 1.0.toFixed(2));
$('#chat_cfg_guidance_scale_counter').text(chat_metadata[metadataKeys.guidance_scale]?.toFixed(2) ?? 1.0.toFixed(2)); $('#chat_cfg_guidance_scale_counter').text(chat_metadata[metadataKeys.guidance_scale]?.toFixed(2) ?? 1.0.toFixed(2));
$('#chat_cfg_negative_prompt').val(chat_metadata[metadataKeys.negative_prompt] ?? ''); $('#chat_cfg_negative_prompt').val(chat_metadata[metadataKeys.negative_prompt] ?? '');
$('#chat_cfg_positive_prompt').val(chat_metadata[metadataKeys.positive_prompt] ?? '');
$('#groupchat_cfg_use_chara').prop('checked', chat_metadata[metadataKeys.groupchat_individual_chars] ?? false); $('#groupchat_cfg_use_chara').prop('checked', chat_metadata[metadataKeys.groupchat_individual_chars] ?? false);
if (chat_metadata[metadataKeys.negative_combine]?.length > 0) { if (chat_metadata[metadataKeys.prompt_combine]?.length > 0) {
chat_metadata[metadataKeys.negative_combine].forEach((element) => { chat_metadata[metadataKeys.prompt_combine].forEach((element) => {
$(`input[name="cfg_negative_combine"][value="${element}"]`) $(`input[name="cfg_prompt_combine"][value="${element}"]`)
.prop("checked", true); .prop("checked", true);
}); });
} }
// Display the negative separator in quotes if not quoted already
let promptSeparatorDisplay = [];
const promptSeparator = chat_metadata[metadataKeys.prompt_separator];
if (promptSeparator) {
promptSeparatorDisplay.push(promptSeparator);
if (!promptSeparator.startsWith(`"`)) {
promptSeparatorDisplay.unshift(`"`);
}
if (!promptSeparator.endsWith(`"`)) {
promptSeparatorDisplay.push(`"`);
}
}
$('#cfg_prompt_separator').val(promptSeparatorDisplay.length === 0 ? '' : promptSeparatorDisplay.join(''));
$('#cfg_prompt_insertion_depth').val(chat_metadata[metadataKeys.prompt_insertion_depth] ?? 1);
// Set character CFG if it exists // Set character CFG if it exists
if (!selected_group) { if (!selected_group) {
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename()); const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
$('#chara_cfg_guidance_scale').val(charaCfg?.guidance_scale ?? 1.00); $('#chara_cfg_guidance_scale').val(charaCfg?.guidance_scale ?? 1.00);
$('#chara_cfg_guidance_scale_counter').text(charaCfg?.guidance_scale?.toFixed(2) ?? 1.0.toFixed(2)); $('#chara_cfg_guidance_scale_counter').text(charaCfg?.guidance_scale?.toFixed(2) ?? 1.0.toFixed(2));
$('#chara_cfg_negative_prompt').val(charaCfg?.negative_prompt ?? ''); $('#chara_cfg_negative_prompt').val(charaCfg?.negative_prompt ?? '');
$('#chara_cfg_positive_prompt').val(charaCfg?.positive_prompt ?? '');
} }
} }
@@ -204,26 +232,50 @@ async function initialLoadSettings() {
$('#global_cfg_guidance_scale').val(extension_settings.cfg.global.guidance_scale); $('#global_cfg_guidance_scale').val(extension_settings.cfg.global.guidance_scale);
$('#global_cfg_guidance_scale_counter').text(extension_settings.cfg.global.guidance_scale.toFixed(2)); $('#global_cfg_guidance_scale_counter').text(extension_settings.cfg.global.guidance_scale.toFixed(2));
$('#global_cfg_negative_prompt').val(extension_settings.cfg.global.negative_prompt); $('#global_cfg_negative_prompt').val(extension_settings.cfg.global.negative_prompt);
$('#global_cfg_positive_prompt').val(extension_settings.cfg.global.positive_prompt);
} }
function migrateSettings() { function migrateSettings() {
let performSave = false; let performSettingsSave = false;
let performMetaSave = false;
if (power_user.guidance_scale) { if (power_user.guidance_scale) {
extension_settings.cfg.global.guidance_scale = power_user.guidance_scale; extension_settings.cfg.global.guidance_scale = power_user.guidance_scale;
delete power_user['guidance_scale']; delete power_user['guidance_scale'];
performSave = true; performSettingsSave = true;
} }
if (power_user.negative_prompt) { if (power_user.negative_prompt) {
extension_settings.cfg.global.negative_prompt = power_user.negative_prompt; extension_settings.cfg.global.negative_prompt = power_user.negative_prompt;
delete power_user['negative_prompt']; delete power_user['negative_prompt'];
performSave = true; performSettingsSave = true;
} }
if (performSave) { if (chat_metadata["cfg_negative_combine"]) {
chat_metadata[metadataKeys.prompt_combine] = chat_metadata["cfg_negative_combine"];
chat_metadata["cfg_negative_combine"] = undefined;
performMetaSave = true;
}
if (chat_metadata["cfg_negative_insertion_depth"]) {
chat_metadata[metadataKeys.prompt_insertion_depth] = chat_metadata["cfg_negative_insertion_depth"];
chat_metadata["cfg_negative_insertion_depth"] = undefined;
performMetaSave = true;
}
if (chat_metadata["cfg_negative_separator"]) {
chat_metadata[metadataKeys.prompt_separator] = chat_metadata["cfg_negative_separator"];
chat_metadata["cfg_negative_separator"] = undefined;
performMetaSave = true;
}
if (performSettingsSave) {
saveSettingsDebounced(); saveSettingsDebounced();
} }
if (performMetaSave) {
saveMetadataDebounced();
}
} }
// This function is called when the extension is loaded // This function is called when the extension is loaded
@@ -255,6 +307,10 @@ jQuery(async () => {
setChatCfg($(this).val(), settingType.negative_prompt); setChatCfg($(this).val(), settingType.negative_prompt);
}); });
windowHtml.find('#chat_cfg_positive_prompt').on('input', function() {
setChatCfg($(this).val(), settingType.positive_prompt);
});
windowHtml.find('#chara_cfg_guidance_scale').on('input', function() { windowHtml.find('#chara_cfg_guidance_scale').on('input', function() {
const value = $(this).val(); const value = $(this).val();
const success = setCharCfg(value, settingType.guidance_scale); const success = setCharCfg(value, settingType.guidance_scale);
@@ -267,6 +323,10 @@ jQuery(async () => {
setCharCfg($(this).val(), settingType.negative_prompt); setCharCfg($(this).val(), settingType.negative_prompt);
}); });
windowHtml.find('#chara_cfg_positive_prompt').on('input', function() {
setCharCfg($(this).val(), settingType.positive_prompt);
});
windowHtml.find('#global_cfg_guidance_scale').on('input', function() { windowHtml.find('#global_cfg_guidance_scale').on('input', function() {
extension_settings.cfg.global.guidance_scale = Number($(this).val()); extension_settings.cfg.global.guidance_scale = Number($(this).val());
$('#global_cfg_guidance_scale_counter').text(extension_settings.cfg.global.guidance_scale.toFixed(2)); $('#global_cfg_guidance_scale_counter').text(extension_settings.cfg.global.guidance_scale.toFixed(2));
@@ -278,14 +338,29 @@ jQuery(async () => {
saveSettingsDebounced(); saveSettingsDebounced();
}); });
windowHtml.find(`input[name="cfg_negative_combine"]`).on('input', function() { windowHtml.find('#global_cfg_positive_prompt').on('input', function() {
const values = windowHtml.find(`input[name="cfg_negative_combine"]`) extension_settings.cfg.global.positive_prompt = $(this).val();
saveSettingsDebounced();
});
windowHtml.find(`input[name="cfg_prompt_combine"]`).on('input', function() {
const values = windowHtml.find(`input[name="cfg_prompt_combine"]`)
.filter(":checked") .filter(":checked")
.map(function() { return parseInt($(this).val()) }) .map(function() { return parseInt($(this).val()) })
.get() .get()
.filter((e) => e !== NaN) || []; .filter((e) => e !== NaN) || [];
chat_metadata[metadataKeys.negative_combine] = values; chat_metadata[metadataKeys.prompt_combine] = values;
saveMetadataDebounced();
});
windowHtml.find(`#cfg_prompt_insertion_depth`).on('input', function() {
chat_metadata[metadataKeys.prompt_insertion_depth] = Number($(this).val());
saveMetadataDebounced();
});
windowHtml.find(`#cfg_prompt_separator`).on('input', function() {
chat_metadata[metadataKeys.prompt_separator] = $(this).val();
saveMetadataDebounced(); saveMetadataDebounced();
}); });

View File

@@ -1,4 +1,4 @@
import { chat_metadata, this_chid } from "../../../script.js"; import { chat_metadata, substituteParams, this_chid } from "../../../script.js";
import { extension_settings, getContext } from "../../extensions.js" import { extension_settings, getContext } from "../../extensions.js"
import { selected_group } from "../../group-chats.js"; import { selected_group } from "../../group-chats.js";
import { getCharaFilename } from "../../utils.js"; import { getCharaFilename } from "../../utils.js";
@@ -11,46 +11,20 @@ export const cfgType = {
export const metadataKeys = { export const metadataKeys = {
guidance_scale: "cfg_guidance_scale", guidance_scale: "cfg_guidance_scale",
negative_prompt: "cfg_negative_prompt", negative_prompt: "cfg_negative_prompt",
negative_combine: "cfg_negative_combine", positive_prompt: "cfg_positive_prompt",
groupchat_individual_chars: "cfg_groupchat_individual_chars" prompt_combine: "cfg_prompt_combine",
groupchat_individual_chars: "cfg_groupchat_individual_chars",
prompt_insertion_depth: "cfg_prompt_insertion_depth",
prompt_separator: "cfg_prompt_separator"
} }
// Gets the CFG value from hierarchy of chat -> character -> global // Gets the CFG guidance scale
// Returns undefined values which should be handled in the respective backend APIs // If the guidance scale is 1, ignore the CFG prompt(s) since it won't be used anyways
export function getCfg() { export function getGuidanceScale() {
let splitNegativePrompt = [];
const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid)); const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid));
const guidanceScale = getGuidanceScale(charaCfg);
const chatNegativeCombine = chat_metadata[metadataKeys.negative_combine] ?? [];
// If there's a guidance scale, continue. Otherwise assume undefined
if (guidanceScale?.value && guidanceScale?.value !== 1) {
if (guidanceScale.type === cfgType.chat || chatNegativeCombine.includes(cfgType.chat)) {
splitNegativePrompt.push(chat_metadata[metadataKeys.negative_prompt]?.trim());
}
if (guidanceScale.type === cfgType.chara || chatNegativeCombine.includes(cfgType.chara)) {
splitNegativePrompt.push(charaCfg.negative_prompt?.trim())
}
if (guidanceScale.type === cfgType.global || chatNegativeCombine.includes(cfgType.global)) {
splitNegativePrompt.push(extension_settings.cfg.global.negative_prompt?.trim());
}
const combinedNegatives = splitNegativePrompt.filter((e) => e.length > 0).join(", ");
console.debug(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedNegatives}`)
return {
guidanceScale: guidanceScale.value,
negativePrompt: combinedNegatives
}
}
}
// If the guidance scale is 1, ignore the CFG negative prompt since it won't be used anyways
function getGuidanceScale(charaCfg) {
const chatGuidanceScale = chat_metadata[metadataKeys.guidance_scale]; const chatGuidanceScale = chat_metadata[metadataKeys.guidance_scale];
const groupchatCharOverride = chat_metadata[metadataKeys.groupchat_individual_chars] ?? false; const groupchatCharOverride = chat_metadata[metadataKeys.groupchat_individual_chars] ?? false;
if (chatGuidanceScale && chatGuidanceScale !== 1 && !groupchatCharOverride) { if (chatGuidanceScale && chatGuidanceScale !== 1 && !groupchatCharOverride) {
return { return {
type: cfgType.chat, type: cfgType.chat,
@@ -70,3 +44,48 @@ function getGuidanceScale(charaCfg) {
value: extension_settings.cfg.global.guidance_scale value: extension_settings.cfg.global.guidance_scale
}; };
} }
// Gets the CFG prompt
export function getCfgPrompt(guidanceScale, isNegative) {
let splitCfgPrompt = [];
const cfgPromptCombine = chat_metadata[metadataKeys.prompt_combine] ?? [];
if (guidanceScale.type === cfgType.chat || cfgPromptCombine.includes(cfgType.chat)) {
splitCfgPrompt.unshift(
substituteParams(
chat_metadata[isNegative ? metadataKeys.negative_prompt : metadataKeys.positive_prompt]
)
?.trim()
);
}
const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid));
if (guidanceScale.type === cfgType.chara || cfgPromptCombine.includes(cfgType.chara)) {
splitCfgPrompt.unshift(
substituteParams(
isNegative ? charaCfg.negative_prompt : charaCfg.positive_prompt
)
?.trim()
);
}
if (guidanceScale.type === cfgType.global || cfgPromptCombine.includes(cfgType.global)) {
splitCfgPrompt.unshift(
substituteParams(
isNegative ? extension_settings.cfg.global.negative_prompt : extension_settings.cfg.global.positive_prompt
)
?.trim()
);
}
// This line is a bit hacky with a JSON.stringify and JSON.parse. Fix this if possible.
const customSeparator = JSON.parse(chat_metadata[metadataKeys.prompt_separator] || JSON.stringify("\n")) ?? "\n";
const combinedCfgPrompt = splitCfgPrompt.filter((e) => e.length > 0).join(customSeparator);
const insertionDepth = chat_metadata[metadataKeys.prompt_insertion_depth] ?? 1;
console.log(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedCfgPrompt}`);
return {
value: combinedCfgPrompt,
depth: insertionDepth
};
}

View File

@@ -14,7 +14,7 @@
<small> <small>
<b>Unique to this chat.</b><br> <b>Unique to this chat.</b><br>
</small> </small>
<label for="chat_cfg_negative_prompt"> <label for="chat_cfg_guidance_scale">
<span data-i18n="Scale">Scale</span> <span data-i18n="Scale">Scale</span>
<small data-i18n="1 = disabled">1 = disabled</small> <small data-i18n="1 = disabled">1 = disabled</small>
</label> </label>
@@ -33,6 +33,11 @@
<span data-i18n="Negative Prompt">Negative Prompt</span> <span data-i18n="Negative Prompt">Negative Prompt</span>
</label> </label>
<textarea id="chat_cfg_negative_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea> <textarea id="chat_cfg_negative_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea>
<label for="chat_cfg_positive_prompt">
<span data-i18n="Positive Prompt">Positive Prompt</span>
</label>
<textarea id="chat_cfg_positive_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea>
</div> </div>
<div id="groupchat_cfg_use_chara_container"> <div id="groupchat_cfg_use_chara_container">
<label class="checkbox_label" for="groupchat_cfg_use_chara"> <label class="checkbox_label" for="groupchat_cfg_use_chara">
@@ -53,7 +58,7 @@
<div class="inline-drawer-content"> <div class="inline-drawer-content">
<small><b>Will be automatically added as the CFG for this character.</b></small> <small><b>Will be automatically added as the CFG for this character.</b></small>
<br /> <br />
<label for="chara_cfg_negative_prompt"> <label for="chara_cfg_guidance_scale">
<span data-i18n="Scale">Scale</span> <span data-i18n="Scale">Scale</span>
<small data-i18n="1 = disabled">1 = disabled</small> <small data-i18n="1 = disabled">1 = disabled</small>
</label> </label>
@@ -72,6 +77,11 @@
<span data-i18n="Negative Prompt">Negative Prompt</span> <span data-i18n="Negative Prompt">Negative Prompt</span>
</label> </label>
<textarea id="chara_cfg_negative_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea> <textarea id="chara_cfg_negative_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea>
<label for="chara_cfg_positive_prompt">
<span data-i18n="Positive Prompt">Positive Prompt</span>
</label>
<textarea id="chara_cfg_positive_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea>
</div> </div>
</div> </div>
</div> </div>
@@ -86,7 +96,7 @@
<div class="inline-drawer-content"> <div class="inline-drawer-content">
<small><b>Will be used as the default CFG options for every chat unless overridden.</b></small> <small><b>Will be used as the default CFG options for every chat unless overridden.</b></small>
<br /> <br />
<label for="global_cfg_negative_prompt"> <label for="global_cfg_guidance_scale">
<span data-i18n="Scale">Scale</span> <span data-i18n="Scale">Scale</span>
<small data-i18n="1 = disabled">1 = disabled</small> <small data-i18n="1 = disabled">1 = disabled</small>
</label> </label>
@@ -105,39 +115,56 @@
<span data-i18n="Negative Prompt">Negative Prompt</span> <span data-i18n="Negative Prompt">Negative Prompt</span>
</label> </label>
<textarea id="global_cfg_negative_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea> <textarea id="global_cfg_negative_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea>
<label for="global_cfg_positive_prompt">
<span data-i18n="Positive Prompt">Negative Prompt</span>
</label>
<textarea id="global_cfg_positive_prompt" rows="2" class="text_pole textarea_compact" data-i18n="[placeholder]write short replies, write replies using past tense" placeholder="write short replies, write replies using past tense"></textarea>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div id="cfg_negative_combine_container"> <div id="cfg_prompt_combine_container">
<hr class="sysHR"> <hr class="sysHR">
<div class="inline-drawer"> <div class="inline-drawer">
<div id="defaultANBlockToggle" class="inline-drawer-toggle inline-drawer-header"> <div id="defaultANBlockToggle" class="inline-drawer-toggle inline-drawer-header">
<b>Negative Cascading</b> <b>CFG Prompt Cascading</b>
<div class="inline-drawer-icon fa-solid fa-circle-chevron-down down"></div> <div class="inline-drawer-icon fa-solid fa-circle-chevron-down down"></div>
</div> </div>
<div class="inline-drawer-content"> <div class="inline-drawer-content">
<small> <div class="flex-container flexFlowColumn">
<b>Combine negative prompts from other boxes.</b> <small>
<br /> <b>Combine positive/negative prompts from other boxes.</b>
For example, ticking the chat, global, and character boxes combine all negative prompts into a comma-separated string. <br />
</small> For example, ticking the chat, global, and character boxes combine all negative prompts into a comma-separated string.
</small>
</div>
<br /> <br />
<label for="cfg_negative_combine"> <div class="flex-container flexFlowColumn">
<span data-i18n="Scale">Always Include</span> <label for="cfg_prompt_combine">
</label> <span data-i18n="Scale">Always Include</span>
<label class="checkbox_label"> </label>
<input type="checkbox" name="cfg_negative_combine" value="0" /> <label class="checkbox_label">
<span data-i18n="Chat Negatives">Chat Negatives</span> <input type="checkbox" name="cfg_prompt_combine" value="0" />
</label> <span data-i18n="Chat Negatives">Chat Negatives</span>
<label class="checkbox_label"> </label>
<input type="checkbox" name="cfg_negative_combine" value="1" /> <label class="checkbox_label">
<span data-i18n="Character Negatives">Character Negatives</span> <input type="checkbox" name="cfg_prompt_combine" value="1" />
</label> <span data-i18n="Character Negatives">Character Negatives</span>
<label class="checkbox_label"> </label>
<input type="checkbox" name="cfg_negative_combine" value="2" /> <label class="checkbox_label">
<span data-i18n="Global Negatives">Global Negatives</span> <input type="checkbox" name="cfg_prompt_combine" value="2" />
</label> <span data-i18n="Global Negatives">Global Negatives</span>
</label>
</div>
<div class="flex-container flexFlowColumn">
<label>
Custom Separator: <input id="cfg_prompt_separator" class="text_pole textarea_compact widthUnset" placeholder="&quot;\n&quot;" type="text" />
</label>
<label>
Insertion Depth: <input id="cfg_prompt_insertion_depth" class="text_pole widthUnset" type="number" min="0" max="99" />
</label>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -7,7 +7,7 @@ import {
saveSettingsDebounced, saveSettingsDebounced,
setGenerationParamsFromPreset setGenerationParamsFromPreset
} from "../script.js"; } from "../script.js";
import { getCfg } from "./extensions/cfg/util.js"; import { getCfgPrompt } from "./extensions/cfg/util.js";
import { MAX_CONTEXT_DEFAULT, tokenizers } from "./power-user.js"; import { MAX_CONTEXT_DEFAULT, tokenizers } from "./power-user.js";
import { import {
getSortableDelay, getSortableDelay,
@@ -395,7 +395,11 @@ function getBadWordPermutations(text) {
return result; return result;
} }
export function getNovelGenerationData(finalPrompt, this_settings, this_amount_gen, isImpersonate) { export function getNovelGenerationData(finalPrompt, this_settings, this_amount_gen, isImpersonate, cfgValues) {
if (cfgValues.guidanceScale && cfgValues.guidanceScale?.value !== 1) {
cfgValues.negativePrompt = (getCfgPrompt(cfgValues.guidanceScale, true))?.value;
}
const clio = nai_settings.model_novel.includes('clio'); const clio = nai_settings.model_novel.includes('clio');
const kayra = nai_settings.model_novel.includes('kayra'); const kayra = nai_settings.model_novel.includes('kayra');
@@ -410,7 +414,6 @@ export function getNovelGenerationData(finalPrompt, this_settings, this_amount_g
: undefined; : undefined;
const prefix = selectPrefix(nai_settings.prefix, finalPrompt); const prefix = selectPrefix(nai_settings.prefix, finalPrompt);
const cfgSettings = getCfg();
let logitBias = []; let logitBias = [];
if (tokenizerType !== tokenizers.NONE && Array.isArray(nai_settings.logit_bias) && nai_settings.logit_bias.length) { if (tokenizerType !== tokenizers.NONE && Array.isArray(nai_settings.logit_bias) && nai_settings.logit_bias.length) {
@@ -437,8 +440,8 @@ export function getNovelGenerationData(finalPrompt, this_settings, this_amount_g
"typical_p": parseFloat(nai_settings.typical_p), "typical_p": parseFloat(nai_settings.typical_p),
"mirostat_lr": parseFloat(nai_settings.mirostat_lr), "mirostat_lr": parseFloat(nai_settings.mirostat_lr),
"mirostat_tau": parseFloat(nai_settings.mirostat_tau), "mirostat_tau": parseFloat(nai_settings.mirostat_tau),
"cfg_scale": cfgSettings?.guidanceScale ?? parseFloat(nai_settings.cfg_scale), "cfg_scale": cfgValues?.guidanceScale?.value ?? parseFloat(nai_settings.cfg_scale),
"cfg_uc": cfgSettings?.negativePrompt ?? nai_settings.cfg_uc ?? "", "cfg_uc": cfgValues?.negativePrompt ?? nai_settings.cfg_uc ?? "",
"phrase_rep_pen": nai_settings.phrase_rep_pen, "phrase_rep_pen": nai_settings.phrase_rep_pen,
"stop_sequences": stopSequences, "stop_sequences": stopSequences,
"bad_words_ids": badWordIds, "bad_words_ids": badWordIds,

View File

@@ -6,8 +6,6 @@ import {
setGenerationParamsFromPreset, setGenerationParamsFromPreset,
} from "../script.js"; } from "../script.js";
import { getCfg } from "./extensions/cfg/util.js";
import { import {
power_user, power_user,
} from "./power-user.js"; } from "./power-user.js";
@@ -235,9 +233,7 @@ async function generateTextGenWithStreaming(generate_data, signal) {
} }
} }
export function getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate) { export function getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate, cfgValues) {
const cfgValues = getCfg();
return { return {
'prompt': finalPromt, 'prompt': finalPromt,
'max_new_tokens': this_amount_gen, 'max_new_tokens': this_amount_gen,
@@ -255,8 +251,8 @@ export function getTextGenGenerationData(finalPromt, this_amount_gen, isImperson
'penalty_alpha': textgenerationwebui_settings.penalty_alpha, 'penalty_alpha': textgenerationwebui_settings.penalty_alpha,
'length_penalty': textgenerationwebui_settings.length_penalty, 'length_penalty': textgenerationwebui_settings.length_penalty,
'early_stopping': textgenerationwebui_settings.early_stopping, 'early_stopping': textgenerationwebui_settings.early_stopping,
'guidance_scale': cfgValues?.guidanceScale ?? textgenerationwebui_settings.guidance_scale ?? 1, 'guidance_scale': isImpersonate ? 1 : cfgValues?.guidanceScale?.value ?? textgenerationwebui_settings.guidance_scale ?? 1,
'negative_prompt': cfgValues?.negativePrompt ?? textgenerationwebui_settings.negative_prompt ?? '', 'negative_prompt': isImpersonate ? '' : cfgValues?.negativePrompt ?? textgenerationwebui_settings.negative_prompt ?? '',
'seed': textgenerationwebui_settings.seed, 'seed': textgenerationwebui_settings.seed,
'add_bos_token': textgenerationwebui_settings.add_bos_token, 'add_bos_token': textgenerationwebui_settings.add_bos_token,
'stopping_strings': getStoppingStrings(isImpersonate, false), 'stopping_strings': getStoppingStrings(isImpersonate, false),