From 6b1965fcf492cd217f877e7f2a032d85509da586 Mon Sep 17 00:00:00 2001 From: Cohee Date: Tue, 11 Jul 2023 23:22:12 +0300 Subject: [PATCH] #693 Format story string after WI insertion for instruct mode --- public/script.js | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/public/script.js b/public/script.js index 1bcc779f8..fce833cfe 100644 --- a/public/script.js +++ b/public/script.js @@ -2251,10 +2251,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, force_name2 = false; } - if (isInstruct) { - storyString = formatInstructStoryString(storyString, systemPrompt); - } - ////////////////////////////////// let chat2 = []; @@ -2301,6 +2297,14 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO); let zeroDepthAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, 0, ' '); + // Pre-format the World Info into the story string + storyString = worldInfoBefore + storyString + worldInfoAfter; + + // Format the instruction string + if (isInstruct) { + storyString = formatInstructStoryString(storyString, systemPrompt); + } + if (main_api === 'openai') { message_already_generated = ''; // OpenAI doesn't have multigen setOpenAIMessages(coreChat); @@ -2322,7 +2326,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, let chatString = ''; function getMessagesTokenCount() { const encodeString = [ - worldInfoString, storyString, examplesString, chatString, @@ -2535,7 +2538,6 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, console.debug('---checking Prompt size'); setPromtString(); const prompt = [ - worldInfoString, storyString, mesExmString, mesSendString, @@ -2571,9 +2573,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, mesSendString = adjustChatsSeparator(mesSendString); let finalPromt = - worldInfoBefore + storyString + - worldInfoAfter + afterScenarioAnchor + mesExmString + mesSendString + @@ -2670,14 +2670,12 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, ...thisPromptBits[currentArrayEntry], rawPrompt: generate_data.prompt, mesId: getNextMessageId(type), - worldInfoBefore: worldInfoBefore, allAnchors: allAnchors, summarizeString: (extension_prompts['1_memory']?.value || ''), authorsNoteString: (extension_prompts['2_floating_prompt']?.value || ''), smartContextString: (extension_prompts['chromadb']?.value || ''), worldInfoString: worldInfoString, storyString: storyString, - worldInfoAfter: worldInfoAfter, afterScenarioAnchor: afterScenarioAnchor, examplesString: examplesString, mesSendString: mesSendString, @@ -2690,6 +2688,8 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, this_max_context: this_max_context, padding: power_user.token_padding, main_api: main_api, + instruction: isInstruct ? substituteParams(power_user.prefer_character_prompt && systemPrompt ? systemPrompt : power_user.instruct.system_prompt) : '', + userPersona: (power_user.persona_description || ''), }; thisPromptBits = additionalPromptStuff; @@ -3178,6 +3178,7 @@ function promptItemize(itemizedPrompts, requestedMesId) { var thisPrompt_padding = itemizedPrompts[thisPromptSet].padding; var promptBiasTokens = getTokenCount(itemizedPrompts[thisPromptSet].promptBias); var this_main_api = itemizedPrompts[thisPromptSet].main_api; + var userPersonaStringTokens = getTokenCount(itemizedPrompts[thisPromptSet].userPersona); if (this_main_api == 'openai') { //for OAI API @@ -3213,10 +3214,11 @@ function promptItemize(itemizedPrompts, requestedMesId) { //for non-OAI APIs //console.log('-- Counting non-OAI Tokens'); var finalPromptTokens = getTokenCount(itemizedPrompts[thisPromptSet].finalPromt); - var storyStringTokens = getTokenCount(itemizedPrompts[thisPromptSet].storyString); + var storyStringTokens = getTokenCount(itemizedPrompts[thisPromptSet].storyString) - worldInfoStringTokens; var examplesStringTokens = getTokenCount(itemizedPrompts[thisPromptSet].examplesString); var mesSendStringTokens = getTokenCount(itemizedPrompts[thisPromptSet].mesSendString) var ActualChatHistoryTokens = mesSendStringTokens - allAnchorsTokens + power_user.token_padding; + var instructionTokens = getTokenCount(itemizedPrompts[thisPromptSet].instruction); var totalTokensInPrompt = storyStringTokens + //chardefs total @@ -3255,14 +3257,16 @@ function promptItemize(itemizedPrompts, requestedMesId) { //console.log('-- calling popup for OAI tokens'); callPopup( ` -

Prompt Itemization

+

+ Prompt Itemization + +

Tokenizer: ${selectedTokenizer}
API Used: ${this_main_api}
Only the white numbers really matter. All numbers are estimates. Grey color items may not have been included in the context due to certain prompt format settings. -
@@ -3323,6 +3327,10 @@ function promptItemize(itemizedPrompts, requestedMesId) {
-- Examples:
${examplesStringTokens}
+
+
-- User Persona:
+
${userPersonaStringTokens}
+
World Info:
@@ -3374,14 +3382,16 @@ function promptItemize(itemizedPrompts, requestedMesId) { //console.log('-- calling popup for non-OAI tokens'); callPopup( ` -

Prompt Itemization

+

+ Prompt Itemization + +

Tokenizer: ${selectedTokenizer}
API Used: ${this_main_api}
Only the white numbers really matter. All numbers are estimates. Grey color items may not have been included in the context due to certain prompt format settings. -
@@ -3414,6 +3424,14 @@ function promptItemize(itemizedPrompts, requestedMesId) {
-- Examples:
${examplesStringTokens}
+
+
-- User Persona:
+
${userPersonaStringTokens}
+
+
+
-- System Prompt (Instruct):
+
${instructionTokens}
+
World Info: