How to handle stream responses?
jung-han opened this issue · comments
Hello @ecyrbe! Thank you for creating a good library.
I am receiving great help during development.
I think it's related to this issue(#390), I have a question.
Currently, we are in a situation where we need to receive openai responses via streaming.
responseType: 'stream',
I am receiving a response with the Responsetype set to stream, but I am looking for a way to control it.
I thought onDownloadProgress
could be a solution, but the parameters do not contain the data being passed.
What method should I use to handle data in real time?
I think there was some talk about middleware. Is there anything that can be of reference?
�Thanks for reading! I'll wait for your reply.
related issue: axios/axios#479
with RQ.
import { useMutation } from '@tanstack/react-query';
import { StatusCodes } from 'http-status-codes';
import { z } from 'zod';
export const useExecutePromptMutation = ({ onStream, onError }) => {
return useMutation({
mutationFn: async ({ data }: { data: SendMessageDto }) => {
await api.sendMessage(data, {
responseType: 'stream',
onDownloadProgress: (progress) => {
const request = progress.event.target;
if (request.status === StatusCodes.OK || request.status === StatusCodes.CREATED) {
onStream(request.response); // response
} else {
onError(request.status);
}
},
});
},
});
};