This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Implement streaming completion #16
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b048a6f
Simplest possible streaming support.
dglazkov 26c070d
Implement `streamCompletion`.
dglazkov 2b4c6c9
Add documentation.
dglazkov a114da1
Revert the schema change.
dglazkov f40c754
Address comments.
dglazkov beb13ae
Add streaming chat completion.
dglazkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** A function that converts from raw Completion response from OpenAI | ||
| * into a nicer object which includes the first choice in response from OpenAI. | ||
| */ | ||
| type ResponseFactory<Raw, Nice> = (response: Raw) => Nice; | ||
|
|
||
| /** | ||
| * A parser for the streaming responses from the OpenAI API. | ||
| * | ||
| * Conveniently shaped like an argument for WritableStream constructor. | ||
| */ | ||
| class OpenAIStreamParser<Raw, Nice> { | ||
| private responseFactory: ResponseFactory<Raw, Nice>; | ||
| onchunk?: (chunk: Nice) => void; | ||
| onend?: () => void; | ||
|
|
||
| constructor(responseFactory: ResponseFactory<Raw, Nice>) { | ||
| this.responseFactory = responseFactory; | ||
| } | ||
|
|
||
| /** | ||
| * Takes the ReadableStream chunks, produced by `fetch` and turns them into | ||
| * `CompletionResponse` objects. | ||
| * @param chunk The chunk of data from the stream. | ||
| */ | ||
| write(chunk: Uint8Array): void { | ||
| const decoder = new TextDecoder(); | ||
| const s = decoder.decode(chunk); | ||
| s.split('\n') | ||
| .map((line) => line.trim()) | ||
| .filter((line) => line.length > 0) | ||
| .forEach((line) => { | ||
| const pos = line.indexOf(':'); | ||
| const name = line.substring(0, pos); | ||
| if (name !== 'data') return; | ||
| const content = line.substring(pos + 1).trim(); | ||
| if (content.length == 0) return; | ||
| if (content === '[DONE]') { | ||
| this.onend?.(); | ||
| return; | ||
| } | ||
| try { | ||
| const parsed = JSON.parse(content); | ||
| this.onchunk?.(this.responseFactory(parsed)); | ||
| } catch (e) { | ||
| console.error('Failed parsing streamed JSON chunk', e); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A transform stream that takes the streaming responses from the OpenAI API | ||
| * and turns them into useful response objects. | ||
| */ | ||
| export class StreamCompletionChunker<Raw, Nice> | ||
| implements TransformStream<Uint8Array, Nice> | ||
| { | ||
| writable: WritableStream<Uint8Array>; | ||
| readable: ReadableStream<Nice>; | ||
|
|
||
| constructor(responseFactory: ResponseFactory<Raw, Nice>) { | ||
| const parser = new OpenAIStreamParser(responseFactory); | ||
| this.writable = new WritableStream(parser); | ||
| this.readable = new ReadableStream({ | ||
| start(controller) { | ||
| parser.onchunk = (chunk: Nice) => controller.enqueue(chunk); | ||
| parser.onend = () => controller.close(); | ||
| }, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this