Merge pull request #200 from gidzzz/fix-disappearing-personality

Fix disappearing personality and unreliable anchors when swiping or with system messages
This commit is contained in:
Cohee
2023-04-29 17:18:27 +03:00
committed by GitHub
2 changed files with 35 additions and 74 deletions

View File

@ -161,7 +161,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,
@ -1224,14 +1223,7 @@ function getExtensionPrompt(position = 0, depth = undefined, separator = "\n") {
function baseChatReplace(value, name1, name2) { function baseChatReplace(value, name1, name2) {
if (value !== undefined && value.length > 0) { if (value !== undefined && value.length > 0) {
if (is_pygmalion) { value = substituteParams(value, is_pygmalion ? "You:" : name1, name2);
value = value.replace(/{{user}}:/gi, "You:");
value = value.replace(/<USER>:/gi, "You:");
}
value = value.replace(/{{user}}/gi, name1);
value = value.replace(/{{char}}/gi, name2);
value = value.replace(/<USER>/gi, name1);
value = value.replace(/<BOT>/gi, name2);
if (power_user.collapse_newlines) { if (power_user.collapse_newlines) {
value = collapseNewlines(value); value = collapseNewlines(value);
@ -1468,7 +1460,6 @@ async function Generate(type, automatic_trigger, force_name2) {
else if (type !== "swipe" && !isImpersonate) { else if (type !== "swipe" && !isImpersonate) {
chat.length = chat.length - 1; chat.length = chat.length - 1;
count_view_mes -= 1; count_view_mes -= 1;
openai_msgs.pop();
$('#chat').children().last().hide(500, function () { $('#chat').children().last().hide(500, function () {
$(this).remove(); $(this).remove();
}); });
@ -1495,6 +1486,7 @@ async function Generate(type, automatic_trigger, force_name2) {
// Compute anchors // Compute anchors
const topAnchorDepth = 8; const topAnchorDepth = 8;
const bottomAnchorThreshold = 8;
let anchorTop = ''; let anchorTop = '';
let anchorBottom = ''; let anchorBottom = '';
if (!is_pygmalion) { if (!is_pygmalion) {
@ -1554,14 +1546,20 @@ async function Generate(type, automatic_trigger, force_name2) {
is_pygmalion ? '<START>' : `This is how ${name2} should talk`; is_pygmalion ? '<START>' : `This is how ${name2} should talk`;
let mesExamplesArray = mesExamples.split(/<START>/gi).slice(1).map(block => `${blockHeading}\n${block.trim()}\n`); let mesExamplesArray = mesExamples.split(/<START>/gi).slice(1).map(block => `${blockHeading}\n${block.trim()}\n`);
if (main_api === 'openai') { // First message in fresh 1-on-1 chat reacts to user/character settings changes
const oai_chat = [...chat].filter(x => !x.is_system); if (chat.length) {
chat[0].mes = substituteParams(chat[0].mes);
if (type == 'swipe') {
oai_chat.pop();
} }
setOpenAIMessages(oai_chat); // 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') {
setOpenAIMessages(coreChat);
setOpenAIMessageExamples(mesExamplesArray); setOpenAIMessageExamples(mesExamplesArray);
} }
@ -1574,7 +1572,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: ");
} }
@ -1592,31 +1590,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;
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(/<USER>/gi, name1);
chat[j]['mes'] = chat[j]['mes'].replace(/<BOT>/gi, charName);
}
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
@ -1624,7 +1610,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
@ -1669,17 +1654,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('');
@ -1745,39 +1721,37 @@ async function Generate(type, automatic_trigger, force_name2) {
generatedPromtCache += cycleGenerationPromt; generatedPromtCache += cycleGenerationPromt;
if (generatedPromtCache.length == 0) { if (generatedPromtCache.length == 0) {
if (main_api === 'openai') { if (main_api === 'openai') {
generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, anchorBottom); generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, bottomAnchorThreshold, anchorBottom);
} }
console.log('generating prompt'); console.log('generating prompt');
chatString = ""; chatString = "";
arrMes = arrMes.reverse(); arrMes = arrMes.reverse();
let is_add_personality = false;
arrMes.forEach(function (item, i, arr) {//For added anchors and others 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 == "") { if (textareaText == "") {
item = item.substr(0, item.length - 1); item = item.substr(0, item.length - 1);
} }
} }
if (i === arrMes.length - topAnchorDepth && count_view_mes >= topAnchorDepth && !is_add_personality) { if (i === arrMes.length - topAnchorDepth && !is_pygmalion) {
is_add_personality = true;
//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(' ');
if (personalityAndAnchor && !is_pygmalion) { if (personalityAndAnchor) {
item += "[" + personalityAndAnchor + ']\n'; 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); 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";
} }
if (is_pygmalion) { 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 + ":"; 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 == "") { if (textareaText == "") {
item = item + '\n' + name2 + ":"; item = item + '\n' + name2 + ":";
} }

View File

@ -7,7 +7,6 @@
import { import {
saveSettingsDebounced, saveSettingsDebounced,
substituteParams, substituteParams,
count_view_mes,
checkOnlineStatus, checkOnlineStatus,
setOnlineStatus, setOnlineStatus,
getExtensionPrompt, getExtensionPrompt,
@ -148,10 +147,6 @@ function setOpenAIMessages(chat) {
// clean openai msgs // clean openai msgs
openai_msgs = []; openai_msgs = [];
for (let i = chat.length - 1; i >= 0; i--) { 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 role = chat[j]['is_user'] ? 'user' : 'assistant';
let content = chat[j]['mes']; let content = chat[j]['mes'];
@ -160,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,19 +188,17 @@ function setOpenAIMessageExamples(mesExamplesArray) {
} }
} }
function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, anchorBottom) { function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, bottomAnchorThreshold, anchorBottom) {
openai_msgs = openai_msgs.reverse(); openai_msgs = openai_msgs.reverse();
let is_add_personality = false;
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 && !is_add_personality) { if (i === openai_msgs.length - topAnchorDepth) {
is_add_personality = true; let personalityAndAnchor = [charPersonality, anchorTop].filter(x => x).join(' ');
if ((anchorTop != "" || charPersonality != "")) { if (personalityAndAnchor) {
if (anchorTop != "") charPersonality += ' '; item = `[${name2} is ${personalityAndAnchor}]\n${item}`;
item = `[${name2} is ${charPersonality}${anchorTop}]\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; item = anchorBottom + "\n" + item;
} }