Merge pull request #244 from gidzzz/fix-openai-no-bottom-anchor

This commit is contained in:
Cohee
2023-05-06 11:48:49 +03:00
committed by GitHub
2 changed files with 17 additions and 13 deletions

View File

@ -1858,7 +1858,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
arrMes = arrMes.reverse(); arrMes = arrMes.reverse();
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 && !item.trim().startsWith(name1 + ":")) {
if (textareaText == "") { if (textareaText == "") {
item = item.substr(0, item.length - 1); item = item.substr(0, item.length - 1);
} }
@ -1868,24 +1868,26 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject,
//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) { if (personalityAndAnchor) {
item += "[" + personalityAndAnchor + ']\n'; 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 && item.trim().startsWith(name1 + ":") && !is_pygmalion) {//For add anchor in end
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"; if (anchorBottom) {
item = item.replace(/\n$/, " ");
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 && item.trim().startsWith(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 && !item.trim().startsWith(name1 + ":")) {//for add name2 when continue
if (textareaText == "") { if (textareaText == "") {
item = item + '\n' + name2 + ":"; item = item + '\n' + name2 + ":";
} }
} }
if ($.trim(item).indexOf(name1) === 0) { if (item.trim().startsWith(name1)) {
item = item.replace(name1 + ':', 'You:'); item = item.replace(name1 + ':', 'You:');
} }
} }
@ -2361,7 +2363,7 @@ function shouldContinueMultigen(getMessage) {
function extractNameFromMessage(getMessage, force_name2, isImpersonate) { function extractNameFromMessage(getMessage, force_name2, isImpersonate) {
const nameToTrim = isImpersonate ? name1 : name2; const nameToTrim = isImpersonate ? name1 : name2;
let this_mes_is_name = true; let this_mes_is_name = true;
if (getMessage.indexOf(nameToTrim + ":") === 0) { if (getMessage.startsWith(nameToTrim + ":")) {
getMessage = getMessage.replace(nameToTrim + ':', ''); getMessage = getMessage.replace(nameToTrim + ':', '');
getMessage = getMessage.trimStart(); getMessage = getMessage.trimStart();
} else { } else {

View File

@ -204,9 +204,11 @@ function generateOpenAIPromptCache(charPersonality, topAnchorDepth, anchorTop, b
item = `[${name2} is ${personalityAndAnchor}]\n${item}`; 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 && msg.role === "user") {//For add anchor in end
if (anchorBottom) {
item = anchorBottom + "\n" + item; item = anchorBottom + "\n" + item;
} }
}
msg["content"] = item; msg["content"] = item;
openai_msgs[i] = msg; openai_msgs[i] = msg;
@ -238,14 +240,14 @@ function parseExampleIntoIndividual(messageExampleString) {
let cur_str = tmp[i]; let cur_str = tmp[i];
// if it's the user message, switch into user mode and out of bot mode // if it's the user message, switch into user mode and out of bot mode
// yes, repeated code, but I don't care // yes, repeated code, but I don't care
if (cur_str.indexOf(name1 + ":") === 0) { if (cur_str.startsWith(name1 + ":")) {
in_user = true; in_user = true;
// we were in the bot mode previously, add the message // we were in the bot mode previously, add the message
if (in_bot) { if (in_bot) {
add_msg(name2, "system", "example_assistant"); add_msg(name2, "system", "example_assistant");
} }
in_bot = false; in_bot = false;
} else if (cur_str.indexOf(name2 + ":") === 0) { } else if (cur_str.startsWith(name2 + ":")) {
in_bot = true; in_bot = true;
// we were in the user mode previously, add the message // we were in the user mode previously, add the message
if (in_user) { if (in_user) {