2023-08-19 07:19:22 +02:00
|
|
|
import { chat_metadata, substituteParams, this_chid } from "../../../script.js";
|
2023-08-13 04:43:00 +02:00
|
|
|
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-20 08:19:25 +02:00
|
|
|
positive_prompt: "cfg_positive_prompt",
|
|
|
|
prompt_combine: "cfg_prompt_combine",
|
2023-08-18 05:51:17 +02:00
|
|
|
groupchat_individual_chars: "cfg_groupchat_individual_chars",
|
2023-08-20 08:19:25 +02:00
|
|
|
prompt_insertion_depth: "cfg_prompt_insertion_depth",
|
|
|
|
prompt_separator: "cfg_prompt_separator"
|
2023-08-12 22:17:35 +02:00
|
|
|
}
|
2023-08-11 07:35:22 +02:00
|
|
|
|
2023-08-20 06:44:39 +02:00
|
|
|
// Gets the CFG guidance scale
|
2023-08-20 08:19:25 +02:00
|
|
|
// If the guidance scale is 1, ignore the CFG prompt(s) since it won't be used anyways
|
2023-08-20 06:44:39 +02:00
|
|
|
export function getGuidanceScale() {
|
|
|
|
const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid));
|
2023-08-12 22:17:35 +02:00
|
|
|
const chatGuidanceScale = chat_metadata[metadataKeys.guidance_scale];
|
2023-08-13 04:43:00 +02:00
|
|
|
const groupchatCharOverride = chat_metadata[metadataKeys.groupchat_individual_chars] ?? false;
|
2023-08-20 06:44:39 +02:00
|
|
|
|
2023-08-13 04:43:00 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-23 17:27:58 +02:00
|
|
|
if (extension_settings.cfg.global && extension_settings.cfg.global?.guidance_scale !== 1) {
|
|
|
|
return {
|
|
|
|
type: cfgType.global,
|
|
|
|
value: extension_settings.cfg.global.guidance_scale
|
|
|
|
};
|
|
|
|
}
|
2023-08-12 22:17:35 +02:00
|
|
|
}
|
2023-08-20 06:44:39 +02:00
|
|
|
|
2023-08-20 08:19:25 +02:00
|
|
|
// Gets the CFG prompt
|
|
|
|
export function getCfgPrompt(guidanceScale, isNegative) {
|
|
|
|
let splitCfgPrompt = [];
|
2023-08-20 06:44:39 +02:00
|
|
|
|
2023-08-20 08:19:25 +02:00
|
|
|
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]
|
|
|
|
)
|
|
|
|
);
|
2023-08-20 06:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid));
|
2023-08-20 08:19:25 +02:00
|
|
|
if (guidanceScale.type === cfgType.chara || cfgPromptCombine.includes(cfgType.chara)) {
|
|
|
|
splitCfgPrompt.unshift(
|
|
|
|
substituteParams(
|
|
|
|
isNegative ? charaCfg.negative_prompt : charaCfg.positive_prompt
|
|
|
|
)
|
|
|
|
);
|
2023-08-20 06:44:39 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 08:19:25 +02:00
|
|
|
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
|
|
|
|
)
|
|
|
|
);
|
2023-08-20 06:44:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This line is a bit hacky with a JSON.stringify and JSON.parse. Fix this if possible.
|
2023-08-20 08:19:25 +02:00
|
|
|
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}`);
|
2023-08-20 06:44:39 +02:00
|
|
|
|
|
|
|
return {
|
2023-08-20 08:19:25 +02:00
|
|
|
value: combinedCfgPrompt,
|
2023-08-20 06:44:39 +02:00
|
|
|
depth: insertionDepth
|
|
|
|
};
|
|
|
|
}
|