Fix disappearing personality and unreliable anchors when swiping or with system messages

This commit is contained in:
Grzegorz Gidel
2023-04-28 00:23:57 +02:00
parent a71b8967ac
commit 303e183cda
2 changed files with 19 additions and 42 deletions

View File

@ -151,7 +151,6 @@ export {
name2, name2,
is_send_press, is_send_press,
api_server_textgenerationwebui, api_server_textgenerationwebui,
count_view_mes,
max_context, max_context,
chat_metadata, chat_metadata,
streamingProcessor, streamingProcessor,
@ -1542,14 +1541,15 @@ async function Generate(type, automatic_trigger, force_name2) {
chat[0].mes = substituteParams(chat[0].mes); chat[0].mes = substituteParams(chat[0].mes);
} }
if (main_api === 'openai') { // Collect messages with usable content
const oai_chat = chat.filter(x => !x.is_system); let coreChat = chat.filter(x => !x.is_system);
if (type === 'swipe') {
if (type == 'swipe') { coreChat.pop();
oai_chat.pop();
} }
console.log(`Core/all messages: ${coreChat.length}/${chat.length}`);
setOpenAIMessages(oai_chat); if (main_api === 'openai') {
setOpenAIMessages(coreChat);
setOpenAIMessageExamples(mesExamplesArray); setOpenAIMessageExamples(mesExamplesArray);
} }
@ -1562,7 +1562,7 @@ async function Generate(type, automatic_trigger, force_name2) {
} else { } else {
storyString += appendToStoryString(charDescription, ''); storyString += appendToStoryString(charDescription, '');
if (count_view_mes < topAnchorDepth) { if (coreChat.length < topAnchorDepth) {
storyString += appendToStoryString(charPersonality, power_user.disable_personality_formatting ? '' : name2 + "'s personality: "); 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 = []; let chat2 = [];
console.log('pre-replace chat.length = ' + chat.length); for (let i = coreChat.length - 1, j = 0; i >= 0; i--, j++) {
for (let i = chat.length - 1, j = 0; i >= 0; i--, j++) { let charName = selected_group ? coreChat[j].name : name2;
let charName = selected_group ? chat[j].name : name2;
let this_mes_ch_name = ''; let this_mes_ch_name = '';
if (chat[j]['is_user']) { if (coreChat[j]['is_user']) {
this_mes_ch_name = name1; this_mes_ch_name = name1;
} else { } else {
this_mes_ch_name = charName; this_mes_ch_name = charName;
} }
if (chat[j]['is_name']) { if (coreChat[j]['is_name']) {
chat2[i] = this_mes_ch_name + ': ' + chat[j]['mes'] + '\n'; chat2[i] = this_mes_ch_name + ': ' + coreChat[j]['mes'] + '\n';
} else { } else {
chat2[i] = chat[j]['mes'] + '\n'; chat2[i] = coreChat[j]['mes'] + '\n';
}
// system messages produce no text
if (chat[j]['is_system']) {
chat2[i] = '';
} }
// replace bias markup // replace bias markup
@ -1606,7 +1600,6 @@ async function Generate(type, automatic_trigger, force_name2) {
chat2[i] = (chat2[i] ?? '').replace(/{{(\*?.+?\*?)}}/g, ''); chat2[i] = (chat2[i] ?? '').replace(/{{(\*?.+?\*?)}}/g, '');
//console.log('replacing chat2 {}s'); //console.log('replacing chat2 {}s');
} }
//console.log('post replace chat.length = ' + chat.length);
//chat2 = chat2.reverse(); //chat2 = chat2.reverse();
// Determine token limit // Determine token limit
@ -1651,17 +1644,8 @@ async function Generate(type, automatic_trigger, force_name2) {
const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO); const afterScenarioAnchor = getExtensionPrompt(extension_prompt_types.AFTER_SCENARIO);
const zeroDepthAnchor = getExtensionPrompt(extension_prompt_types.IN_CHAT, 0, ' '); 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); let { worldInfoString, worldInfoBefore, worldInfoAfter } = getWorldInfoPrompt(chat2);
console.log('post swipe shift:' + chat2.length);
// hack for regeneration of the first message // hack for regeneration of the first message
if (chat2.length == 0) { if (chat2.length == 0) {
chat2.push(''); chat2.push('');
@ -1730,7 +1714,7 @@ async function Generate(type, automatic_trigger, force_name2) {
item = item.substr(0, item.length - 1); 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); //chatString = chatString.substr(0,chatString.length-1);
//anchorAndPersonality = "[Genre: roleplay chat][Tone: very long messages with descriptions]"; //anchorAndPersonality = "[Genre: roleplay chat][Tone: very long messages with descriptions]";
let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' '); let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' ');
@ -1738,7 +1722,7 @@ async function Generate(type, automatic_trigger, force_name2) {
item += "[" + personalityAndAnchor + ']\n'; 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); item = item.substr(0, item.length - 1);
//chatString+=postAnchor+"\n";//"[Writing style: very long messages]\n"; //chatString+=postAnchor+"\n";//"[Writing style: very long messages]\n";
item = item + anchorBottom + "\n"; item = item + anchorBottom + "\n";

View File

@ -7,7 +7,6 @@
import { import {
saveSettingsDebounced, saveSettingsDebounced,
substituteParams, substituteParams,
count_view_mes,
checkOnlineStatus, checkOnlineStatus,
setOnlineStatus, setOnlineStatus,
getExtensionPrompt, getExtensionPrompt,
@ -156,12 +155,6 @@ function setOpenAIMessages(chat) {
content = `${chat[j].name}: ${content}`; content = `${chat[j].name}: ${content}`;
} }
// system messages produce no content
if (chat[j]['is_system']) {
role = 'system';
content = '';
}
// replace bias markup // replace bias markup
//content = (content ?? '').replace(/{.*}/g, ''); //content = (content ?? '').replace(/{.*}/g, '');
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 = openai_msgs.reverse();
openai_msgs.forEach(function (msg, i, arr) {//For added anchors and others openai_msgs.forEach(function (msg, i, arr) {//For added anchors and others
let item = msg["content"]; 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(' '); let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' ');
if (personalityAndAnchor) { if (personalityAndAnchor) {
item = `[${name2} is ${personalityAndAnchor}]\n${item}`; 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; item = anchorBottom + "\n" + item;
} }