CFG: Add insertion depth and custom separator

Insertion depth allows for CFG to variably inject itself into the
negative prompt. This is similar to how Author's note works.

However, this method of insertion depth conflicts with AN and
world info where negatives can be meshed between two lines
of those specific insertions.

A custom separator must be wrapped in quotes, otherwise the default
separator is a newline for negative cascading.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2023-08-19 01:19:22 -04:00
parent cdbca6d9fd
commit 7191f7a8ad
6 changed files with 79 additions and 45 deletions

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 { selected_group } from "../../group-chats.js";
import { getCharaFilename } from "../../utils.js";
@ -13,13 +13,14 @@ export const metadataKeys = {
negative_prompt: "cfg_negative_prompt",
negative_combine: "cfg_negative_combine",
groupchat_individual_chars: "cfg_groupchat_individual_chars",
negative_insertion_depth: "cfg_negative_insertion_depth"
negative_insertion_depth: "cfg_negative_insertion_depth",
negative_separator: "cfg_negative_separator"
}
// Gets the CFG value from hierarchy of chat -> character -> global
// Returns undefined values which should be handled in the respective backend APIs
// TODO: Include a custom negative separator
// TODO: Maybe use existing prompt building/substitution?
// TODO: Insertion depth conflicts with author's note. Shouldn't matter though since CFG is prompt mixing.
export function getCfg(prompt) {
const splitPrompt = prompt?.split("\n") ?? [];
let splitNegativePrompt = [];
@ -28,23 +29,24 @@ export function getCfg(prompt) {
const chatNegativeCombine = chat_metadata[metadataKeys.negative_combine] ?? [];
// If there's a guidance scale, continue. Otherwise assume undefined
// TODO: Run substitute params
if (guidanceScale?.value && guidanceScale?.value !== 1) {
if (guidanceScale.type === cfgType.chat || chatNegativeCombine.includes(cfgType.chat)) {
splitNegativePrompt.push(chat_metadata[metadataKeys.negative_prompt]?.trim());
splitNegativePrompt.unshift(substituteParams(chat_metadata[metadataKeys.negative_prompt])?.trim());
}
if (guidanceScale.type === cfgType.chara || chatNegativeCombine.includes(cfgType.chara)) {
splitNegativePrompt.push(charaCfg.negative_prompt?.trim())
splitNegativePrompt.unshift(substituteParams(charaCfg.negative_prompt)?.trim())
}
if (guidanceScale.type === cfgType.global || chatNegativeCombine.includes(cfgType.global)) {
splitNegativePrompt.push(extension_settings.cfg.global.negative_prompt?.trim());
splitNegativePrompt.unshift(substituteParams(extension_settings.cfg.global.negative_prompt)?.trim());
}
// TODO: use a custom separator for join
const combinedNegatives = splitNegativePrompt.filter((e) => e.length > 0).join("\n");
// This line is a bit hacky with a JSON.stringify and JSON.parse. Fix this if possible.
const negativeSeparator = JSON.parse(chat_metadata[metadataKeys.negative_separator] || JSON.stringify("\n")) ?? "\n";
const combinedNegatives = splitNegativePrompt.filter((e) => e.length > 0).join(negativeSeparator);
const insertionDepth = chat_metadata[metadataKeys.negative_insertion_depth] ?? 1;
console.log(insertionDepth)
splitPrompt.splice(splitPrompt.length - insertionDepth, 0, combinedNegatives);
console.log(`Setting CFG with guidance scale: ${guidanceScale.value}, negatives: ${combinedNegatives}`);
@ -78,12 +80,3 @@ function getGuidanceScale(charaCfg) {
value: extension_settings.cfg.global.guidance_scale
};
}
export function getNegativePrompt(prompt) {
const splitPrompt = prompt.split("\n");
const insertionDepth = chat_metadata[metadataKeys.negative_insertion_depth] ?? 1;
splitPrompt.splice(splitPrompt.length - insertionDepth, 0, "Test negative list");
console.log(splitPrompt);
const negativePrompt = splitPrompt.join("\n");
//console.log(negativePrompt);
}