mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
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:
@ -2,13 +2,12 @@ import {
|
|||||||
chat_metadata,
|
chat_metadata,
|
||||||
eventSource,
|
eventSource,
|
||||||
event_types,
|
event_types,
|
||||||
getTokenCount,
|
|
||||||
saveSettingsDebounced,
|
saveSettingsDebounced,
|
||||||
this_chid,
|
this_chid,
|
||||||
} from "../../../script.js";
|
} from "../../../script.js";
|
||||||
import { selected_group } from "../../group-chats.js";
|
import { selected_group } from "../../group-chats.js";
|
||||||
import { extension_settings, getContext, saveMetadataDebounced, loadExtensionSettings } from "../../extensions.js";
|
import { extension_settings, saveMetadataDebounced } from "../../extensions.js";
|
||||||
import { getCharaFilename, debounce, waitUntilCondition, delay } from "../../utils.js";
|
import { getCharaFilename, delay } from "../../utils.js";
|
||||||
import { power_user } from "../../power-user.js";
|
import { power_user } from "../../power-user.js";
|
||||||
|
|
||||||
// Keep track of where your extension is located, name should match repo name
|
// Keep track of where your extension is located, name should match repo name
|
||||||
@ -51,7 +50,7 @@ function setCharCfg(tempValue, setting) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let existingCharaCfgIndex;
|
let existingCharaCfgIndex;
|
||||||
@ -66,7 +65,7 @@ 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 ((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);
|
extension_settings.cfg.chara.splice(existingCharaCfgIndex, 1);
|
||||||
}
|
}
|
||||||
} else if (avatarName && tempValue.length > 0) {
|
} else if (avatarName && tempValue.length > 0) {
|
||||||
@ -79,31 +78,46 @@ function setCharCfg(tempValue, setting) {
|
|||||||
extension_settings.cfg.chara.push(tempCharaCfg);
|
extension_settings.cfg.chara.push(tempCharaCfg);
|
||||||
} else {
|
} else {
|
||||||
console.log("Character CFG error: No avatar name key could be found.");
|
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
|
// Don't save settings if something went wrong
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSettings();
|
updateSettings();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCharCfgCheckbox() {
|
function setCharCfgCheckbox() {
|
||||||
const value = !!$(this).prop('checked');
|
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) {
|
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();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
saveMetadataDebounced();
|
||||||
|
|
||||||
function setChatCfgScale() {
|
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Only change CFG when character is selected
|
// TODO: Only change CFG when character is selected
|
||||||
@ -160,6 +174,10 @@ function onChatChanged() {
|
|||||||
|
|
||||||
// Reloads chat-specific settings
|
// Reloads chat-specific settings
|
||||||
function loadSettings() {
|
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
|
// Set character CFG if it exists
|
||||||
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
|
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
|
||||||
@ -221,8 +239,24 @@ jQuery(async () => {
|
|||||||
setTimeout(function () { $('#cfgConfig').hide() }, 200);
|
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() {
|
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() {
|
windowHtml.find('#chara_cfg_negative_prompt').on('input', function() {
|
||||||
|
@ -1,18 +1,33 @@
|
|||||||
|
import { chat_metadata } from "../../../script.js";
|
||||||
import { extension_settings } from "../../extensions.js"
|
import { extension_settings } from "../../extensions.js"
|
||||||
import { getCharaFilename } from "../../utils.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() {
|
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());
|
const charaCfg = extension_settings.cfg.chara.find((e) => e.name === getCharaFilename());
|
||||||
if (charaCfg && charaCfg?.useChara) {
|
if (charaCfg && charaCfg?.useChara) {
|
||||||
return {
|
if (charaCfg.guidance_scale !== 1) {
|
||||||
guidanceScale: charaCfg.guidance_scale ?? 1.00,
|
return {
|
||||||
negativePrompt: charaCfg.negative_prompt ?? ''
|
guidanceScale: charaCfg.guidance_scale,
|
||||||
|
negativePrompt: charaCfg.negative_prompt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if (extension_settings.cfg.global?.guidance_scale !== 1) {
|
||||||
return {
|
return {
|
||||||
guidanceScale: extension_settings.cfg.global.guidance_scale ?? 1.00,
|
guidanceScale: extension_settings.cfg.global.guidance_scale,
|
||||||
negativePrompt: extension_settings.cfg.global.negative_prompt ?? ''
|
negativePrompt: extension_settings.cfg.global.negative_prompt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -232,7 +232,7 @@ async function generateTextGenWithStreaming(generate_data, signal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate) {
|
export function getTextGenGenerationData(finalPromt, this_amount_gen, isImpersonate) {
|
||||||
const { guidanceScale, negativePrompt } = getCfg();
|
const cfgValues = getCfg();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'prompt': finalPromt,
|
'prompt': finalPromt,
|
||||||
@ -251,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': guidanceScale ?? 1,
|
'guidance_scale': cfgValues?.guidanceScale ?? 1,
|
||||||
'negative_prompt': negativePrompt ?? '',
|
'negative_prompt': cfgValues?.negativePrompt ?? '',
|
||||||
'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),
|
||||||
|
Reference in New Issue
Block a user