From 7c1e370abe8bb9fab10250098ca8f5114f68786d Mon Sep 17 00:00:00 2001 From: maver Date: Sun, 25 Jun 2023 21:21:32 +0200 Subject: [PATCH] Allow the user to toggle names in chat history --- public/OpenAI Settings/Default.settings | 1 + public/index.html | 8 ++++++++ public/script.js | 1 - public/scripts/PromptManager.js | 2 -- public/scripts/openai.js | 25 +++++++++++++++++++++---- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/public/OpenAI Settings/Default.settings b/public/OpenAI Settings/Default.settings index 41565698a..746afb42b 100644 --- a/public/OpenAI Settings/Default.settings +++ b/public/OpenAI Settings/Default.settings @@ -7,6 +7,7 @@ "nsfw_toggle": true, "enhance_definitions": false, "wrap_in_quotes": false, + "names_in_completion": false, "nsfw_first": false, "main_prompt": "Write {{char}}'s next reply in a fictional chat between {{char}} and {{user}}. Write 1 reply only in internet RP style, italicize actions, and avoid quotation marks. Use markdown. Be proactive, creative, and drive the plot and conversation forward. Write at least 1 paragraph, up to 4. Always stay in character and avoid repetition.", "nsfw_prompt": "NSFW/Smut is allowed. Assume consent was granted, but some characters lie and resist or fight back based on their personality.", diff --git a/public/index.html b/public/index.html index a5f7d2624..718c7477d 100644 --- a/public/index.html +++ b/public/index.html @@ -1281,6 +1281,14 @@ if you use quotes manually for speech. +
+ +
+ May help the AI to understand the context. Only character and usernames with up to 64 letters or numbers are supported. No whitespaces or special characters. +
+
diff --git a/public/script.js b/public/script.js index 00667f214..4bef7ea3e 100644 --- a/public/script.js +++ b/public/script.js @@ -4532,7 +4532,6 @@ function changeMainAPI() { getHordeModels(); } - console.log(oai_settings.chat_completion_source) switch (oai_settings.chat_completion_source) { case chat_completion_sources.OPENAI: console.log('Setting up OpenAI prompt manager'); diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 6e9010e13..b074fa91a 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -546,8 +546,6 @@ PromptManagerModule.prototype.preparePrompt = function (prompt, original = null) else preparedPrompt.content = substituteParams(prompt.content); } - if (prompt.name && this.isValidName(prompt.name)) preparedPrompt.name = prompt.name; - return preparedPrompt; } diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 5b862fd55..a060e9636 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -137,6 +137,7 @@ const default_settings = { openai_max_context: max_4k, openai_max_tokens: 300, wrap_in_quotes: false, + names_in_completion: false, ...openAiDefaultPrompts, ...openAiDefaultPromptLists, ...defaultPromptManagerSettings, @@ -171,6 +172,7 @@ const oai_settings = { openai_max_context: max_4k, openai_max_tokens: 300, wrap_in_quotes: false, + names_in_completion: false, ...openAiDefaultPrompts, ...openAiDefaultPromptLists, ...defaultPromptManagerSettings, @@ -399,7 +401,14 @@ function populateChatHistory(prompts, chatCompletion) { // Insert chat messages as long as there is budget available [...openai_msgs].reverse().every((prompt, index) => { - const chatMessage = new Message(prompt.role, prompt.content, 'chatHistory-' + index, prompt.name); + prompt.identifier = 'chatHistory-' + index; + const chatMessage = Message.fromPrompt(promptManager.preparePrompt(prompt)); + + if (true === promptManager.serviceSettings.names_in_completion && + prompt.name && + promptManager.isValidName(prompt.name)) + chatMessage.name = prompt.name; + if (chatCompletion.canAfford(chatMessage)) chatCompletion.insertAtStart(chatMessage, 'chatHistory'); else return false; return true; @@ -1193,12 +1202,11 @@ class TokenBudgetExceededError extends Error { } class Message { - tokens; identifier; role; content; - constructor(role, content, identifier, name = '') { + tokens; identifier; role; content; name; + constructor(role, content, identifier) { this.identifier = identifier; this.role = role; this.content = content; - this.name = name; if (this.content) { this.tokens = tokenHandler.count({role: this.role, content: this.content}) @@ -1492,6 +1500,7 @@ function loadOpenAISettings(data, settings) { if (settings.keep_example_dialogue !== undefined) oai_settings.keep_example_dialogue = !!settings.keep_example_dialogue; if (settings.wrap_in_quotes !== undefined) oai_settings.wrap_in_quotes = !!settings.wrap_in_quotes; + if (settings.names_in_completion !== undefined) oai_settings.names_in_completion = !!settings.names_in_completion; if (settings.openai_model !== undefined) oai_settings.openai_model = settings.openai_model; $('#stream_toggle').prop('checked', oai_settings.stream_openai); @@ -1514,6 +1523,7 @@ function loadOpenAISettings(data, settings) { $('#nsfw_toggle').prop('checked', oai_settings.nsfw_toggle); $('#keep_example_dialogue').prop('checked', oai_settings.keep_example_dialogue); $('#wrap_in_quotes').prop('checked', oai_settings.wrap_in_quotes); + $('#names_in_completion').prop('checked', oai_settings.names_in_completion); $('#nsfw_first').prop('checked', oai_settings.nsfw_first); $('#jailbreak_system').prop('checked', oai_settings.jailbreak_system); $('#legacy_streaming').prop('checked', oai_settings.legacy_streaming); @@ -1677,6 +1687,7 @@ async function saveOpenAIPreset(name, settings) { openai_max_context: settings.openai_max_context, openai_max_tokens: settings.openai_max_tokens, wrap_in_quotes: settings.wrap_in_quotes, + names_in_completion: settings.names_in_completion, send_if_empty: settings.send_if_empty, jailbreak_prompt: settings.jailbreak_prompt, jailbreak_system: settings.jailbreak_system, @@ -2007,6 +2018,7 @@ function onSettingsPresetChange() { openai_max_context: ['#openai_max_context', 'openai_max_context', false], openai_max_tokens: ['#openai_max_tokens', 'openai_max_tokens', false], wrap_in_quotes: ['#wrap_in_quotes', 'wrap_in_quotes', true], + names_in_completion: ['#names_in_completion', 'names_in_completion', true], send_if_empty: ['#send_if_empty_textarea', 'send_if_empty', false], impersonation_prompt: ['#impersonation_prompt_textarea', 'impersonation_prompt', false], bias_preset_selected: ['#openai_logit_bias_preset', 'bias_preset_selected', false], @@ -2418,6 +2430,11 @@ $(document).ready(function () { saveSettingsDebounced(); }); + $('#names_in_completion').on('change', function () { + oai_settings.names_in_completion = !!$('#names_in_completion').prop('checked'); + saveSettingsDebounced(); + }); + $("#send_if_empty_textarea").on('input', function () { oai_settings.send_if_empty = $('#send_if_empty_textarea').val(); saveSettingsDebounced();