2023-08-13 04:43:00 +02:00
|
|
|
import { chat_metadata, this_chid } from "../../../script.js";
|
|
|
|
import { extension_settings, getContext } from "../../extensions.js"
|
|
|
|
import { selected_group } from "../../group-chats.js";
|
2023-08-10 05:42:04 +02:00
|
|
|
import { getCharaFilename } from "../../utils.js";
|
2023-08-09 05:22:25 +02:00
|
|
|
|
2023-08-12 22:17:35 +02:00
|
|
|
export const cfgType = {
|
|
|
|
chat: 0,
|
|
|
|
chara: 1,
|
|
|
|
global: 2
|
|
|
|
}
|
|
|
|
export const metadataKeys = {
|
|
|
|
guidance_scale: "cfg_guidance_scale",
|
|
|
|
negative_prompt: "cfg_negative_prompt",
|
2023-08-13 04:43:00 +02:00
|
|
|
negative_combine: "cfg_negative_combine",
|
|
|
|
groupchat_individual_chars: "cfg_groupchat_individual_chars"
|
2023-08-12 22:17:35 +02:00
|
|
|
}
|
2023-08-11 07:35:22 +02:00
|
|
|
|
2023-08-12 22:17:35 +02:00
|
|
|
// Gets the CFG value from hierarchy of chat -> character -> global
|
|
|
|
// Returns undefined values which should be handled in the respective backend APIs
|
2023-08-09 05:22:25 +02:00
|
|
|
export function getCfg() {
|
2023-08-12 22:17:35 +02:00
|
|
|
let splitNegativePrompt = [];
|
2023-08-13 04:43:00 +02:00
|
|
|
const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid));
|
2023-08-12 22:17:35 +02:00
|
|
|
const guidanceScale = getGuidanceScale(charaCfg);
|
2023-08-13 04:43:00 +02:00
|
|
|
const chatNegativeCombine = chat_metadata[metadataKeys.negative_combine] ?? [];
|
2023-08-12 22:17:35 +02:00
|
|
|
|
|
|
|
// 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());
|
2023-08-11 07:35:22 +02:00
|
|
|
}
|
|
|
|
|
2023-08-12 22:17:35 +02:00
|
|
|
if (guidanceScale.type === cfgType.chara || chatNegativeCombine.includes(cfgType.chara)) {
|
|
|
|
splitNegativePrompt.push(charaCfg.negative_prompt?.trim())
|
2023-08-10 05:42:04 +02:00
|
|
|
}
|
2023-08-12 22:17:35 +02:00
|
|
|
|
|
|
|
if (guidanceScale.type === cfgType.global || chatNegativeCombine.includes(cfgType.global)) {
|
|
|
|
splitNegativePrompt.push(extension_settings.cfg.global.negative_prompt?.trim());
|
|
|
|
}
|
|
|
|
|
2023-08-13 04:43:00 +02:00
|
|
|
const combinedNegatives = splitNegativePrompt.filter((e) => e.length > 0).join(", ");
|
|
|
|
console.debug(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedNegatives}`)
|
|
|
|
|
2023-08-11 07:35:22 +02:00
|
|
|
return {
|
2023-08-12 22:17:35 +02:00
|
|
|
guidanceScale: guidanceScale.value,
|
2023-08-13 04:43:00 +02:00
|
|
|
negativePrompt: combinedNegatives
|
2023-08-10 05:42:04 +02:00
|
|
|
}
|
2023-08-09 05:22:25 +02:00
|
|
|
}
|
2023-08-10 05:42:04 +02:00
|
|
|
}
|
2023-08-12 22:17:35 +02:00
|
|
|
|
|
|
|
// 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];
|
2023-08-13 04:43:00 +02:00
|
|
|
const groupchatCharOverride = chat_metadata[metadataKeys.groupchat_individual_chars] ?? false;
|
|
|
|
if (chatGuidanceScale && chatGuidanceScale !== 1 && !groupchatCharOverride) {
|
2023-08-12 22:17:35 +02:00
|
|
|
return {
|
|
|
|
type: cfgType.chat,
|
|
|
|
value: chatGuidanceScale
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-13 04:43:00 +02:00
|
|
|
if ((!selected_group && charaCfg || groupchatCharOverride) && charaCfg?.guidance_scale !== 1) {
|
2023-08-12 22:17:35 +02:00
|
|
|
return {
|
|
|
|
type: cfgType.chara,
|
|
|
|
value: charaCfg.guidance_scale
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: cfgType.global,
|
|
|
|
value: extension_settings.cfg.global.guidance_scale
|
|
|
|
};
|
|
|
|
}
|