Skip to content

Commit e61f5e2

Browse files
committed
Keep replacing until mismatch
1 parent f19cdb2 commit e61f5e2

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
@@ -4039,28 +4039,46 @@
40394039
const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
40404040
const finalPrompt = assembleFinalPrompt(additionalContextPrompt, templateReplacements);
40414041

4042+
let isReplacing = false;
40424043
predict(finalPrompt, leftPromptChunks.length, (chunk) => {
40434044
let stop = false;
40444045
let omitChunk = false;
40454046

4047+
let wasReplacing = isReplacing;
4048+
isReplacing = false;
4049+
40464050
if (rightPromptChunks[0]) {
4047-
const trimmedChunk = chunk.content.replace(/^ +| +$/gm, "")
4048-
if (trimmedChunk[0] == rightPromptChunks[0].content[0]) {
4051+
const trimmedChunk = chunk.content.replace(/^ /, "");
4052+
if (rightPromptChunks[0].content[0] === chunk.content[0] ||
4053+
rightPromptChunks[0].content[0] === trimmedChunk[0]
4054+
) {
40494055
omitChunk = true;
40504056
if (chunk.content[0] == ' ' && rightPromptChunks[0].content[0] != ' ') {
4057+
if (rightPromptChunks[0].type !== 'user')
4058+
console.warn("Predicted token changed, this shouldn't happen.");
40514059
rightPromptChunks[0].content = ' ' + rightPromptChunks[0].content;
4052-
} else if (rightPromptChunks[0].type === 'user') {
4053-
if (rightPromptChunks[0].content === chunk.content) {
4054-
rightPromptChunks[0] = chunk;
4055-
} else if (rightPromptChunks[0].content.startsWith(chunk.content)) {
4060+
}
4061+
if (rightPromptChunks[0].type === 'user') {
4062+
if (rightPromptChunks[0].content.startsWith(chunk.content)) {
40564063
rightPromptChunks[0].content = rightPromptChunks[0].content.substring(chunk.content.length);
4064+
if (!rightPromptChunks[0].content)
4065+
rightPromptChunks.shift();
40574066
omitChunk = false;
4067+
isReplacing = true;
40584068
}
40594069
}
40604070
stop = true;
40614071
}
40624072
}
4063-
4073+
4074+
// When replacing, we continue until any mismatch.
4075+
if (wasReplacing && !isReplacing) {
4076+
if (rightPromptChunks.length !== 0)
4077+
return false;
4078+
// This means that the mismatch was caused by the end of the chunks to be replaced.
4079+
isReplacing = false;
4080+
}
4081+
40644082
if (!omitChunk) {
40654083
setTokens(t => t + (chunk?.completion_probabilities?.length ?? 1));
40664084
leftPromptChunks.push(chunk);
@@ -4070,7 +4088,7 @@
40704088
...rightPromptChunks
40714089
]);
40724090

4073-
return !stop;
4091+
return !stop || isReplacing;
40744092
});
40754093

40764094
return true;

0 commit comments

Comments
 (0)