jddev273 / streamed-chatgpt-api

A node module for streaming API responses from the ChatGPT APi.
MIT License
30 stars 6 forks source link

Feature Request: allow access to reader object in callback #2

Open shrft opened 1 year ago

shrft commented 1 year ago

Describe the solution you'd like I think it would be nice if reader object can be accessed within the response callback. I made change for myself since I wanted to cancel the stream in the middle. https://github.com/shrft/streamed-chatgpt-api/commit/60bbdc16e68163508c4ba42aa809a59b449ab5fe

I use it like this.

fetchStreamedChat({
    apiKey,
    messageInput: 'Hello, how are you?',
}, (responseChunk, reader) => {
    if(xxxx){
      reader.cancel();
    }
    // get the actual content from the JSON
    const content = JSON.parse(responseChunk).choices[0].delta.content;
    if (content) {
        process.stdout.write(content);
    }
});

Additional context thank you for your work. I use your library for my personal chatgpt command line tool.

jschuur commented 1 year ago

I was actually just looking at how to cancel a stream myself when I came across this patch.

I've just implemented this via @shrft's fork in my chatgpt-repl project.

jschuur commented 1 year ago

Having looked into this a little more, if the sole purpose of this is to cancel a stream, wouldn't it be better to pass in an abortController signal into the fetch call instead?

Something like:

const controller = new AbortController();

fetchStreamedChatContent({
  fetchOptions: {
    signal: controller.signal
  },
  // other options
  ...
  },
  // callbacks
  ...
);

And then in fetchChatResponseWithRetry:

  fetch(apiUrl, {
    method: 'POST',
    headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${apiKey}`,
  },
  body: options.body,
  ..fetchOptions
}),

I've kept this generic enough incase you want to override other fetch options too.

Now you can call controller.abort() from your code.