From a4ab8989339da5b131336a76cdb88a8c904eb623 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 21 Oct 2023 22:19:49 +0300 Subject: [PATCH] Move CFG scale to built-in functionality --- public/index.html | 180 +++++++++++++++++- public/script.js | 7 +- .../{extensions/cfg/index.js => cfg-scale.js} | 178 +++++++++++++---- public/scripts/extensions/cfg/manifest.json | 11 -- public/scripts/extensions/cfg/menuButton.html | 4 - public/scripts/extensions/cfg/style.css | 0 public/scripts/extensions/cfg/util.js | 95 --------- public/scripts/extensions/cfg/window.html | 172 ----------------- public/scripts/nai-settings.js | 2 +- 9 files changed, 321 insertions(+), 328 deletions(-) rename public/scripts/{extensions/cfg/index.js => cfg-scale.js} (69%) delete mode 100644 public/scripts/extensions/cfg/manifest.json delete mode 100644 public/scripts/extensions/cfg/menuButton.html delete mode 100644 public/scripts/extensions/cfg/style.css delete mode 100644 public/scripts/extensions/cfg/util.js delete mode 100644 public/scripts/extensions/cfg/window.html diff --git a/public/index.html b/public/index.html index fd213d166..4dfa4d670 100644 --- a/public/index.html +++ b/public/index.html @@ -88,6 +88,7 @@ + SillyTavern @@ -4751,6 +4752,178 @@ +
+
+
+
+
+
+
+
+
+ Chat CFG +
+
+
+ + Unique to this chat.
+
+ +
+
+ +
+
+
+ select +
+
+
+
+ + + + + +
+
+ +
+
+
+
+ +
+
+
+
+ Global CFG +
+
+
+ Will be used as the default CFG options for every chat unless overridden. +
+ +
+
+ +
+
+
+ select +
+
+
+
+ + + + + +
+
+
+
+
+
+
+
+ CFG Prompt Cascading +
+
+
+
+ + Combine positive/negative prompts from other boxes. +
+ For example, ticking the chat, global, and character boxes combine all negative prompts into a comma-separated string. +
+
+
+
+ + + + +
+
+ + +
+
+
+
+
+
@@ -4790,9 +4963,10 @@ Author's Note -
- -
+ + + CFG Scale + Back to parent chat diff --git a/public/script.js b/public/script.js index 32acd97d7..3582b86d5 100644 --- a/public/script.js +++ b/public/script.js @@ -169,7 +169,7 @@ import { getDeviceInfo } from "./scripts/RossAscends-mods.js"; import { registerPromptManagerMigration } from "./scripts/PromptManager.js"; import { getRegexedString, regex_placement } from "./scripts/extensions/regex/engine.js"; import { FILTER_TYPES, FilterHelper } from "./scripts/filters.js"; -import { getCfgPrompt, getGuidanceScale } from "./scripts/extensions/cfg/util.js"; +import { getCfgPrompt, getGuidanceScale, initCfg } from "./scripts/cfg-scale.js"; import { force_output_sequence, formatInstructModeChat, @@ -724,6 +724,7 @@ async function firstLoadInit() { initPersonas(); initRossMods(); initStats(); + initCfg(); } function checkOnlineStatus() { @@ -3017,7 +3018,9 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, : ` ${cfgPrompt.value}`; } else { // TODO: Make all extension prompts use an array/splice method - finalMesSend[mesSend.length - cfgPrompt.depth].extensionPrompts.push(`${cfgPrompt.value}\n`); + const lengthDiff = mesSend.length - cfgPrompt.depth; + const cfgDepth = lengthDiff >= 0 ? lengthDiff : 0; + finalMesSend[cfgDepth].extensionPrompts.push(`${cfgPrompt.value}\n`); } } diff --git a/public/scripts/extensions/cfg/index.js b/public/scripts/cfg-scale.js similarity index 69% rename from public/scripts/extensions/cfg/index.js rename to public/scripts/cfg-scale.js index 238c6c5d6..8849c1418 100644 --- a/public/scripts/extensions/cfg/index.js +++ b/public/scripts/cfg-scale.js @@ -1,19 +1,17 @@ import { chat_metadata, + substituteParams, + this_chid, eventSource, event_types, saveSettingsDebounced, - this_chid, -} from "../../../script.js"; -import { selected_group } from "../../group-chats.js"; -import { extension_settings, saveMetadataDebounced } from "../../extensions.js"; -import { getCharaFilename, delay } from "../../utils.js"; -import { power_user } from "../../power-user.js"; -import { metadataKeys } from "./util.js"; +} from "../script.js"; +import { extension_settings, saveMetadataDebounced } from "./extensions.js" +import { selected_group } from "./group-chats.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 -const extensionName = "cfg"; -const extensionFolderPath = `scripts/extensions/${extensionName}`; +const extensionName = 'cfg'; const defaultSettings = { global: { "guidance_scale": 1, @@ -199,7 +197,7 @@ function loadSettings() { if (!promptSeparator.startsWith(`"`)) { promptSeparatorDisplay.unshift(`"`); } - + if (!promptSeparator.endsWith(`"`)) { promptSeparatorDisplay.push(`"`); } @@ -279,14 +277,8 @@ function migrateSettings() { } // This function is called when the extension is loaded -jQuery(async () => { - // This is an example of loading HTML from a file - const windowHtml = $(await $.get(`${extensionFolderPath}/window.html`)); - - // Append settingsHtml to extensions_settings - // extension_settings and extensions_settings2 are the left and right columns of the settings menu - // Left should be extensions that deal with system functions and right should be visual/UI related - windowHtml.find('#CFGClose').on('click', function () { +export function initCfg() { + $('#CFGClose').on('click', function () { $("#cfgConfig").transition({ opacity: 0, duration: 200, @@ -295,7 +287,7 @@ jQuery(async () => { setTimeout(function () { $('#cfgConfig').hide() }, 200); }); - windowHtml.find('#chat_cfg_guidance_scale').on('input', function() { + $('#chat_cfg_guidance_scale').on('input', function() { const numberValue = Number($(this).val()); const success = setChatCfg(numberValue, settingType.guidance_scale); if (success) { @@ -303,15 +295,15 @@ jQuery(async () => { } }); - windowHtml.find('#chat_cfg_negative_prompt').on('input', function() { + $('#chat_cfg_negative_prompt').on('input', function() { setChatCfg($(this).val(), settingType.negative_prompt); }); - windowHtml.find('#chat_cfg_positive_prompt').on('input', function() { + $('#chat_cfg_positive_prompt').on('input', function() { setChatCfg($(this).val(), settingType.positive_prompt); }); - windowHtml.find('#chara_cfg_guidance_scale').on('input', function() { + $('#chara_cfg_guidance_scale').on('input', function() { const value = $(this).val(); const success = setCharCfg(value, settingType.guidance_scale); if (success) { @@ -319,34 +311,34 @@ jQuery(async () => { } }); - windowHtml.find('#chara_cfg_negative_prompt').on('input', function() { + $('#chara_cfg_negative_prompt').on('input', function() { setCharCfg($(this).val(), settingType.negative_prompt); }); - windowHtml.find('#chara_cfg_positive_prompt').on('input', function() { + $('#chara_cfg_positive_prompt').on('input', function() { setCharCfg($(this).val(), settingType.positive_prompt); }); - windowHtml.find('#global_cfg_guidance_scale').on('input', function() { + $('#global_cfg_guidance_scale').on('input', function() { extension_settings.cfg.global.guidance_scale = Number($(this).val()); $('#global_cfg_guidance_scale_counter').text(extension_settings.cfg.global.guidance_scale.toFixed(2)); saveSettingsDebounced(); }); - windowHtml.find('#global_cfg_negative_prompt').on('input', function() { + $('#global_cfg_negative_prompt').on('input', function() { extension_settings.cfg.global.negative_prompt = $(this).val(); saveSettingsDebounced(); }); - windowHtml.find('#global_cfg_positive_prompt').on('input', function() { + $('#global_cfg_positive_prompt').on('input', function() { extension_settings.cfg.global.positive_prompt = $(this).val(); saveSettingsDebounced(); }); - windowHtml.find(`input[name="cfg_prompt_combine"]`).on('input', function() { - const values = windowHtml.find(`input[name="cfg_prompt_combine"]`) + $(`input[name="cfg_prompt_combine"]`).on('input', function() { + const values = $('#cfgConfig').find(`input[name="cfg_prompt_combine"]`) .filter(":checked") - .map(function() { return parseInt($(this).val()) }) + .map(function() { return Number($(this).val()) }) .get() .filter((e) => !Number.isNaN(e)) || []; @@ -354,17 +346,17 @@ jQuery(async () => { saveMetadataDebounced(); }); - windowHtml.find(`#cfg_prompt_insertion_depth`).on('input', function() { + $(`#cfg_prompt_insertion_depth`).on('input', function() { chat_metadata[metadataKeys.prompt_insertion_depth] = Number($(this).val()); saveMetadataDebounced(); }); - windowHtml.find(`#cfg_prompt_separator`).on('input', function() { + $(`#cfg_prompt_separator`).on('input', function() { chat_metadata[metadataKeys.prompt_separator] = $(this).val(); saveMetadataDebounced(); }); - windowHtml.find('#groupchat_cfg_use_chara').on('input', function() { + $('#groupchat_cfg_use_chara').on('input', function() { const checked = !!$(this).prop('checked'); chat_metadata[metadataKeys.groupchat_individual_chars] = checked @@ -375,20 +367,126 @@ jQuery(async () => { saveMetadataDebounced(); }); - $("#movingDivs").append(windowHtml); - initialLoadSettings(); if (extension_settings.cfg) { migrateSettings(); } - const buttonHtml = $(await $.get(`${extensionFolderPath}/menuButton.html`)); - buttonHtml.on('click', onCfgMenuItemClick) - buttonHtml.appendTo("#options_advanced"); + $('#option_toggle_CFG').on('click', onCfgMenuItemClick); // Hook events eventSource.on(event_types.CHAT_CHANGED, async () => { await onChatChanged(); }); -}); +} + +export const cfgType = { + chat: 0, + chara: 1, + global: 2 +} + +export const metadataKeys = { + guidance_scale: "cfg_guidance_scale", + negative_prompt: "cfg_negative_prompt", + positive_prompt: "cfg_positive_prompt", + prompt_combine: "cfg_prompt_combine", + groupchat_individual_chars: "cfg_groupchat_individual_chars", + prompt_insertion_depth: "cfg_prompt_insertion_depth", + prompt_separator: "cfg_prompt_separator" +} + +// Gets the CFG guidance scale +// If the guidance scale is 1, ignore the CFG prompt(s) since it won't be used anyways +export function getGuidanceScale() { + if (!extension_settings.cfg) { + console.warn("CFG extension is not enabled. Skipping CFG guidance."); + return; + } + + const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid)); + const chatGuidanceScale = chat_metadata[metadataKeys.guidance_scale]; + const groupchatCharOverride = chat_metadata[metadataKeys.groupchat_individual_chars] ?? false; + + if (chatGuidanceScale && chatGuidanceScale !== 1 && !groupchatCharOverride) { + return { + type: cfgType.chat, + value: chatGuidanceScale + }; + } + + if ((!selected_group && charaCfg || groupchatCharOverride) && charaCfg?.guidance_scale !== 1) { + return { + type: cfgType.chara, + value: charaCfg.guidance_scale + }; + } + + if (extension_settings.cfg.global && extension_settings.cfg.global?.guidance_scale !== 1) { + return { + type: cfgType.global, + value: extension_settings.cfg.global.guidance_scale + }; + } +} + +/** + * Gets the CFG prompt separator. + * @returns {string} The CFG prompt separator + */ +function getCustomSeparator() { + const defaultSeparator = "\n"; + + try { + if (chat_metadata[metadataKeys.prompt_separator]) { + return JSON.parse(chat_metadata[metadataKeys.prompt_separator]); + } + + return defaultSeparator; + } catch { + console.warn("Invalid JSON detected for prompt separator. Using default separator."); + return defaultSeparator; + } +} + +// Gets the CFG prompt +export function getCfgPrompt(guidanceScale, isNegative) { + let splitCfgPrompt = []; + + 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] + ) + ); + } + + const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid)); + if (guidanceScale.type === cfgType.chara || cfgPromptCombine.includes(cfgType.chara)) { + splitCfgPrompt.unshift( + substituteParams( + isNegative ? charaCfg.negative_prompt : charaCfg.positive_prompt + ) + ); + } + + 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 + ) + ); + } + + const customSeparator = getCustomSeparator(); + 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}`); + + return { + value: combinedCfgPrompt, + depth: insertionDepth + }; +} diff --git a/public/scripts/extensions/cfg/manifest.json b/public/scripts/extensions/cfg/manifest.json deleted file mode 100644 index 123f9e10a..000000000 --- a/public/scripts/extensions/cfg/manifest.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "display_name": "CFG", - "loading_order": 1, - "requires": [], - "optional": [], - "js": "index.js", - "css": "style.css", - "author": "kingbri", - "version": "1.0.0", - "homePage": "https://github.com/SillyTavern/SillyTavern" -} diff --git a/public/scripts/extensions/cfg/menuButton.html b/public/scripts/extensions/cfg/menuButton.html deleted file mode 100644 index d407540a6..000000000 --- a/public/scripts/extensions/cfg/menuButton.html +++ /dev/null @@ -1,4 +0,0 @@ - - - CFG Scale - diff --git a/public/scripts/extensions/cfg/style.css b/public/scripts/extensions/cfg/style.css deleted file mode 100644 index e69de29bb..000000000 diff --git a/public/scripts/extensions/cfg/util.js b/public/scripts/extensions/cfg/util.js deleted file mode 100644 index 01bbec92d..000000000 --- a/public/scripts/extensions/cfg/util.js +++ /dev/null @@ -1,95 +0,0 @@ -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"; - -export const cfgType = { - chat: 0, - chara: 1, - global: 2 -} -export const metadataKeys = { - guidance_scale: "cfg_guidance_scale", - negative_prompt: "cfg_negative_prompt", - positive_prompt: "cfg_positive_prompt", - prompt_combine: "cfg_prompt_combine", - groupchat_individual_chars: "cfg_groupchat_individual_chars", - prompt_insertion_depth: "cfg_prompt_insertion_depth", - prompt_separator: "cfg_prompt_separator" -} - -// Gets the CFG guidance scale -// If the guidance scale is 1, ignore the CFG prompt(s) since it won't be used anyways -export function getGuidanceScale() { - if (!extension_settings.cfg) { - console.warn("CFG extension is not enabled. Skipping CFG guidance."); - return; - } - - const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid)); - const chatGuidanceScale = chat_metadata[metadataKeys.guidance_scale]; - const groupchatCharOverride = chat_metadata[metadataKeys.groupchat_individual_chars] ?? false; - - if (chatGuidanceScale && chatGuidanceScale !== 1 && !groupchatCharOverride) { - return { - type: cfgType.chat, - value: chatGuidanceScale - }; - } - - if ((!selected_group && charaCfg || groupchatCharOverride) && charaCfg?.guidance_scale !== 1) { - return { - type: cfgType.chara, - value: charaCfg.guidance_scale - }; - } - - if (extension_settings.cfg.global && extension_settings.cfg.global?.guidance_scale !== 1) { - return { - type: cfgType.global, - value: extension_settings.cfg.global.guidance_scale - }; - } -} - -// Gets the CFG prompt -export function getCfgPrompt(guidanceScale, isNegative) { - let splitCfgPrompt = []; - - 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] - ) - ); - } - - const charaCfg = extension_settings.cfg.chara?.find((e) => e.name === getCharaFilename(this_chid)); - if (guidanceScale.type === cfgType.chara || cfgPromptCombine.includes(cfgType.chara)) { - splitCfgPrompt.unshift( - substituteParams( - isNegative ? charaCfg.negative_prompt : charaCfg.positive_prompt - ) - ); - } - - 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 - ) - ); - } - - // This line is a bit hacky with a JSON.stringify and JSON.parse. Fix this if possible. - 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}`); - - return { - value: combinedCfgPrompt, - depth: insertionDepth - }; -} diff --git a/public/scripts/extensions/cfg/window.html b/public/scripts/extensions/cfg/window.html deleted file mode 100644 index 6ff886fb3..000000000 --- a/public/scripts/extensions/cfg/window.html +++ /dev/null @@ -1,172 +0,0 @@ -
-
-
-
-
-
-
-
-
- Chat CFG -
-
-
- - Unique to this chat.
-
- -
-
- -
-
-
- select -
-
-
-
- - - - - -
-
- -
-
-
-
- -
-
-
-
- Global CFG -
-
-
- Will be used as the default CFG options for every chat unless overridden. -
- -
-
- -
-
-
- select -
-
-
-
- - - - - -
-
-
-
-
-
-
-
- CFG Prompt Cascading -
-
-
-
- - Combine positive/negative prompts from other boxes. -
- For example, ticking the chat, global, and character boxes combine all negative prompts into a comma-separated string. -
-
-
-
- - - - -
-
- - -
-
-
-
-
-
diff --git a/public/scripts/nai-settings.js b/public/scripts/nai-settings.js index dba24b4ea..c5575922a 100644 --- a/public/scripts/nai-settings.js +++ b/public/scripts/nai-settings.js @@ -5,7 +5,7 @@ import { saveSettingsDebounced, setGenerationParamsFromPreset } from "../script.js"; -import { getCfgPrompt } from "./extensions/cfg/util.js"; +import { getCfgPrompt } from "./cfg-scale.js"; import { MAX_CONTEXT_DEFAULT } from "./power-user.js"; import { getTextTokens, tokenizers } from "./tokenizers.js"; import {