|
3879 | 3879 | const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
|
3880 | 3880 | const finalPrompt = assembleFinalPrompt(additionalContextPrompt);
|
3881 | 3881 |
|
| 3882 | + let isReplacing = false; |
3882 | 3883 | predict(finalPrompt, leftPromptChunks.length, (chunk) => {
|
3883 | 3884 | let stop = false;
|
3884 | 3885 | let omitChunk = false;
|
3885 | 3886 |
|
| 3887 | + let wasReplacing = isReplacing; |
| 3888 | + isReplacing = false; |
| 3889 | + |
3886 | 3890 | 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 | + ) { |
3889 | 3895 | omitChunk = true;
|
3890 | 3896 | if (chunk.content[0] == ' ' && rightPromptChunks[0].content[0] != ' ') {
|
| 3897 | + if (rightPromptChunks[0].type !== 'user') |
| 3898 | + console.warn("Predicted token changed, this shouldn't happen."); |
3891 | 3899 | 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)) { |
3896 | 3903 | rightPromptChunks[0].content = rightPromptChunks[0].content.substring(chunk.content.length);
|
| 3904 | + if (!rightPromptChunks[0].content) |
| 3905 | + rightPromptChunks.shift(); |
3897 | 3906 | omitChunk = false;
|
| 3907 | + isReplacing = true; |
3898 | 3908 | }
|
3899 | 3909 | }
|
3900 | 3910 | stop = true;
|
3901 | 3911 | }
|
3902 | 3912 | }
|
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 | + |
3904 | 3922 | if (!omitChunk) {
|
3905 | 3923 | setTokens(t => t + (chunk?.completion_probabilities?.length ?? 1));
|
3906 | 3924 | leftPromptChunks.push(chunk);
|
|
3910 | 3928 | ...rightPromptChunks
|
3911 | 3929 | ]);
|
3912 | 3930 |
|
3913 |
| - return !stop; |
| 3931 | + return !stop || isReplacing; |
3914 | 3932 | });
|
3915 | 3933 |
|
3916 | 3934 | return true;
|
|
0 commit comments