From f2d4b5ba76952352b364ca8cf9b5946f33468c0a Mon Sep 17 00:00:00 2001 From: Grzegorz Gidel Date: Sun, 23 Apr 2023 16:48:31 +0200 Subject: [PATCH 1/6] Remove redundant checks for personality insertion and fix a stray space in OpenAI prompt --- public/script.js | 6 ++---- public/scripts/openai.js | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/public/script.js b/public/script.js index a03920876..fbf086458 100644 --- a/public/script.js +++ b/public/script.js @@ -1731,7 +1731,6 @@ async function Generate(type, automatic_trigger, force_name2) { console.log('generating prompt'); chatString = ""; arrMes = arrMes.reverse(); - let is_add_personality = false; arrMes.forEach(function (item, i, arr) {//For added anchors and others if (i >= arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) != name1 + ":") { @@ -1739,12 +1738,11 @@ async function Generate(type, automatic_trigger, force_name2) { item = item.substr(0, item.length - 1); } } - if (i === arrMes.length - topAnchorDepth && count_view_mes >= topAnchorDepth && !is_add_personality) { - is_add_personality = true; + if (i === arrMes.length - topAnchorDepth && count_view_mes >= topAnchorDepth && !is_pygmalion) { //chatString = chatString.substr(0,chatString.length-1); //anchorAndPersonality = "[Genre: roleplay chat][Tone: very long messages with descriptions]"; let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' '); - if (personalityAndAnchor && !is_pygmalion) { + if (personalityAndAnchor) { item += "[" + personalityAndAnchor + ']\n'; } } diff --git a/public/scripts/openai.js b/public/scripts/openai.js index f61e73e33..020225dd5 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -201,14 +201,12 @@ function setOpenAIMessageExamples(mesExamplesArray) { function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, anchorBottom) { openai_msgs = openai_msgs.reverse(); - let is_add_personality = false; openai_msgs.forEach(function (msg, i, arr) {//For added anchors and others let item = msg["content"]; - if (i === openai_msgs.length - topAnchorDepth && count_view_mes >= topAnchorDepth && !is_add_personality) { - is_add_personality = true; - if ((anchorTop != "" || charPersonality != "")) { - if (anchorTop != "") charPersonality += ' '; - item = `[${name2} is ${charPersonality}${anchorTop}]\n${item}`; + if (i === openai_msgs.length - topAnchorDepth && count_view_mes >= topAnchorDepth) { + let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' '); + if (personalityAndAnchor) { + item = `[${name2} is ${personalityAndAnchor}]\n${item}`; } } if (i >= openai_msgs.length - 1 && count_view_mes > 8 && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end From 717c14801d464a92f28ed7faef907f71931fef00 Mon Sep 17 00:00:00 2001 From: Grzegorz Gidel Date: Sun, 23 Apr 2023 17:22:23 +0200 Subject: [PATCH 2/6] Define bottom anchor threshold in one place --- public/script.js | 5 +++-- public/scripts/openai.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/public/script.js b/public/script.js index fbf086458..e6aa952c8 100644 --- a/public/script.js +++ b/public/script.js @@ -1485,6 +1485,7 @@ async function Generate(type, automatic_trigger, force_name2) { // Compute anchors const topAnchorDepth = 8; + const bottomAnchorThreshold = 8; let anchorTop = ''; let anchorBottom = ''; if (!is_pygmalion) { @@ -1725,7 +1726,7 @@ async function Generate(type, automatic_trigger, force_name2) { generatedPromtCache += cycleGenerationPromt; if (generatedPromtCache.length == 0) { if (main_api === 'openai') { - generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, anchorBottom); + generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, bottomAnchorThreshold, anchorBottom); } console.log('generating prompt'); @@ -1746,7 +1747,7 @@ async function Generate(type, automatic_trigger, force_name2) { item += "[" + personalityAndAnchor + ']\n'; } } - if (i >= arrMes.length - 1 && count_view_mes > 8 && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":" && !is_pygmalion) {//For add anchor in end + if (i >= arrMes.length - 1 && count_view_mes > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":" && !is_pygmalion) {//For add anchor in end item = item.substr(0, item.length - 1); //chatString+=postAnchor+"\n";//"[Writing style: very long messages]\n"; item = item + anchorBottom + "\n"; diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 020225dd5..a1c63dc8c 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -199,7 +199,7 @@ function setOpenAIMessageExamples(mesExamplesArray) { } } -function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, anchorBottom) { +function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, bottomAnchorThreshold, anchorBottom) { openai_msgs = openai_msgs.reverse(); openai_msgs.forEach(function (msg, i, arr) {//For added anchors and others let item = msg["content"]; @@ -209,7 +209,7 @@ function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, a item = `[${name2} is ${personalityAndAnchor}]\n${item}`; } } - if (i >= openai_msgs.length - 1 && count_view_mes > 8 && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end + if (i >= openai_msgs.length - 1 && count_view_mes > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end item = anchorBottom + "\n" + item; } From 087274db9b1468ef0bc78f673c20dacfe890babc Mon Sep 17 00:00:00 2001 From: Grzegorz Gidel Date: Thu, 27 Apr 2023 21:50:07 +0200 Subject: [PATCH 3/6] Remove redundant OpenAI regenerate/swipe code --- public/script.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/public/script.js b/public/script.js index e6aa952c8..1602b06eb 100644 --- a/public/script.js +++ b/public/script.js @@ -1458,7 +1458,6 @@ async function Generate(type, automatic_trigger, force_name2) { else if (type !== "swipe" && !isImpersonate) { chat.length = chat.length - 1; count_view_mes -= 1; - openai_msgs.pop(); $('#chat').children().last().hide(500, function () { $(this).remove(); }); @@ -1546,7 +1545,7 @@ async function Generate(type, automatic_trigger, force_name2) { let mesExamplesArray = mesExamples.split(//gi).slice(1).map(block => `${blockHeading}\n${block.trim()}\n`); if (main_api === 'openai') { - const oai_chat = [...chat].filter(x => !x.is_system); + const oai_chat = chat.filter(x => !x.is_system); if (type == 'swipe') { oai_chat.pop(); From a71b8967ac29afcbbc21aa06e4cbf6cfb9eac7d5 Mon Sep 17 00:00:00 2001 From: Grzegorz Gidel Date: Sat, 29 Apr 2023 00:59:31 +0200 Subject: [PATCH 4/6] Deduplicate code for substitution in the story and the first message --- public/script.js | 20 ++++++-------------- public/scripts/openai.js | 4 ---- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/public/script.js b/public/script.js index 1602b06eb..3474a5e87 100644 --- a/public/script.js +++ b/public/script.js @@ -1214,14 +1214,7 @@ function getExtensionPrompt(position = 0, depth = undefined, separator = "\n") { function baseChatReplace(value, name1, name2) { if (value !== undefined && value.length > 0) { - if (is_pygmalion) { - value = value.replace(/{{user}}:/gi, "You:"); - value = value.replace(/:/gi, "You:"); - } - value = value.replace(/{{user}}/gi, name1); - value = value.replace(/{{char}}/gi, name2); - value = value.replace(//gi, name1); - value = value.replace(//gi, name2); + value = substituteParams(value, is_pygmalion ? "You:" : name1, name2); if (power_user.collapse_newlines) { value = collapseNewlines(value); @@ -1544,6 +1537,11 @@ async function Generate(type, automatic_trigger, force_name2) { is_pygmalion ? '' : `This is how ${name2} should talk`; let mesExamplesArray = mesExamples.split(//gi).slice(1).map(block => `${blockHeading}\n${block.trim()}\n`); + // First message in fresh 1-on-1 chat reacts to user/character settings changes + if (chat.length) { + chat[0].mes = substituteParams(chat[0].mes); + } + if (main_api === 'openai') { const oai_chat = chat.filter(x => !x.is_system); @@ -1587,12 +1585,6 @@ async function Generate(type, automatic_trigger, force_name2) { console.log('pre-replace chat.length = ' + chat.length); for (let i = chat.length - 1, j = 0; i >= 0; i--, j++) { let charName = selected_group ? chat[j].name : name2; - if (j == 0) { - chat[j]['mes'] = chat[j]['mes'].replace(/{{user}}/gi, name1); - chat[j]['mes'] = chat[j]['mes'].replace(/{{char}}/gi, charName); - chat[j]['mes'] = chat[j]['mes'].replace(//gi, name1); - chat[j]['mes'] = chat[j]['mes'].replace(//gi, charName); - } let this_mes_ch_name = ''; if (chat[j]['is_user']) { this_mes_ch_name = name1; diff --git a/public/scripts/openai.js b/public/scripts/openai.js index a1c63dc8c..ed726896a 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -148,10 +148,6 @@ function setOpenAIMessages(chat) { // clean openai msgs openai_msgs = []; for (let i = chat.length - 1; i >= 0; i--) { - // first greeting message - if (j == 0) { - chat[j]['mes'] = substituteParams(chat[j]['mes']); - } let role = chat[j]['is_user'] ? 'user' : 'assistant'; let content = chat[j]['mes']; From 303e183cdaa5400031422a63a1662f555be70493 Mon Sep 17 00:00:00 2001 From: Grzegorz Gidel Date: Fri, 28 Apr 2023 00:23:57 +0200 Subject: [PATCH 5/6] Fix disappearing personality and unreliable anchors when swiping or with system messages --- public/script.js | 50 ++++++++++++++-------------------------- public/scripts/openai.js | 11 ++------- 2 files changed, 19 insertions(+), 42 deletions(-) diff --git a/public/script.js b/public/script.js index 3474a5e87..26b5581b5 100644 --- a/public/script.js +++ b/public/script.js @@ -151,7 +151,6 @@ export { name2, is_send_press, api_server_textgenerationwebui, - count_view_mes, max_context, chat_metadata, streamingProcessor, @@ -1542,14 +1541,15 @@ async function Generate(type, automatic_trigger, force_name2) { chat[0].mes = substituteParams(chat[0].mes); } + // Collect messages with usable content + let coreChat = chat.filter(x => !x.is_system); + if (type === 'swipe') { + coreChat.pop(); + } + console.log(`Core/all messages: ${coreChat.length}/${chat.length}`); + if (main_api === 'openai') { - const oai_chat = chat.filter(x => !x.is_system); - - if (type == 'swipe') { - oai_chat.pop(); - } - - setOpenAIMessages(oai_chat); + setOpenAIMessages(coreChat); setOpenAIMessageExamples(mesExamplesArray); } @@ -1562,7 +1562,7 @@ async function Generate(type, automatic_trigger, force_name2) { } else { storyString += appendToStoryString(charDescription, ''); - if (count_view_mes < topAnchorDepth) { + if (coreChat.length < topAnchorDepth) { storyString += appendToStoryString(charPersonality, power_user.disable_personality_formatting ? '' : name2 + "'s personality: "); } @@ -1580,25 +1580,19 @@ async function Generate(type, automatic_trigger, force_name2) { ////////////////////////////////// - console.log('emptying chat2'); let chat2 = []; - console.log('pre-replace chat.length = ' + chat.length); - for (let i = chat.length - 1, j = 0; i >= 0; i--, j++) { - let charName = selected_group ? chat[j].name : name2; + for (let i = coreChat.length - 1, j = 0; i >= 0; i--, j++) { + let charName = selected_group ? coreChat[j].name : name2; let this_mes_ch_name = ''; - if (chat[j]['is_user']) { + if (coreChat[j]['is_user']) { this_mes_ch_name = name1; } else { this_mes_ch_name = charName; } - if (chat[j]['is_name']) { - chat2[i] = this_mes_ch_name + ': ' + chat[j]['mes'] + '\n'; + if (coreChat[j]['is_name']) { + chat2[i] = this_mes_ch_name + ': ' + coreChat[j]['mes'] + '\n'; } else { - chat2[i] = chat[j]['mes'] + '\n'; - } - // system messages produce no text - if (chat[j]['is_system']) { - chat2[i] = ''; + chat2[i] = coreChat[j]['mes'] + '\n'; } // replace bias markup @@ -1606,7 +1600,6 @@ async function Generate(type, automatic_trigger, force_name2) { chat2[i] = (chat2[i] ?? '').replace(/{{(\*?.+?\*?)}}/g, ''); //console.log('replacing chat2 {}s'); } - //console.log('post replace chat.length = ' + chat.length); //chat2 = chat2.reverse(); // Determine token limit @@ -1651,17 +1644,8 @@ async function Generate(type, automatic_trigger, force_name2) { const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO); const zeroDepthAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, 0, ' '); - /////////////////////// swipecode - if (type == 'swipe') { - //console.log('pre swipe shift: ' + chat2.length); - //console.log('shifting swipe chat2'); - chat2.shift(); - } - let { worldInfoString, worldInfoBefore, worldInfoAfter } = getWorldInfoPrompt(chat2); - console.log('post swipe shift:' + chat2.length); - // hack for regeneration of the first message if (chat2.length == 0) { chat2.push(''); @@ -1730,7 +1714,7 @@ async function Generate(type, automatic_trigger, force_name2) { item = item.substr(0, item.length - 1); } } - if (i === arrMes.length - topAnchorDepth && count_view_mes >= topAnchorDepth && !is_pygmalion) { + if (i === arrMes.length - topAnchorDepth && !is_pygmalion) { //chatString = chatString.substr(0,chatString.length-1); //anchorAndPersonality = "[Genre: roleplay chat][Tone: very long messages with descriptions]"; let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' '); @@ -1738,7 +1722,7 @@ async function Generate(type, automatic_trigger, force_name2) { item += "[" + personalityAndAnchor + ']\n'; } } - if (i >= arrMes.length - 1 && count_view_mes > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":" && !is_pygmalion) {//For add anchor in end + if (i >= arrMes.length - 1 && coreChat.length > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":" && !is_pygmalion) {//For add anchor in end item = item.substr(0, item.length - 1); //chatString+=postAnchor+"\n";//"[Writing style: very long messages]\n"; item = item + anchorBottom + "\n"; diff --git a/public/scripts/openai.js b/public/scripts/openai.js index ed726896a..7235ffcfd 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -7,7 +7,6 @@ import { saveSettingsDebounced, substituteParams, - count_view_mes, checkOnlineStatus, setOnlineStatus, getExtensionPrompt, @@ -156,12 +155,6 @@ function setOpenAIMessages(chat) { content = `${chat[j].name}: ${content}`; } - // system messages produce no content - if (chat[j]['is_system']) { - role = 'system'; - content = ''; - } - // replace bias markup //content = (content ?? '').replace(/{.*}/g, ''); content = (content ?? '').replace(/{{(\*?.+?\*?)}}/g, ''); @@ -199,13 +192,13 @@ function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, b openai_msgs = openai_msgs.reverse(); openai_msgs.forEach(function (msg, i, arr) {//For added anchors and others let item = msg["content"]; - if (i === openai_msgs.length - topAnchorDepth && count_view_mes >= topAnchorDepth) { + if (i === openai_msgs.length - topAnchorDepth) { let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' '); if (personalityAndAnchor) { item = `[${name2} is ${personalityAndAnchor}]\n${item}`; } } - if (i >= openai_msgs.length - 1 && count_view_mes > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end + if (i >= openai_msgs.length - 1 && openai_msgs.length > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end item = anchorBottom + "\n" + item; } From d625f1386eed83cff23047f1fb4cf5cbbaa77dd1 Mon Sep 17 00:00:00 2001 From: Grzegorz Gidel Date: Fri, 28 Apr 2023 12:06:31 +0200 Subject: [PATCH 6/6] Do not act as if forEach can iterate beyond the chat array --- public/script.js | 8 ++++---- public/scripts/openai.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/public/script.js b/public/script.js index 26b5581b5..d789e4151 100644 --- a/public/script.js +++ b/public/script.js @@ -1709,7 +1709,7 @@ async function Generate(type, automatic_trigger, force_name2) { arrMes = arrMes.reverse(); arrMes.forEach(function (item, i, arr) {//For added anchors and others - if (i >= arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) != name1 + ":") { + if (i === arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) != name1 + ":") { if (textareaText == "") { item = item.substr(0, item.length - 1); } @@ -1722,16 +1722,16 @@ async function Generate(type, automatic_trigger, force_name2) { item += "[" + personalityAndAnchor + ']\n'; } } - if (i >= arrMes.length - 1 && coreChat.length > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":" && !is_pygmalion) {//For add anchor in end + if (i === arrMes.length - 1 && coreChat.length > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":" && !is_pygmalion) {//For add anchor in end item = item.substr(0, item.length - 1); //chatString+=postAnchor+"\n";//"[Writing style: very long messages]\n"; item = item + anchorBottom + "\n"; } if (is_pygmalion) { - if (i >= arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//for add name2 when user sent + if (i === arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//for add name2 when user sent item = item + name2 + ":"; } - if (i >= arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) != name1 + ":") {//for add name2 when continue + if (i === arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) != name1 + ":") {//for add name2 when continue if (textareaText == "") { item = item + '\n' + name2 + ":"; } diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 7235ffcfd..821f0e474 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -198,7 +198,7 @@ function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, b item = `[${name2} is ${personalityAndAnchor}]\n${item}`; } } - if (i >= openai_msgs.length - 1 && openai_msgs.length > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end + if (i === openai_msgs.length - 1 && openai_msgs.length > bottomAnchorThreshold && $.trim(item).substr(0, (name1 + ":").length) == name1 + ":") {//For add anchor in end item = anchorBottom + "\n" + item; }