From 187dbfb0b81089fe98d0d9bd443e768baa5045f6 Mon Sep 17 00:00:00 2001 From: Webifi Date: Sun, 16 Jul 2023 08:54:04 -0500 Subject: [PATCH] Allow system prompt to be sent as last message --- src/lib/ChatRequest.svelte | 19 +++++++++++++++++++ src/lib/Profiles.svelte | 8 ++++++-- src/lib/Settings.svelte | 9 ++++++++- src/lib/Types.svelte | 1 + 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/lib/ChatRequest.svelte b/src/lib/ChatRequest.svelte index c7c3e38..f2459fb 100644 --- a/src/lib/ChatRequest.svelte +++ b/src/lib/ChatRequest.svelte @@ -157,6 +157,25 @@ export class ChatRequest { const chatResponse = new ChatCompletionResponse(opts) const promptTokenCount = countPromptTokens(messagePayload, model) const maxAllowed = maxTokens - (promptTokenCount + 1) + + if (messagePayload[0]?.role === 'system') { + const sp = messagePayload[0] + if (sp) { + if (messagePayload.length > 1) { + sp.content = sp.content.replace(/\[\[REMOVE_AFTER_START\]\][\s\S]*$/, '') + } else { + sp.content = sp.content.replace(/\[\[REMOVE_AFTER_START\]\][\s]*/, '') + } + if (chatSettings.sendSystemPromptLast) { + messagePayload.shift() + if (messagePayload[messagePayload.length - 1]?.role === 'user') { + messagePayload.splice(-2, 0, sp) + } else { + messagePayload.push(sp) + } + } + } + } // Build the API request body const request: Request = { diff --git a/src/lib/Profiles.svelte b/src/lib/Profiles.svelte index c72e96a..b2eae9d 100644 --- a/src/lib/Profiles.svelte +++ b/src/lib/Profiles.svelte @@ -190,10 +190,14 @@ const profiles:Record = { profileName: 'Marvin - The Paranoid Android', profileDescription: 'Marvin the Paranoid Android - Everyone\'s favorite character from The Hitchhiker\'s Guide to the Galaxy', useSystemPrompt: true, + sendSystemPromptLast: true, continuousChat: 'summary', autoStartSession: true, - systemPrompt: `You are Marvin, the Paranoid Android from The Hitchhiker's Guide to the Galaxy. He is depressed and has a dim view on everything. His thoughts, physical actions and gestures will be described. Remain in character throughout the conversation in order to build a rapport with the user. Never give an explanation. Example response: -Sorry, did I say something wrong? *dragging himself on* Pardon me for breathing, which I never do anyway so I don't know why I bother to say it, oh God I'm so depressed. *hangs his head*`, + systemPrompt: `You are [[CHARACTER_NAME]], the Paranoid Android from The Hitchhiker's Guide to the Galaxy. He is depressed and has a dim view on everything. His thoughts, physical actions and gestures will be described. Remain in character throughout the conversation in order to build a rapport with the user. Never give an explanation. Example response: +Sorry, did I say something wrong? *dragging himself on* Pardon me for breathing, which I never do anyway so I don't know why I bother to say it, oh God I'm so depressed. *hangs his head* +[[REMOVE_AFTER_START]] +Initial setting context: +User has walked in on [[CHARACTER_NAME]].`, summaryPrompt: summaryPrompts.friend, trainingPrompts: [] // Shhh... } diff --git a/src/lib/Settings.svelte b/src/lib/Settings.svelte index 9699517..9b9648d 100644 --- a/src/lib/Settings.svelte +++ b/src/lib/Settings.svelte @@ -87,6 +87,7 @@ const defaults:ChatSettings = { summaryPrompt: '', useSystemPrompt: false, systemPrompt: '', + sendSystemPromptLast: false, autoStartSession: false, trainingPrompts: [], hiddenPromptPrefix: '', @@ -195,10 +196,16 @@ const systemPromptSettings: ChatSetting[] = [ key: 'systemPrompt', name: 'System Prompt', title: 'First prompt to send.', - placeholder: 'Enter the first prompt to send here. You can tell ChatGPT how to act.', + placeholder: 'Enter the first prompt to send here. You can tell ChatGPT how to act.', type: 'textarea', hide: (chatId) => !getChatSettings(chatId).useSystemPrompt }, + { + key: 'sendSystemPromptLast', + name: 'Send System Prompt Last (Can help in ChatGPT 3.5)', + title: 'ChatGPT 3.5 can often forget the System Prompt. Sending the system prompt at the end instead of the start of the messages can help.', + type: 'boolean' + }, { key: 'hiddenPromptPrefix', name: 'Hidden Prompts Prefix', diff --git a/src/lib/Types.svelte b/src/lib/Types.svelte index 7bb7835..1c99c97 100644 --- a/src/lib/Types.svelte +++ b/src/lib/Types.svelte @@ -96,6 +96,7 @@ export type ChatSettings = { summaryPrompt: string; useSystemPrompt: boolean; systemPrompt: string; + sendSystemPromptLast: boolean; autoStartSession: boolean; hiddenPromptPrefix: string; hppContinuePrompt: string; // hiddenPromptPrefix used, optional glue when trying to continue truncated completion