CFG: Add per-chat CFG and fixes

Per-chat CFG applies a CFG setting per-chat only rather than character
or globally. This overrides all other CFG settings (this will be changed).

Also add fixes to remove character CFG entries properly and not to
apply CFG if the scale is 1 as that won't do anything to generation.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-08-11 01:35:22 -04:00
parent 63ee7d4e10
commit 5bb6c89868
3 changed files with 74 additions and 25 deletions

View File

@ -2,13 +2,12 @@ import {
chat_metadata,
eventSource,
event_types,
getTokenCount,
saveSettingsDebounced,
this_chid,
} from "../../../script.js";
import { selected_group } from "../../group-chats.js";
import { extension_settings, getContext, saveMetadataDebounced, loadExtensionSettings } from "../../extensions.js";
import { getCharaFilename, debounce, waitUntilCondition, delay } from "../../utils.js";
import { extension_settings, saveMetadataDebounced } from "../../extensions.js";
import { getCharaFilename, delay } from "../../utils.js";
import { power_user } from "../../power-user.js";
// Keep track of where your extension is located, name should match repo name
@ -51,7 +50,7 @@ function setCharCfg(tempValue, setting) {
}
break;
default:
return;
return false;
}
let existingCharaCfgIndex;
@ -66,7 +65,7 @@ function setCharCfg(tempValue, setting) {
const tempAssign = Object.assign(existingCharaCfg, tempCharaCfg);
// if both values are default, remove the entry
if ((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) {
extension_settings.cfg.chara.splice(existingCharaCfgIndex, 1);
}
} else if (avatarName && tempValue.length > 0) {
@ -79,31 +78,46 @@ function setCharCfg(tempValue, setting) {
extension_settings.cfg.chara.push(tempCharaCfg);
} else {
console.log("Character CFG error: No avatar name key could be found.");
toastr.error("Something went wrong. Could not save character's CFG.");
// Don't save settings if something went wrong
return;
return false;
}
updateSettings();
return true;
}
function setCharCfgCheckbox() {
const value = !!$(this).prop('checked');
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
const charaCfgIndex = extension_settings.cfg.chara.findIndex((e) => e.name === getCharaFilename());
const charaCfg = extension_settings.cfg.chara[charaCfgIndex];
if (charaCfg) {
charaCfg.useChara = value;
if (!value && (charaCfg.guidance_scale ?? 1.00) === 1.00 && (charaCfg.negative_prompt?.length ?? 0) === 0) {
extension_settings.cfg.chara.splice(charaCfgIndex, 1);
} else {
charaCfg.useChara = value;
}
updateSettings();
}
}
function setChatCfgNegative() {
function setChatCfg(tempValue, setting) {
switch(setting) {
case settingType.guidance_scale:
chat_metadata['guidance_scale'] = tempValue;
break;
case settingType.negative_prompt:
chat_metadata['negative_prompt'] = tempValue;
break;
default:
return false;
}
}
function setChatCfgScale() {
saveMetadataDebounced();
return true;
}
// TODO: Only change CFG when character is selected
@ -160,6 +174,10 @@ function onChatChanged() {
// Reloads chat-specific settings
function loadSettings() {
// Set chat CFG if it exists
$('#chat_cfg_guidance_scale').val(chat_metadata['guidance_scale'] ?? 1.00);
$('#chat_cfg_guidance_scale_counter').text(chat_metadata['guidance_scale']?.toFixed(2) ?? 1.00);
$('#chat_cfg_negative_prompt').val(chat_metadata['negative_prompt'] ?? '');
// Set character CFG if it exists
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
@ -221,8 +239,24 @@ jQuery(async () => {
setTimeout(function () { $('#cfgConfig').hide() }, 200);
})
windowHtml.find('#chat_cfg_guidance_scale').on('input', function() {
const numberValue = Number($(this).val());
const success = setChatCfg(numberValue, settingType.guidance_scale);
if (success) {
$('#chat_cfg_guidance_scale_counter').text(numberValue.toFixed(2));
}
});
windowHtml.find('#chat_cfg_negative_prompt').on('input', function() {
setChatCfg($(this).val(), settingType.negative_prompt);
});
windowHtml.find('#chara_cfg_guidance_scale').on('input', function() {
setCharCfg($(this).val(), settingType.guidance_scale);
const value = $(this).val();
const success = setCharCfg(value, settingType.guidance_scale);
if (success) {
$('#chara_cfg_guidance_scale_counter').text(Number(value).toFixed(2));
}
});
windowHtml.find('#chara_cfg_negative_prompt').on('input', function() {

View File

@ -1,18 +1,33 @@
import { chat_metadata } from "../../../script.js";
import { extension_settings } from "../../extensions.js"
import { getCharaFilename } from "../../utils.js";
// TODO: Update to use per-chat and per-character CFG
// Gets the CFG value from hierarchy of chat -> character -> global
// Returns undefined values which should be handled in the respective backend APIs
// If the guidance scale is 1, ignore the CFG negative prompt since it won't be used anyways
// TODO: Add the ability to combine negative prompts if specified. Proposed, chat + global and chat + chara
// TODO: Add groupchat support and fetch the CFG values for the current character
export function getCfg() {
if (chat_metadata['guidance_scale'] !== 1) {
return {
guidanceScale: chat_metadata['guidance_scale'],
negativePrompt: chat_metadata['negative_prompt']
}
}
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
if (charaCfg && charaCfg?.useChara) {
return {
guidanceScale: charaCfg.guidance_scale ?? 1.00,
negativePrompt: charaCfg.negative_prompt ?? ''
if (charaCfg.guidance_scale !== 1) {
return {
guidanceScale: charaCfg.guidance_scale,
negativePrompt: charaCfg.negative_prompt
}
}
} else {
return {
guidanceScale: extension_settings.cfg.global.guidance_scale ?? 1.00,
negativePrompt: extension_settings.cfg.global.negative_prompt ?? ''
} else if (extension_settings.cfg.global?.guidance_scale !== 1) {
return {
guidanceScale: extension_settings.cfg.global.guidance_scale,
negativePrompt: extension_settings.cfg.global.negative_prompt
}
}
}

View File

@ -232,7 +232,7 @@ async function generateTextGenWithStreaming(generate_data, signal) {
}
export function getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate) {
const { guidanceScale, negativePrompt } = getCfg();
const cfgValues = getCfg();
return {
'prompt': finalPromt,
@ -251,8 +251,8 @@ export function getTextGenGenerationData(finalPromt, this_amount_gen, isImperson
'penalty_alpha': textgenerationwebui_settings.penalty_alpha,
'length_penalty': textgenerationwebui_settings.length_penalty,
'early_stopping': textgenerationwebui_settings.early_stopping,
'guidance_scale': guidanceScale ?? 1,
'negative_prompt': negativePrompt ?? '',
'guidance_scale': cfgValues?.guidanceScale ?? 1,
'negative_prompt': cfgValues?.negativePrompt ?? '',
'seed': textgenerationwebui_settings.seed,
'add_bos_token': textgenerationwebui_settings.add_bos_token,
'stopping_strings': getStoppingStrings(isImpersonate, false),