diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index d5c57b308..c18cb527d 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -2,6 +2,14 @@ import {countTokens} from "./openai.js"; import {DraggablePromptListModule as DraggableList} from "./DraggableList.js"; import {substituteParams} from "../script.js"; +// Thrown by ChatCompletion when a requested prompt couldn't be found. +class IdentifierNotFoundError extends Error { + constructor(identifier) { + super(`Identifier ${identifier} not found`); + this.name = 'IdentifierNotFoundError'; + } +} + // OpenAI API chat message handling // const map = [{identifier: 'example', message: {role: 'system', content: 'exampleContent'}}, ...]; const ChatCompletion = { @@ -12,6 +20,10 @@ const ChatCompletion = { this.map.push({ identifier, message }); return this; }, + get(identifier) { + const index = this.getMessageIndex(identifier); + return this.assertIndex(index, identifier).map[index]; + }, insertBefore(identifier, insertIdentifier, insert) { const index = this.getMessageIndex(identifier); this.map.splice(this.assertIndex(index, identifier), 0, { identifier: insertIdentifier, message: insert }); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 941977505..42e046517 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -26,7 +26,7 @@ import { import {groups, selected_group} from "./group-chats.js"; import { - defaultPromptManagerSettings, + defaultPromptManagerSettings, IdentifierNotFoundError, openAiDefaultPromptLists, openAiDefaultPrompts, PromptManagerModule as PromptManager @@ -378,7 +378,7 @@ async function prepareOpenAIMessages({ systemPrompt, name2, storyString, worldIn .replace('newMainChat', newChatMessage) .replace('chatHistory', chatMessages) - // Hande group chats + // Handle group chats if (selected_group) { const names = getGroupMembers(groups); const groupChatMessage = chatCompletion.makeSystemMessage(`[Start a new group chat. Group members: ${names}]`); @@ -387,6 +387,14 @@ async function prepareOpenAIMessages({ systemPrompt, name2, storyString, worldIn chatCompletion.insertAfter('newMainChat', 'groupNudgeMessage', groupNudgeMessage); } + // Handle NSFW prompt + try { + const nsfwMessage = chatCompletion.get('nsfw'); + } catch (error) { + if (error instanceof IdentifierNotFoundError && oai_settings.nsfw_avoidance_prompt) + chatCompletion.insertAfter('main', 'nsfwAvoidance', chatCompletion.makeSystemMessage(oai_settings.nsfw_avoidance_prompt)); + } + // Handle extension prompt if (extensionPrompt) chatCompletion.insertAfter('worldInfoAfter', 'extensionPrompt', extensionPrompt); @@ -397,6 +405,7 @@ async function prepareOpenAIMessages({ systemPrompt, name2, storyString, worldIn if (type === "impersonate") chatCompletion.replace('main', substituteParams(oai_settings.impersonation_prompt)); // Handle chat examples + // ToDo: Update dialogueExamples prompt with only the token count that's actually sent. const exampleMessages = prepareExampleMessages(openai_msgs ,openai_msgs_example, power_user.pin_examples); if (exampleMessages.length) chatCompletion.replace('dialogueExamples', exampleMessages);