Add support for nsfw avoidance prompt

This commit is contained in:
maver 2023-06-03 16:48:29 +02:00
parent 1adad6105a
commit e47f436cf7
2 changed files with 23 additions and 2 deletions

View File

@ -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 });

View File

@ -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);