(CC) Move continue nudge at the end of completion (#3611) * Move continue nudge at the end of completion Closes #3607 * Move continue message together with nudge

070de9df2dabb722a4194f7643c631716f202f87

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
3 files changed, +23 -14Ignore whitespace
default/content/presets/openai/Default.json+1 -1
@@ -32,7 +32,7 @@
3232 "new_chat_prompt": "[Start a new Chat]",
3333 "new_group_chat_prompt": "[Start a new group chat. Group members: {{group}}]",
3434 "new_example_chat_prompt": "[Example Chat]",
35- "continue_nudge_prompt": "[Continue the following message. Do not include ANY parts of the original message. Use capitalization and punctuation as if your reply is a part of the original message: {{lastChatMessage}}]",
35+ "continue_nudge_prompt": "[Continue your last message without repeating its original content.]",
3636 "bias_preset_selected": "Default (none)",
3737 "reverse_proxy": "",
3838 "proxy_password": "",
default/content/settings.json+1 -1
@@ -593,7 +593,7 @@
593593 "new_chat_prompt": "[Start a new Chat]",
594594 "new_group_chat_prompt": "[Start a new group chat. Group members: {{group}}]",
595595 "new_example_chat_prompt": "[Example Chat]",
596- "continue_nudge_prompt": "[Continue the following message. Do not include ANY parts of the original message. Use capitalization and punctuation as if your reply is a part of the original message: {{lastChatMessage}}]",
596+ "continue_nudge_prompt": "[Continue your last message without repeating its original content.]",
597597 "bias_preset_selected": "Default (none)",
598598 "bias_presets": {
599599 "Default (none)": [],
public/scripts/openai.js+21 -12
@@ -100,7 +100,7 @@ const default_wi_format = '{0}';
100100const default_new_chat_prompt = '[Start a new Chat]';
101101const default_new_group_chat_prompt = '[Start a new group chat. Group members: {{group}}]';
102102const default_new_example_chat_prompt = '[Example Chat]';
103-const default_continue_nudge_prompt = '[Continue the following message. Do not include ANY parts of the original message. Use capitalization and punctuation as if your reply is a part of the original message: {{lastChatMessage}}]';
103+const default_continue_nudge_prompt = '[Continue your last message without repeating its original content.]';
104104const default_bias = 'Default (none)';
105105const default_personality_format = '{{personality}}';
106106const default_scenario_format = '{{scenario}}';
@@ -792,7 +792,7 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
792792 }
793793
794794 // Reserve budget for continue nudge
795795 let continueMessagecontinueMessageCollection = null;
796796 if (type === 'continue' && cyclePrompt && !oai_settings.continue_prefill) {
797797 const promptObject = {
798798 identifier: 'continueNudge',
@@ -800,10 +800,19 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
800800 content: substituteParamsExtended(oai_settings.continue_nudge_prompt, { lastChatMessage: String(cyclePrompt).trim() }),
801801 system_prompt: true,
802802 };
803803 const continuePromptcontinueMessageCollection = new PromptMessageCollection(promptObject'continueNudge');
804804 const preparedPromptcontinueMessageIndex = promptManagermessages.preparePromptfindLastIndex(continuePromptx => !x.injected);
805- continueMessage = await Message.fromPromptAsync(preparedPrompt);
805+ if (continueMessageIndex >= 0) {
806- chatCompletion.reserveBudget(continueMessage);
806+ const continueMessage = messages.splice(continueMessageIndex, 1)[0];
807+ const prompt = new Prompt(continueMessage);
808+ const chatMessage = await Message.fromPromptAsync(promptManager.preparePrompt(prompt));
809+ continueMessageCollection.add(chatMessage);
810+ }
811+ const continueNudgePrompt = new Prompt(promptObject);
812+ const preparedNudgePrompt = promptManager.preparePrompt(continueNudgePrompt);
813+ const continueNudgeMessage = await Message.fromPromptAsync(preparedNudgePrompt);
814+ continueMessageCollection.add(continueNudgeMessage);
815+ chatCompletion.reserveBudget(continueMessageCollection);
807816 }
808817
809818 const lastChatPrompt = messages[messages.length - 1];
@@ -887,9 +896,9 @@ async function populateChatHistory(messages, prompts, chatCompletion, type = nul
887896 }
888897
889898 // Insert and free continue nudge
890899 if (type === 'continue' && continueMessagecontinueMessageCollection) {
891900 chatCompletion.freeBudget(continueMessagecontinueMessageCollection);
892901 chatCompletion.insertAtEndadd(continueMessagecontinueMessageCollection, 'chatHistory'-1);
893902 }
894903}
895904
@@ -2970,7 +2979,7 @@ export class ChatCompletion {
29702979 /**
29712980 * Checks if the token budget can afford the tokens of the specified message.
29722981 *
29732982 * @param {Message|MessageCollection} message - The message to check for affordability.
29742983 * @returns {boolean} True if the budget can afford the message, false otherwise.
29752984 */
29762985 canAfford(message) {
@@ -3058,7 +3067,7 @@ export class ChatCompletion {
30583067 * Validates if the given argument is an instance of MessageCollection.
30593068 * Throws an error if the validation fails.
30603069 *
30613070 * @param {MessageCollection|Message} collection - The collection to validate.
30623071 */
30633072 validateMessageCollection(collection) {
30643073 if (!(collection instanceof MessageCollection)) {
@@ -3084,7 +3093,7 @@ export class ChatCompletion {
30843093 * Checks if the token budget can afford the tokens of the given message.
30853094 * Throws an error if the budget can't afford the message.
30863095 *
30873096 * @param {Message|MessageCollection} message - The message to check.
30883097 * @param {string} identifier - The identifier of the message.
30893098 */
30903099 checkTokenBudget(message, identifier) {