Open shrft opened 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.
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.
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.
Additional context thank you for your work. I use your library for my personal chatgpt command line tool.