stream
parameter to true
in the model request.
This tells the model to start streaming the response as soon as it starts generating the response.
Here is an example that uses OpenAI:
addQuestion
, updateAnswer
, and done
. The addQuestion
action adds a new question to the conversation,
the updateAnswer
action updates the answer with the new chunk, and the done
action marks the end of the last answer.
Note that we are creating a new array with the new chunk and updating the state with this new array, if you reuse the existing array, React will not re-render the component.
useReducer
hook. The useReducer
hook takes the reducer function and the initial state,
and returns the current state and a dispatch function. The hook has two parts - the state and the dispatch function. The state is the current state of the component,
and we use this to show the current conversation. The dispatch function is used to send actions to the reducer, which updates the state.
As you saw in an earlier snippet, we call dispatch
with the action when we read a new chunk from the stream.
The snippet below shows how to use the useReducer
hook to update the state with the new chunk, and how to use the state in the component to display the conversation.
}
of the response.
Which means that the client will not be able to parse the response until the very end, and will not be able to display the response until the very end.
Which is the opposite of what we want with streaming responses.
To solve this, you need to use a slightly different format. A common way to handle this is to have structured metadata at the beginning of the response, and then stream the content.
You also need a unique delimiter to separate the metadata from the content. This way, the client can parse the metadata and start displaying the content as it arrives.
Here is an example of how you can structure the response that you send from the backend.
First, the iteratorToStream
function needs to be updated to send the metadata. We’ll modify it to accept a metadata string (we’ll stringify
the JSON to generate it),
and send the metadata and the delimiter when the stream starts, before any of the iterator chunks are streamed: