|
4039 | 4039 | const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
|
4040 | 4040 | const finalPrompt = assembleFinalPrompt(additionalContextPrompt, templateReplacements);
|
4041 | 4041 |
|
| 4042 | + let isReplacing = false; |
4042 | 4043 | predict(finalPrompt, leftPromptChunks.length, (chunk) => {
|
4043 | 4044 | let stop = false;
|
4044 | 4045 | let omitChunk = false;
|
4045 | 4046 |
|
| 4047 | + let wasReplacing = isReplacing; |
| 4048 | + isReplacing = false; |
| 4049 | + |
4046 | 4050 | 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 | + ) { |
4049 | 4055 | omitChunk = true;
|
4050 | 4056 | if (chunk.content[0] == ' ' && rightPromptChunks[0].content[0] != ' ') {
|
| 4057 | + if (rightPromptChunks[0].type !== 'user') |
| 4058 | + console.warn("Predicted token changed, this shouldn't happen."); |
4051 | 4059 | 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)) { |
4056 | 4063 | rightPromptChunks[0].content = rightPromptChunks[0].content.substring(chunk.content.length);
|
| 4064 | + if (!rightPromptChunks[0].content) |
| 4065 | + rightPromptChunks.shift(); |
4057 | 4066 | omitChunk = false;
|
| 4067 | + isReplacing = true; |
4058 | 4068 | }
|
4059 | 4069 | }
|
4060 | 4070 | stop = true;
|
4061 | 4071 | }
|
4062 | 4072 | }
|
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 | + |
4064 | 4082 | if (!omitChunk) {
|
4065 | 4083 | setTokens(t => t + (chunk?.completion_probabilities?.length ?? 1));
|
4066 | 4084 | leftPromptChunks.push(chunk);
|
|
4070 | 4088 | ...rightPromptChunks
|
4071 | 4089 | ]);
|
4072 | 4090 |
|
4073 |
| - return !stop; |
| 4091 | + return !stop || isReplacing; |
4074 | 4092 | });
|
4075 | 4093 |
|
4076 | 4094 | return true;
|
|
0 commit comments