From 9a7bbd4ffbf7937dd66596df8ad3554d898eddbf Mon Sep 17 00:00:00 2001 From: maver Date: Sun, 4 Jun 2023 17:12:27 +0200 Subject: [PATCH] Add prompt manager support for group chats --- public/scripts/PromptManager.js | 34 ++++++++++++++++++++++++++++++--- public/scripts/openai.js | 3 ++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index 21be135fc..b58e8dca7 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -10,8 +10,12 @@ class IdentifierNotFoundError extends Error { } } -// OpenAI API chat message handling -// const map = [{identifier: 'example', message: {role: 'system', content: 'exampleContent'}}, ...]; +/** + * OpenAI API chat completion representation + * const map = [{identifier: 'example', message: {role: 'system', content: 'exampleContent'}}, ...]; + * + * @see https://platform.openai.com/docs/guides/gpt/chat-completions-api + */ const ChatCompletion = { new() { return { @@ -228,6 +232,12 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti this.saveServiceSettings().then(() => this.render()); }); + // Re-render when the group changes. + document.addEventListener('groupSelected', (event) => { + this.handleGroupSelected(event) + this.saveServiceSettings().then(() => this.render()); + }); + // Sanitize settings after character has been deleted. document.addEventListener('characterDeleted', (event) => { this.handleCharacterDeleted(event) @@ -297,7 +307,14 @@ PromptManagerModule.prototype.appendPrompt = function (prompt, character) { if (-1 === index) promptList.push({identifier: prompt.identifier, enabled: false}); } -// Add a prompt to the current characters prompt list + +/** + * Append a prompt to the current characters prompt list + * + * @param {object} prompt + * @param {object} character + * @returns {void} + */ PromptManagerModule.prototype.appendPrompt = function (prompt, character) { const promptList = this.getPromptListByCharacter(character); const index = promptList.findIndex(entry => entry.identifier === prompt.identifier); @@ -414,6 +431,17 @@ PromptManagerModule.prototype.handleCharacterSelected = function (event) { if (0 === this.serviceSettings.prompts.length) this.setPrompts(openAiDefaultPrompts.prompts); } +PromptManagerModule.prototype.handleGroupSelected = function (event) { + console.log(event.detail); + + const characterDummy = {id: event.detail.id}; + this.activeCharacter = characterDummy; + const promptList = this.getPromptListByCharacter(characterDummy); +console.log(promptList); + if (0 === promptList.length) this.addPromptListForCharacter(characterDummy, openAiDefaultPromptList) + if (0 === this.serviceSettings.prompts.length) this.setPrompts(openAiDefaultPrompts.prompts); +} + /** * Get the prompts for a specific character. Can be filtered to only include enabled prompts. * @returns {object[]} The prompts for the character. diff --git a/public/scripts/openai.js b/public/scripts/openai.js index adf9d8d67..62219f6b5 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -384,7 +384,8 @@ async function prepareOpenAIMessages({ systemPrompt, name2, storyString, worldIn const groupChatMessage = chatCompletion.makeSystemMessage(`[Start a new group chat. Group members: ${names}]`); const groupNudgeMessage = chatCompletion.makeSystemMessage(`[Write the next reply only as ${name2}]`); chatCompletion.replace('newMainChat', groupChatMessage) - chatCompletion.insertAfter('newMainChat', 'groupNudgeMessage', groupNudgeMessage); + chatCompletion.insertAfter('main', 'groupNudgeMessage', groupNudgeMessage); + chatCompletion.remove('main'); // ToDo: Instead of removing, the main prompt could be altered to reinforce the situation being a group scene. } // Handle NSFW prompt