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

How to handle stream responses? #604

Closed
jung-han opened this issue May 21, 2024 · 2 comments
Closed

How to handle stream responses? #604

jung-han opened this issue May 21, 2024 · 2 comments

Comments

@jung-han
Copy link

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.

@jung-han
Copy link
Author

related issue: axios/axios#479

@jung-han
Copy link
Author

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);
          }
        },
      });
    },
  });
};

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

No branches or pull requests

1 participant