From 607df2f5550a4356be22fce87a51f46d3c9fa1f1 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sat, 23 Mar 2024 17:36:43 +0200
Subject: [PATCH 01/14] Add ability to insert role-typed prompt injections
---
public/index.html | 28 ++++-
public/script.js | 233 ++++++++++++++++++++---------------
public/scripts/openai.js | 16 ++-
public/scripts/world-info.js | 38 +++++-
src/endpoints/characters.js | 1 +
5 files changed, 201 insertions(+), 115 deletions(-)
diff --git a/public/index.html b/public/index.html
index 7c82d0b1b..9398adbe4 100644
--- a/public/index.html
+++ b/public/index.html
@@ -4646,12 +4646,28 @@
diff --git a/public/script.js b/public/script.js
index b1485f425..b6157c34a 100644
--- a/public/script.js
+++ b/public/script.js
@@ -2454,7 +2454,7 @@ function addPersonaDescriptionExtensionPrompt() {
? `${power_user.persona_description}\n${originalAN}`
: `${originalAN}\n${power_user.persona_description}`;
- setExtensionPrompt(NOTE_MODULE_NAME, ANWithDesc, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth], extension_settings.note.allowWIScan);
+ setExtensionPrompt(NOTE_MODULE_NAME, ANWithDesc, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth], extension_settings.note.allowWIScan, chat_metadata[metadata_keys.role]);
}
}
diff --git a/public/scripts/authors-note.js b/public/scripts/authors-note.js
index 6642f998b..24e73b278 100644
--- a/public/scripts/authors-note.js
+++ b/public/scripts/authors-note.js
@@ -3,6 +3,7 @@ import {
chat_metadata,
eventSource,
event_types,
+ extension_prompt_roles,
saveSettingsDebounced,
this_chid,
} from '../script.js';
@@ -22,6 +23,7 @@ export const metadata_keys = {
interval: 'note_interval',
depth: 'note_depth',
position: 'note_position',
+ role: 'note_role',
};
const chara_note_position = {
@@ -140,6 +142,16 @@ async function onDefaultIntervalInput() {
saveSettingsDebounced();
}
+function onExtensionFloatingRoleInput(e) {
+ chat_metadata[metadata_keys.role] = Number(e.target.value);
+ updateSettings();
+}
+
+function onExtensionDefaultRoleInput(e) {
+ extension_settings.note.defaultRole = Number(e.target.value);
+ saveSettingsDebounced();
+}
+
async function onExtensionFloatingCharPositionInput(e) {
const value = e.target.value;
const charaNote = extension_settings.note.chara.find((e) => e.name === getCharaFilename());
@@ -217,6 +229,7 @@ function loadSettings() {
const DEFAULT_DEPTH = 4;
const DEFAULT_POSITION = 1;
const DEFAULT_INTERVAL = 1;
+ const DEFAULT_ROLE = extension_prompt_roles.SYSTEM;
if (extension_settings.note.defaultPosition === undefined) {
extension_settings.note.defaultPosition = DEFAULT_POSITION;
@@ -230,14 +243,20 @@ function loadSettings() {
extension_settings.note.defaultInterval = DEFAULT_INTERVAL;
}
+ if (extension_settings.note.defaultRole === undefined) {
+ extension_settings.note.defaultRole = DEFAULT_ROLE;
+ }
+
chat_metadata[metadata_keys.prompt] = chat_metadata[metadata_keys.prompt] ?? extension_settings.note.default ?? '';
chat_metadata[metadata_keys.interval] = chat_metadata[metadata_keys.interval] ?? extension_settings.note.defaultInterval ?? DEFAULT_INTERVAL;
chat_metadata[metadata_keys.position] = chat_metadata[metadata_keys.position] ?? extension_settings.note.defaultPosition ?? DEFAULT_POSITION;
chat_metadata[metadata_keys.depth] = chat_metadata[metadata_keys.depth] ?? extension_settings.note.defaultDepth ?? DEFAULT_DEPTH;
+ chat_metadata[metadata_keys.role] = chat_metadata[metadata_keys.role] ?? extension_settings.note.defaultRole ?? DEFAULT_ROLE;
$('#extension_floating_prompt').val(chat_metadata[metadata_keys.prompt]);
$('#extension_floating_interval').val(chat_metadata[metadata_keys.interval]);
$('#extension_floating_allow_wi_scan').prop('checked', extension_settings.note.allowWIScan ?? false);
$('#extension_floating_depth').val(chat_metadata[metadata_keys.depth]);
+ $('#extension_floating_role').val(chat_metadata[metadata_keys.role]);
$(`input[name="extension_floating_position"][value="${chat_metadata[metadata_keys.position]}"]`).prop('checked', true);
if (extension_settings.note.chara && getContext().characterId) {
@@ -255,6 +274,7 @@ function loadSettings() {
$('#extension_floating_default').val(extension_settings.note.default);
$('#extension_default_depth').val(extension_settings.note.defaultDepth);
$('#extension_default_interval').val(extension_settings.note.defaultInterval);
+ $('#extension_default_role').val(extension_settings.note.defaultRole);
$(`input[name="extension_default_position"][value="${extension_settings.note.defaultPosition}"]`).prop('checked', true);
}
@@ -274,6 +294,10 @@ export function setFloatingPrompt() {
------
lastMessageNumber = ${lastMessageNumber}
metadata_keys.interval = ${chat_metadata[metadata_keys.interval]}
+ metadata_keys.position = ${chat_metadata[metadata_keys.position]}
+ metadata_keys.depth = ${chat_metadata[metadata_keys.depth]}
+ metadata_keys.role = ${chat_metadata[metadata_keys.role]}
+ ------
`);
// interval 1 should be inserted no matter what
@@ -313,7 +337,14 @@ export function setFloatingPrompt() {
}
}
}
- context.setExtensionPrompt(MODULE_NAME, prompt, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth], extension_settings.note.allowWIScan);
+ context.setExtensionPrompt(
+ MODULE_NAME,
+ prompt,
+ chat_metadata[metadata_keys.position],
+ chat_metadata[metadata_keys.depth],
+ extension_settings.note.allowWIScan,
+ chat_metadata[metadata_keys.role],
+ );
$('#extension_floating_counter').text(shouldAddPrompt ? '0' : messagesTillInsertion);
}
@@ -410,6 +441,8 @@ export function initAuthorsNote() {
$('#extension_default_depth').on('input', onDefaultDepthInput);
$('#extension_default_interval').on('input', onDefaultIntervalInput);
$('#extension_floating_allow_wi_scan').on('input', onAllowWIScanCheckboxChanged);
+ $('#extension_floating_role').on('input', onExtensionFloatingRoleInput);
+ $('#extension_default_role').on('input', onExtensionDefaultRoleInput);
$('input[name="extension_floating_position"]').on('change', onExtensionFloatingPositionInput);
$('input[name="extension_default_position"]').on('change', onDefaultPositionInput);
$('input[name="extension_floating_char_position"]').on('change', onExtensionFloatingCharPositionInput);
diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js
index 5ffd9e3c3..29582a1f0 100644
--- a/public/scripts/world-info.js
+++ b/public/scripts/world-info.js
@@ -2285,7 +2285,7 @@ async function checkWorldInfo(chat, maxContext) {
if (shouldWIAddPrompt) {
const originalAN = context.extensionPrompts[NOTE_MODULE_NAME].value;
const ANWithWI = `${ANTopEntries.join('\n')}\n${originalAN}\n${ANBottomEntries.join('\n')}`;
- context.setExtensionPrompt(NOTE_MODULE_NAME, ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth], extension_settings.note.allowWIScan);
+ context.setExtensionPrompt(NOTE_MODULE_NAME, ANWithWI, chat_metadata[metadata_keys.position], chat_metadata[metadata_keys.depth], extension_settings.note.allowWIScan, chat_metadata[metadata_keys.role]);
}
return { worldInfoBefore, worldInfoAfter, WIDepthEntries, allActivatedEntries };
From 67e78fa4567dd00e3e09750830b4f479f09f1825 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sat, 23 Mar 2024 19:18:43 +0200
Subject: [PATCH 05/14] Add roles to summary injects
---
public/scripts/extensions/memory/index.js | 23 ++++++++++++++++++++---
public/scripts/openai.js | 22 ++++++++++++++++++++--
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/public/scripts/extensions/memory/index.js b/public/scripts/extensions/memory/index.js
index 778ef5caa..19003052e 100644
--- a/public/scripts/extensions/memory/index.js
+++ b/public/scripts/extensions/memory/index.js
@@ -1,6 +1,6 @@
import { getStringHash, debounce, waitUntilCondition, extractAllWords } from '../../utils.js';
import { getContext, getApiUrl, extension_settings, doExtrasFetch, modules } from '../../extensions.js';
-import { animation_duration, eventSource, event_types, extension_prompt_types, generateQuietPrompt, is_send_press, saveSettingsDebounced, substituteParams } from '../../../script.js';
+import { animation_duration, eventSource, event_types, extension_prompt_roles, extension_prompt_types, generateQuietPrompt, is_send_press, saveSettingsDebounced, substituteParams } from '../../../script.js';
import { is_group_generating, selected_group } from '../../group-chats.js';
import { registerSlashCommand } from '../../slash-commands.js';
import { loadMovingUIState } from '../../power-user.js';
@@ -49,6 +49,7 @@ const defaultSettings = {
prompt: defaultPrompt,
template: defaultTemplate,
position: extension_prompt_types.IN_PROMPT,
+ role: extension_prompt_roles.SYSTEM,
depth: 2,
promptWords: 200,
promptMinWords: 25,
@@ -83,6 +84,7 @@ function loadSettings() {
$('#memory_prompt_interval').val(extension_settings.memory.promptInterval).trigger('input');
$('#memory_template').val(extension_settings.memory.template).trigger('input');
$('#memory_depth').val(extension_settings.memory.depth).trigger('input');
+ $('#memory_role').val(extension_settings.memory.role).trigger('input');
$(`input[name="memory_position"][value="${extension_settings.memory.position}"]`).prop('checked', true).trigger('input');
$('#memory_prompt_words_force').val(extension_settings.memory.promptForceWords).trigger('input');
switchSourceControls(extension_settings.memory.source);
@@ -148,6 +150,13 @@ function onMemoryDepthInput() {
saveSettingsDebounced();
}
+function onMemoryRoleInput() {
+ const value = $(this).val();
+ extension_settings.memory.role = Number(value);
+ reinsertMemory();
+ saveSettingsDebounced();
+}
+
function onMemoryPositionChange(e) {
const value = e.target.value;
extension_settings.memory.position = value;
@@ -480,11 +489,12 @@ function reinsertMemory() {
function setMemoryContext(value, saveToMessage) {
const context = getContext();
- context.setExtensionPrompt(MODULE_NAME, formatMemoryValue(value), extension_settings.memory.position, extension_settings.memory.depth);
+ context.setExtensionPrompt(MODULE_NAME, formatMemoryValue(value), extension_settings.memory.position, extension_settings.memory.depth, false, extension_settings.memory.role);
$('#memory_contents').val(value);
console.log('Summary set to: ' + value);
console.debug('Position: ' + extension_settings.memory.position);
console.debug('Depth: ' + extension_settings.memory.depth);
+ console.debug('Role: ' + extension_settings.memory.role);
if (saveToMessage && context.chat.length) {
const idx = context.chat.length - 2;
@@ -560,6 +570,7 @@ function setupListeners() {
$('#memory_force_summarize').off('click').on('click', forceSummarizeChat);
$('#memory_template').off('click').on('input', onMemoryTemplateInput);
$('#memory_depth').off('click').on('input', onMemoryDepthInput);
+ $('#memory_role').off('click').on('input', onMemoryRoleInput);
$('input[name="memory_position"]').off('click').on('change', onMemoryPositionChange);
$('#memory_prompt_words_force').off('click').on('input', onMemoryPromptWordsForceInput);
$('#summarySettingsBlockToggle').off('click').on('click', function () {
@@ -620,9 +631,15 @@ jQuery(function () {
+
In-chat @ Depth
+ as
+
+ System
+ User
+ Assistant
+
diff --git a/public/scripts/openai.js b/public/scripts/openai.js
index bea072da3..5cb42a2bc 100644
--- a/public/scripts/openai.js
+++ b/public/scripts/openai.js
@@ -841,6 +841,24 @@ function getPromptPosition(position) {
return false;
}
+/**
+ * Gets a Chat Completion role based on the prompt role.
+ * @param {number} role Role of the prompt.
+ * @returns {string} Mapped role.
+ */
+function getPromptRole(role) {
+ switch (role) {
+ case extension_prompt_roles.SYSTEM:
+ return 'system';
+ case extension_prompt_roles.USER:
+ return 'user';
+ case extension_prompt_roles.ASSISTANT:
+ return 'assistant';
+ default:
+ return 'system';
+ }
+}
+
/**
* Populate a chat conversation by adding prompts to the conversation and managing system and user prompts.
*
@@ -1020,7 +1038,7 @@ function preparePromptsForChatCompletion({ Scenario, charPersonality, name2, wor
// Tavern Extras - Summary
const summary = extensionPrompts['1_memory'];
if (summary && summary.value) systemPrompts.push({
- role: 'system',
+ role: getPromptRole(summary.role),
content: summary.value,
identifier: 'summary',
position: getPromptPosition(summary.position),
@@ -1029,7 +1047,7 @@ function preparePromptsForChatCompletion({ Scenario, charPersonality, name2, wor
// Authors Note
const authorsNote = extensionPrompts['2_floating_prompt'];
if (authorsNote && authorsNote.value) systemPrompts.push({
- role: 'system',
+ role: getPromptRole(authorsNote.role),
content: authorsNote.value,
identifier: 'authorsNote',
position: getPromptPosition(authorsNote.position),
From 6665666098c11d3b1588f81e9cf06161030be43b Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sat, 23 Mar 2024 23:02:42 +0200
Subject: [PATCH 06/14] Add continue postfix controls for Chat Completion
---
public/index.html | 34 +++++++++++++++++++-----
public/script.js | 11 +++++---
public/scripts/openai.js | 56 +++++++++++++++++++++++++++++++++++++++-
3 files changed, 91 insertions(+), 10 deletions(-)
diff --git a/public/index.html b/public/index.html
index 714cd032c..165f41fef 100644
--- a/public/index.html
+++ b/public/index.html
@@ -1656,6 +1656,28 @@
+
@@ -5225,9 +5247,9 @@
as
- System
- User
- Assistant
+ System
+ User
+ Assistant
@@ -5310,9 +5332,9 @@
as
- System
- User
- Assistant
+ System
+ User
+ Assistant
diff --git a/public/script.js b/public/script.js
index b6157c34a..3189a0f07 100644
--- a/public/script.js
+++ b/public/script.js
@@ -3258,7 +3258,12 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
let continue_mag = '';
for (let i = coreChat.length - 1, j = 0; i >= 0; i--, j++) {
if (main_api == 'openai') {
- break;
+ chat2[i] = coreChat[j].mes;
+ if (i === 0 && isContinue) {
+ chat2[i] = chat2[i].slice(0, chat2[i].lastIndexOf(coreChat[j].mes) + coreChat[j].mes.length);
+ continue_mag = coreChat[j].mes;
+ }
+ continue;
}
chat2[i] = formatMessageHistoryItem(coreChat[j], isInstruct, false);
@@ -3399,8 +3404,8 @@ async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, qu
// Coping mechanism for OAI spacing
const isForceInstruct = isOpenRouterWithInstruct();
if (main_api === 'openai' && !isForceInstruct && !cyclePrompt.endsWith(' ')) {
- cyclePrompt += ' ';
- continue_mag += ' ';
+ cyclePrompt += oai_settings.continue_postfix;
+ continue_mag += oai_settings.continue_postfix;
}
message_already_generated = continue_mag;
}
diff --git a/public/scripts/openai.js b/public/scripts/openai.js
index 5cb42a2bc..3c75efe51 100644
--- a/public/scripts/openai.js
+++ b/public/scripts/openai.js
@@ -179,6 +179,12 @@ const character_names_behavior = {
CONTENT: 2,
};
+const continue_postfix_types = {
+ SPACE: ' ',
+ NEWLINE: '\n',
+ DOUBLE_NEWLINE: '\n\n',
+};
+
const prefixMap = selected_group ? {
assistant: '',
user: '',
@@ -253,6 +259,7 @@ const default_settings = {
bypass_status_check: false,
continue_prefill: false,
names_behavior: character_names_behavior.NONE,
+ continue_postfix: continue_postfix_types.SPACE,
seed: -1,
n: 1,
};
@@ -320,6 +327,7 @@ const oai_settings = {
bypass_status_check: false,
continue_prefill: false,
names_behavior: character_names_behavior.NONE,
+ continue_postfix: continue_postfix_types.SPACE,
seed: -1,
n: 1,
};
@@ -718,7 +726,7 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
// Reserve budget for continue nudge
let continueMessage = null;
const instruct = isOpenRouterWithInstruct();
- if (type === 'continue' && cyclePrompt && !instruct) {
+ if (type === 'continue' && cyclePrompt && !instruct && !oai_settings.continue_prefill) {
const promptObject = oai_settings.continue_prefill ?
{
identifier: 'continueNudge',
@@ -2600,6 +2608,7 @@ function loadOpenAISettings(data, settings) {
oai_settings.squash_system_messages = settings.squash_system_messages ?? default_settings.squash_system_messages;
oai_settings.continue_prefill = settings.continue_prefill ?? default_settings.continue_prefill;
oai_settings.names_behavior = settings.names_behavior ?? default_settings.names_behavior;
+ oai_settings.continue_postfix = settings.continue_postfix ?? default_settings.continue_postfix;
// Migrate from old settings
if (settings.names_in_completion === true) {
@@ -2716,6 +2725,7 @@ function loadOpenAISettings(data, settings) {
}
setNamesBehaviorControls();
+ setContinuePostfixControls();
$('#chat_completion_source').val(oai_settings.chat_completion_source).trigger('change');
$('#oai_max_context_unlocked').prop('checked', oai_settings.max_context_unlocked);
@@ -2735,6 +2745,27 @@ function setNamesBehaviorControls() {
}
}
+function setContinuePostfixControls() {
+ switch (oai_settings.continue_postfix) {
+ case continue_postfix_types.SPACE:
+ $('#continue_postfix_space').prop('checked', true);
+ break;
+ case continue_postfix_types.NEWLINE:
+ $('#continue_postfix_newline').prop('checked', true);
+ break;
+ case continue_postfix_types.DOUBLE_NEWLINE:
+ $('#continue_postfix_double_newline').prop('checked', true);
+ break;
+ default:
+ // Prevent preset value abuse
+ oai_settings.continue_postfix = continue_postfix_types.SPACE;
+ $('#continue_postfix_space').prop('checked', true);
+ break;
+ }
+
+ $('#continue_postfix').val(oai_settings.continue_postfix);
+}
+
async function getStatusOpen() {
if (oai_settings.chat_completion_source == chat_completion_sources.WINDOWAI) {
let status;
@@ -2891,6 +2922,7 @@ async function saveOpenAIPreset(name, settings, triggerUi = true) {
image_inlining: settings.image_inlining,
bypass_status_check: settings.bypass_status_check,
continue_prefill: settings.continue_prefill,
+ continue_postfix: settings.continue_postfix,
seed: settings.seed,
n: settings.n,
};
@@ -3265,6 +3297,7 @@ function onSettingsPresetChange() {
squash_system_messages: ['#squash_system_messages', 'squash_system_messages', true],
image_inlining: ['#openai_image_inlining', 'image_inlining', true],
continue_prefill: ['#continue_prefill', 'continue_prefill', true],
+ continue_postfix: ['#continue_postfix', 'continue_postfix', false],
seed: ['#seed_openai', 'seed', false],
n: ['#n_openai', 'n', false],
};
@@ -4387,6 +4420,27 @@ $(document).ready(async function () {
saveSettingsDebounced();
});
+ $('#continue_postifx').on('input', function () {
+ oai_settings.continue_postfix = String($(this).val());
+ setContinuePostfixControls();
+ saveSettingsDebounced();
+ });
+
+ $('#continue_postfix_space').on('input', function () {
+ oai_settings.continue_postfix = continue_postfix_types.SPACE;
+ saveSettingsDebounced();
+ });
+
+ $('#continue_postfix_newline').on('input', function () {
+ oai_settings.continue_postfix = continue_postfix_types.NEWLINE;
+ saveSettingsDebounced();
+ });
+
+ $('#continue_postfix_double_newline').on('input', function () {
+ oai_settings.continue_postfix = continue_postfix_types.DOUBLE_NEWLINE;
+ saveSettingsDebounced();
+ });
+
$(document).on('input', '#openai_settings .autoSetHeight', function () {
resetScrollHeight($(this));
});
From 7b9c0e303fcd95e91ed543c89792abb2062e2e32 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sat, 23 Mar 2024 23:11:05 +0200
Subject: [PATCH 07/14] Clean-up continue nudge init
---
public/scripts/openai.js | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
diff --git a/public/scripts/openai.js b/public/scripts/openai.js
index 3c75efe51..1b47caca5 100644
--- a/public/scripts/openai.js
+++ b/public/scripts/openai.js
@@ -727,19 +727,12 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
let continueMessage = null;
const instruct = isOpenRouterWithInstruct();
if (type === 'continue' && cyclePrompt && !instruct && !oai_settings.continue_prefill) {
- const promptObject = oai_settings.continue_prefill ?
- {
- identifier: 'continueNudge',
- role: 'assistant',
- content: cyclePrompt,
- system_prompt: true,
- } :
- {
- identifier: 'continueNudge',
- role: 'system',
- content: oai_settings.continue_nudge_prompt.replace('{{lastChatMessage}}', cyclePrompt),
- system_prompt: true,
- };
+ const promptObject = {
+ identifier: 'continueNudge',
+ role: 'system',
+ content: oai_settings.continue_nudge_prompt.replace('{{lastChatMessage}}', String(cyclePrompt).trim()),
+ system_prompt: true,
+ };
const continuePrompt = new Prompt(promptObject);
const preparedPrompt = promptManager.preparePrompt(continuePrompt);
continueMessage = Message.fromPrompt(preparedPrompt);
@@ -3494,7 +3487,7 @@ async function onModelChange() {
if (oai_settings.chat_completion_source == chat_completion_sources.MAKERSUITE) {
if (oai_settings.max_context_unlocked) {
$('#openai_max_context').attr('max', unlocked_max);
- } else if (value === 'gemini-1.5-pro') {
+ } else if (value === 'gemini-1.5-pro') {
$('#openai_max_context').attr('max', max_1mil);
} else if (value === 'gemini-pro') {
$('#openai_max_context').attr('max', max_32k);
From c1ac34e0019d95a3dbb0cb2c45f5417eeb17db55 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sun, 24 Mar 2024 00:28:54 +0200
Subject: [PATCH 08/14] Disable-able main prompt
---
public/scripts/PromptManager.js | 8 +++++++-
public/scripts/openai.js | 4 ++--
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js
index cc0beac7b..875e2559c 100644
--- a/public/scripts/PromptManager.js
+++ b/public/scripts/PromptManager.js
@@ -884,7 +884,7 @@ class PromptManager {
* @returns {boolean} True if the prompt can be deleted, false otherwise.
*/
isPromptToggleAllowed(prompt) {
- const forceTogglePrompts = ['charDescription', 'charPersonality', 'scenario', 'personaDescription', 'worldInfoBefore', 'worldInfoAfter'];
+ const forceTogglePrompts = ['charDescription', 'charPersonality', 'scenario', 'personaDescription', 'worldInfoBefore', 'worldInfoAfter', 'main'];
return prompt.marker && !forceTogglePrompts.includes(prompt.identifier) ? false : !this.configuration.toggleDisabled.includes(prompt.identifier);
}
@@ -1255,6 +1255,12 @@ class PromptManager {
if (true === entry.enabled) {
const prompt = this.getPromptById(entry.identifier);
if (prompt) promptCollection.add(this.preparePrompt(prompt));
+ } else if (!entry.enabled && entry.identifier === 'main') {
+ // Some extensions require main prompt to be present for relative inserts.
+ // So we make a GMO-free vegan replacement.
+ const prompt = this.getPromptById(entry.identifier);
+ prompt.content = '';
+ if (prompt) promptCollection.add(this.preparePrompt(prompt));
}
});
diff --git a/public/scripts/openai.js b/public/scripts/openai.js
index 1b47caca5..77a0f4954 100644
--- a/public/scripts/openai.js
+++ b/public/scripts/openai.js
@@ -549,7 +549,7 @@ function setupChatCompletionPromptManager(openAiSettings) {
prefix: 'completion_',
containerIdentifier: 'completion_prompt_manager',
listIdentifier: 'completion_prompt_manager_list',
- toggleDisabled: ['main'],
+ toggleDisabled: [],
sortableDelay: getSortableDelay(),
defaultPrompts: {
main: default_main_prompt,
@@ -881,7 +881,7 @@ async function populateChatCompletion(prompts, chatCompletion, { bias, quietProm
// We need the prompts array to determine a position for the source.
if (false === prompts.has(source)) return;
- if (promptManager.isPromptDisabledForActiveCharacter(source)) {
+ if (promptManager.isPromptDisabledForActiveCharacter(source) && source !== 'main') {
promptManager.log(`Skipping prompt ${source} because it is disabled`);
return;
}
From 3b637cc9a6074f2c17e1cde5f32a29423fa2af8a Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Sun, 24 Mar 2024 01:28:35 +0200
Subject: [PATCH 09/14] Add forbid overrides to prompts
---
public/css/promptmanager.css | 10 ++++++---
public/index.html | 18 +++++++++++----
public/scripts/PromptManager.js | 39 ++++++++++++++++++++++++++-------
public/scripts/openai.js | 4 ++--
4 files changed, 54 insertions(+), 17 deletions(-)
diff --git a/public/css/promptmanager.css b/public/css/promptmanager.css
index 8cd6f7357..89e11dbff 100644
--- a/public/css/promptmanager.css
+++ b/public/css/promptmanager.css
@@ -19,13 +19,12 @@
#completion_prompt_manager #completion_prompt_manager_list li {
display: grid;
- grid-template-columns: 4fr 80px 60px;
+ grid-template-columns: 4fr 80px 40px;
margin-bottom: 0.5em;
width: 100%
}
#completion_prompt_manager #completion_prompt_manager_list .completion_prompt_manager_prompt .completion_prompt_manager_prompt_name .fa-solid {
- padding: 0 0.5em;
color: var(--white50a);
}
@@ -40,6 +39,7 @@
#completion_prompt_manager #completion_prompt_manager_list li.completion_prompt_manager_list_head .prompt_manager_prompt_tokens,
#completion_prompt_manager #completion_prompt_manager_list li.completion_prompt_manager_prompt .prompt_manager_prompt_tokens {
+ font-size: calc(var(--mainFontSize)*0.9);
text-align: right;
}
@@ -237,6 +237,10 @@
font-size: 12px;
}
+#completion_prompt_manager .completion_prompt_manager_important a {
+ font-weight: 600;
+}
+
#completion_prompt_manager_footer_append_prompt {
font-size: 16px;
}
@@ -305,4 +309,4 @@
#completion_prompt_manager #completion_prompt_manager_list li.completion_prompt_manager_prompt span span span {
margin-left: 0.5em;
}
-}
\ No newline at end of file
+}
diff --git a/public/index.html b/public/index.html
index 165f41fef..f6adf96de 100644
--- a/public/index.html
+++ b/public/index.html
@@ -4962,10 +4962,20 @@
diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js
index 875e2559c..30e128d46 100644
--- a/public/scripts/PromptManager.js
+++ b/public/scripts/PromptManager.js
@@ -70,7 +70,7 @@ const registerPromptManagerMigration = () => {
* Represents a prompt.
*/
class Prompt {
- identifier; role; content; name; system_prompt; position; injection_position; injection_depth;
+ identifier; role; content; name; system_prompt; position; injection_position; injection_depth; forbid_overrides;
/**
* Create a new Prompt instance.
@@ -84,8 +84,9 @@ class Prompt {
* @param {string} param0.position - The position of the prompt in the prompt list.
* @param {number} param0.injection_position - The insert position of the prompt.
* @param {number} param0.injection_depth - The depth of the prompt in the chat.
+ * @param {boolean} param0.forbid_overrides - Indicates if the prompt should not be overridden.
*/
- constructor({ identifier, role, content, name, system_prompt, position, injection_depth, injection_position } = {}) {
+ constructor({ identifier, role, content, name, system_prompt, position, injection_depth, injection_position, forbid_overrides } = {}) {
this.identifier = identifier;
this.role = role;
this.content = content;
@@ -94,6 +95,7 @@ class Prompt {
this.position = position;
this.injection_depth = injection_depth;
this.injection_position = injection_position;
+ this.forbid_overrides = forbid_overrides;
}
}
@@ -187,6 +189,11 @@ class PromptManager {
'enhanceDefinitions',
];
+ this.overridablePrompts = [
+ 'main',
+ 'jailbreak',
+ ];
+
this.configuration = {
version: 1,
prefix: '',
@@ -389,6 +396,7 @@ class PromptManager {
case 'main':
prompt.name = 'Main Prompt';
prompt.content = this.configuration.defaultPrompts.main;
+ prompt.forbid_overrides = false;
break;
case 'nsfw':
prompt.name = 'Nsfw Prompt';
@@ -397,6 +405,7 @@ class PromptManager {
case 'jailbreak':
prompt.name = 'Jailbreak Prompt';
prompt.content = this.configuration.defaultPrompts.jailbreak;
+ prompt.forbid_overrides = false;
break;
case 'enhanceDefinitions':
prompt.name = 'Enhance Definitions';
@@ -410,6 +419,8 @@ class PromptManager {
document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position').value = prompt.injection_position ?? 0;
document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth').value = prompt.injection_depth ?? DEFAULT_DEPTH;
document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block').style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden';
+ document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides').checked = prompt.forbid_overrides ?? false;
+ document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block').style.visibility = this.overridablePrompts.includes(prompt.identifier) ? 'visible' : 'hidden';
if (!this.systemPrompts.includes(promptId)) {
document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position').removeAttribute('disabled');
@@ -711,6 +722,7 @@ class PromptManager {
prompt.content = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_prompt').value;
prompt.injection_position = Number(document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position').value);
prompt.injection_depth = Number(document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth').value);
+ prompt.forbid_overrides = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides').checked;
}
/**
@@ -1133,6 +1145,8 @@ class PromptManager {
const injectionPositionField = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position');
const injectionDepthField = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth');
const injectionDepthBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block');
+ const forbidOverridesField = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides');
+ const forbidOverridesBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block');
nameField.value = prompt.name ?? '';
roleField.value = prompt.role ?? '';
@@ -1141,6 +1155,8 @@ class PromptManager {
injectionDepthField.value = prompt.injection_depth ?? DEFAULT_DEPTH;
injectionDepthBlock.style.visibility = prompt.injection_position === INJECTION_POSITION.ABSOLUTE ? 'visible' : 'hidden';
injectionPositionField.removeAttribute('disabled');
+ forbidOverridesField.checked = prompt.forbid_overrides ?? false;
+ forbidOverridesBlock.style.visibility = this.overridablePrompts.includes(prompt.identifier) ? 'visible' : 'hidden';
if (this.systemPrompts.includes(prompt.identifier)) {
injectionPositionField.setAttribute('disabled', 'disabled');
@@ -1224,6 +1240,8 @@ class PromptManager {
const injectionPositionField = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_position');
const injectionDepthField = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_injection_depth');
const injectionDepthBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_depth_block');
+ const forbidOverridesField = document.getElementById(this.configuration.prefix + 'prompt_manager_popup_entry_form_forbid_overrides');
+ const forbidOverridesBlock = document.getElementById(this.configuration.prefix + 'prompt_manager_forbid_overrides_block');
nameField.value = '';
roleField.selectedIndex = 0;
@@ -1232,6 +1250,8 @@ class PromptManager {
injectionPositionField.removeAttribute('disabled');
injectionDepthField.value = DEFAULT_DEPTH;
injectionDepthBlock.style.visibility = 'unset';
+ forbidOverridesBlock.style.visibility = 'unset';
+ forbidOverridesField.checked = false;
roleField.disabled = false;
}
@@ -1501,16 +1521,19 @@ class PromptManager {
}
const encodedName = escapeHtml(prompt.name);
- const isSystemPrompt = !prompt.marker && prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE;
+ const isSystemPrompt = !prompt.marker && prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE && !prompt.forbid_overrides;
+ const isImportantPrompt = !prompt.marker && prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE && prompt.forbid_overrides;
const isUserPrompt = !prompt.marker && !prompt.system_prompt && prompt.injection_position !== INJECTION_POSITION.ABSOLUTE;
const isInjectionPrompt = !prompt.marker && prompt.injection_position === INJECTION_POSITION.ABSOLUTE;
+ const importantClass = isImportantPrompt ? `${prefix}prompt_manager_important` : '';
listItemHtml += `
-