Skip to content
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

Extend support for useFileOutput to stream #309

Merged
merged 5 commits into from
Sep 25, 2024
Merged

Extend support for useFileOutput to stream #309

merged 5 commits into from
Sep 25, 2024

Conversation

mattt
Copy link
Contributor

@mattt mattt commented Sep 23, 2024

No description provided.

lib/stream.js Outdated Show resolved Hide resolved
@mattt mattt requested a review from aron September 23, 2024 13:09
lib/stream.js Outdated Show resolved Hide resolved
if (
useFileOutput &&
typeof data === "string" &&
(data.startsWith("https:") || data.startsWith("data:"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to explicitly match that data consists of only a valid data uri. There is a reasonable chance that a language model might emit a data: line that starts with "data:" but less so that it will emit a line that consists only of a well formed data-uri.

🤔 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.

data: {"type": "url", value: "data://..."}

And in future we refactor the text streaming interface to do the same:

data: {"type": "string", "data: and some more text"}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to explicitly match that data consists of only a valid data uri. There is a reasonable chance that a language model might emit a data: line that starts with "data:" but less so that it will emit a line that consists only of a well formed data-uri.

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.

🤔 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.

data: {"type": "url", value: "data://..."}

And in future we refactor the text streaming interface to do the same:

data: {"type": "string", "data: and some more text"}

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 Accept: text/event-stream as-is, but adding support for Accept: text/event-stream+json. The client libraries could start sending that as needed to opt into structured outputs.

@mattt mattt merged commit 7f02a64 into main Sep 25, 2024
19 checks passed
@mattt mattt deleted the mattt/stream-file branch September 25, 2024 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants