mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-03-03 03:17:54 +01:00
Add support for nsfw avoidance prompt
This commit is contained in:
parent
1adad6105a
commit
e47f436cf7
@ -2,6 +2,14 @@ import {countTokens} from "./openai.js";
|
|||||||
import {DraggablePromptListModule as DraggableList} from "./DraggableList.js";
|
import {DraggablePromptListModule as DraggableList} from "./DraggableList.js";
|
||||||
import {substituteParams} from "../script.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
|
// OpenAI API chat message handling
|
||||||
// const map = [{identifier: 'example', message: {role: 'system', content: 'exampleContent'}}, ...];
|
// const map = [{identifier: 'example', message: {role: 'system', content: 'exampleContent'}}, ...];
|
||||||
const ChatCompletion = {
|
const ChatCompletion = {
|
||||||
@ -12,6 +20,10 @@ const ChatCompletion = {
|
|||||||
this.map.push({ identifier, message });
|
this.map.push({ identifier, message });
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
get(identifier) {
|
||||||
|
const index = this.getMessageIndex(identifier);
|
||||||
|
return this.assertIndex(index, identifier).map[index];
|
||||||
|
},
|
||||||
insertBefore(identifier, insertIdentifier, insert) {
|
insertBefore(identifier, insertIdentifier, insert) {
|
||||||
const index = this.getMessageIndex(identifier);
|
const index = this.getMessageIndex(identifier);
|
||||||
this.map.splice(this.assertIndex(index, identifier), 0, { identifier: insertIdentifier, message: insert });
|
this.map.splice(this.assertIndex(index, identifier), 0, { identifier: insertIdentifier, message: insert });
|
||||||
|
@ -26,7 +26,7 @@ import {
|
|||||||
import {groups, selected_group} from "./group-chats.js";
|
import {groups, selected_group} from "./group-chats.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
defaultPromptManagerSettings,
|
defaultPromptManagerSettings, IdentifierNotFoundError,
|
||||||
openAiDefaultPromptLists,
|
openAiDefaultPromptLists,
|
||||||
openAiDefaultPrompts,
|
openAiDefaultPrompts,
|
||||||
PromptManagerModule as PromptManager
|
PromptManagerModule as PromptManager
|
||||||
@ -378,7 +378,7 @@ async function prepareOpenAIMessages({ systemPrompt, name2, storyString, worldIn
|
|||||||
.replace('newMainChat', newChatMessage)
|
.replace('newMainChat', newChatMessage)
|
||||||
.replace('chatHistory', chatMessages)
|
.replace('chatHistory', chatMessages)
|
||||||
|
|
||||||
// Hande group chats
|
// Handle group chats
|
||||||
if (selected_group) {
|
if (selected_group) {
|
||||||
const names = getGroupMembers(groups);
|
const names = getGroupMembers(groups);
|
||||||
const groupChatMessage = chatCompletion.makeSystemMessage(`[Start a new group chat. Group members: ${names}]`);
|
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);
|
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
|
// Handle extension prompt
|
||||||
if (extensionPrompt) chatCompletion.insertAfter('worldInfoAfter', 'extensionPrompt', extensionPrompt);
|
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));
|
if (type === "impersonate") chatCompletion.replace('main', substituteParams(oai_settings.impersonation_prompt));
|
||||||
|
|
||||||
// Handle chat examples
|
// 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);
|
const exampleMessages = prepareExampleMessages(openai_msgs ,openai_msgs_example, power_user.pin_examples);
|
||||||
if (exampleMessages.length) chatCompletion.replace('dialogueExamples', exampleMessages);
|
if (exampleMessages.length) chatCompletion.replace('dialogueExamples', exampleMessages);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user