vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.94k stars 1.47k forks source link

Export utils to parse AI stream responses on the client #2341

Open wong2 opened 3 months ago

wong2 commented 3 months ago

Feature Description

We're using the AI SDK on the server side only, as our client-side code already includes the logic to handle message states and API calls. It would be helpful to have utilities extracted from useChat to parse the stream responses generated by the AI SDK.

Use Case

No response

Additional context

No response

lgrammel commented 3 months ago

This is great, since we want to document the protocol in the coming weeks. Can you share which exact functionality / functions you would need to access? some of it is exposed but not documented

wong2 commented 3 months ago

Our user cases include:

lgrammel commented 3 months ago

@wong2 we have started documenting the data stream protocol: https://sdk.vercel.ai/docs/ai-sdk-ui/stream-protocol#data-stream-protocol

The @ai-sdk/ui-utils package exposes utilities for formatting and parsing these stream parts (warning: the api is internal/unstable and might change).

wong2 commented 3 months ago

Great!

xl0 commented 3 months ago

Can only second @wong2 . I love the server-side stuff because it abstracts away the differences between providers, but the UI part just feels wrong. I would very much prefer simple building blocks that you can use individually, and then, fine, put a useChat on top of them. Right now useChat is complicated because it's the only official way to handle the server responses, and thus it's hard to use for anything non-standard. I don't think the solution is to extend useChat even further, but rather to expose the building blocks so people can handle their use-case on their own.