diff --git a/public/script.js b/public/script.js index efc1e0b28..3419432a7 100644 --- a/public/script.js +++ b/public/script.js @@ -161,7 +161,6 @@ export { name2, is_send_press, api_server_textgenerationwebui, - count_view_mes, max_context, chat_metadata, streamingProcessor, @@ -1224,14 +1223,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); @@ -1468,7 +1460,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(); }); @@ -1495,6 +1486,7 @@ async function Generate(type, automatic_trigger, force_name2) { // Compute anchors const topAnchorDepth = 8; + const bottomAnchorThreshold = 8; let anchorTop = ''; let anchorBottom = ''; if (!is_pygmalion) { @@ -1554,14 +1546,20 @@ 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); + } + + // 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); } @@ -1574,7 +1572,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: "); } @@ -1592,31 +1590,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; - 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); - } + 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 @@ -1624,7 +1610,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 @@ -1669,17 +1654,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(''); @@ -1745,39 +1721,37 @@ 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'); 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 + ":") { + if (i === arrMes.length - 1 && $.trim(item).substr(0, (name1 + ":").length) != name1 + ":") { if (textareaText == "") { 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 && !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'; } } - 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 && 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 f61e73e33..821f0e474 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -7,7 +7,6 @@ import { saveSettingsDebounced, substituteParams, - count_view_mes, checkOnlineStatus, setOnlineStatus, getExtensionPrompt, @@ -148,10 +147,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']; @@ -160,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,19 +188,17 @@ function setOpenAIMessageExamples(mesExamplesArray) { } } -function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, anchorBottom) { +function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, bottomAnchorThreshold, 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) { + 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 + 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; }