-
Notifications
You must be signed in to change notification settings - Fork 240
Extend support for useFileOutput to stream
#309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
254b7df
4430e6b
139fb89
6a2156d
51d300b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,15 +48,18 @@ class ServerSentEvent { | |
| * @param {string} config.url The URL to connect to. | ||
| * @param {typeof fetch} [config.fetch] The URL to connect to. | ||
| * @param {object} [config.options] The EventSource options. | ||
| * @param {boolean} [config.options.useFileOutput] Whether to use the file output stream. | ||
| * @returns {ReadableStream<ServerSentEvent> & AsyncIterable<ServerSentEvent>} | ||
| */ | ||
| function createReadableStream({ url, fetch, options = {} }) { | ||
| const { useFileOutput = false, headers = {}, ...initOptions } = options; | ||
|
|
||
| return new ReadableStream({ | ||
| async start(controller) { | ||
| const init = { | ||
| ...options, | ||
| ...initOptions, | ||
| headers: { | ||
| ...options.headers, | ||
| ...headers, | ||
| Accept: "text/event-stream", | ||
| }, | ||
| }; | ||
|
|
@@ -84,9 +87,15 @@ function createReadableStream({ url, fetch, options = {} }) { | |
| break; | ||
| } | ||
|
|
||
| controller.enqueue( | ||
| new ServerSentEvent(event.event, event.data, event.id) | ||
| ); | ||
| let data = event.data; | ||
| if ( | ||
| useFileOutput && | ||
| typeof data === "string" && | ||
| (data.startsWith("https:") || data.startsWith("data:")) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to explicitly match that 🤔 That said, I did think today after reading this post on other AI apis that we should move to structured outputs. Perhaps the file stream should emit JSON. And in future we refactor the text streaming interface to do the same: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's a good callout. The trick is finding a good way to validate without parsing the whole thing and throwing away the result. I think we can still read lazily if we use a regex to apply some heuristics about its first chunk of content.
I see the advantages of typed outputs, but also quite like the experience we have now of emitting raw tokens. In any case, structured outputs would be a backwards-incompatible change, so we'd have to be clever about a migration. One way we could support both would be keeping |
||
| ) { | ||
| data = createFileOutput({ data, fetch }); | ||
| } | ||
| controller.enqueue(new ServerSentEvent(event.event, data, event.id)); | ||
|
|
||
| if (event.event === "done") { | ||
| break; | ||
|
|
@@ -104,7 +113,7 @@ function createReadableStream({ url, fetch, options = {} }) { | |
| * | ||
| * @param {object} config | ||
| * @param {string} config.url The URL to connect to. | ||
| * @param {typeof fetch} [config.fetch] The URL to connect to. | ||
| * @param {typeof fetch} [config.fetch] The fetch function. | ||
| * @returns {ReadableStream<Uint8Array>} | ||
| */ | ||
| function createFileOutput({ url, fetch }) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.