Merge pull request #2912 from Pyroboomka/fix/char-stream-stop-tokens
Fix an edge case when stop token is not filtered out in completion response.
This commit is contained in:
commit
2014130ea1
2 changed files with 22 additions and 1 deletions
|
|
@ -157,4 +157,20 @@ describe("stopAtStopTokens", () => {
|
|||
|
||||
expect(output.join("")).toBe("Hello world!");
|
||||
});
|
||||
|
||||
it("should handle stop token when remaining buffer is smaller than maximum stop token length", async () => {
|
||||
const mockStream = createMockStream(["Hello world!STOP"]);
|
||||
const stopTokens: string[] = [
|
||||
"STOP",
|
||||
"STOP_TOKEN_THAT_IS_LARGER_THAN_BUFFER",
|
||||
];
|
||||
const result = stopAtStopTokens(mockStream, stopTokens);
|
||||
|
||||
const output = [];
|
||||
for await (const char of result) {
|
||||
output.push(char);
|
||||
}
|
||||
|
||||
expect(output.join("")).toBe("Hello world!");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -70,7 +70,8 @@ export async function* noFirstCharNewline(stream: AsyncGenerator<string>) {
|
|||
* 2. Otherwise, buffers incoming chunks and checks for stop tokens.
|
||||
* 3. Yields characters one by one if no stop token is found at the start of the buffer.
|
||||
* 4. Stops yielding and returns if a stop token is encountered.
|
||||
* 5. After the stream ends, yields any remaining buffered characters.
|
||||
* 5. After the stream ends, filters encountered stop tokens in remaining buffer.
|
||||
* 6. Yields any remaining buffered characters.
|
||||
*/
|
||||
export async function* stopAtStopTokens(
|
||||
stream: AsyncGenerator<string>,
|
||||
|
|
@ -106,6 +107,10 @@ export async function* stopAtStopTokens(
|
|||
}
|
||||
}
|
||||
}
|
||||
// Filter out the possible stop tokens from remaining buffer
|
||||
stopTokens.forEach((token) => {
|
||||
buffer = buffer.replace(token, "");
|
||||
});
|
||||
|
||||
// Yield any remaining characters in the buffer
|
||||
for (const char of buffer) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue