Skip to content

Commit 711de05

Browse files
committed
Keep replacing until mismatch
1 parent 6ef08d0 commit 711de05

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

mikupad.html

+26-8
Original file line numberDiff line numberDiff line change
@@ -3879,28 +3879,46 @@
38793879
const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
38803880
const finalPrompt = assembleFinalPrompt(additionalContextPrompt);
38813881

3882+
let isReplacing = false;
38823883
predict(finalPrompt, leftPromptChunks.length, (chunk) => {
38833884
let stop = false;
38843885
let omitChunk = false;
38853886

3887+
let wasReplacing = isReplacing;
3888+
isReplacing = false;
3889+
38863890
if (rightPromptChunks[0]) {
3887-
const trimmedChunk = chunk.content.replace(/^ +| +$/gm, "")
3888-
if (trimmedChunk[0] == rightPromptChunks[0].content[0]) {
3891+
const trimmedChunk = chunk.content.replace(/^ /, "");
3892+
if (rightPromptChunks[0].content[0] === chunk.content[0] ||
3893+
rightPromptChunks[0].content[0] === trimmedChunk[0]
3894+
) {
38893895
omitChunk = true;
38903896
if (chunk.content[0] == ' ' && rightPromptChunks[0].content[0] != ' ') {
3897+
if (rightPromptChunks[0].type !== 'user')
3898+
console.warn("Predicted token changed, this shouldn't happen.");
38913899
rightPromptChunks[0].content = ' ' + rightPromptChunks[0].content;
3892-
} else if (rightPromptChunks[0].type === 'user') {
3893-
if (rightPromptChunks[0].content === chunk.content) {
3894-
rightPromptChunks[0] = chunk;
3895-
} else if (rightPromptChunks[0].content.startsWith(chunk.content)) {
3900+
}
3901+
if (rightPromptChunks[0].type === 'user') {
3902+
if (rightPromptChunks[0].content.startsWith(chunk.content)) {
38963903
rightPromptChunks[0].content = rightPromptChunks[0].content.substring(chunk.content.length);
3904+
if (!rightPromptChunks[0].content)
3905+
rightPromptChunks.shift();
38973906
omitChunk = false;
3907+
isReplacing = true;
38983908
}
38993909
}
39003910
stop = true;
39013911
}
39023912
}
3903-
3913+
3914+
// When replacing, we continue until any mismatch.
3915+
if (wasReplacing && !isReplacing) {
3916+
if (rightPromptChunks.length !== 0)
3917+
return false;
3918+
// This means that the mismatch was caused by the end of the chunks to be replaced.
3919+
isReplacing = false;
3920+
}
3921+
39043922
if (!omitChunk) {
39053923
setTokens(t => t + (chunk?.completion_probabilities?.length ?? 1));
39063924
leftPromptChunks.push(chunk);
@@ -3910,7 +3928,7 @@
39103928
...rightPromptChunks
39113929
]);
39123930

3913-
return !stop;
3931+
return !stop || isReplacing;
39143932
});
39153933

39163934
return true;

0 commit comments

Comments
 (0)