OpenAI icon indicating copy to clipboard operation
OpenAI copied to clipboard

Cancel the async openAI.chatsStream(query: query)

Open thekoc opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

Simple use case:

let task = Task {
    for try await result in openAI.chatsStream(query: query) {
        print(result)
    }
}

During the streaming, if I cancel the task manually (e.g. The stop generation button in the official ChatGPT app), I expect the stream to be stopped.

task.cancel() // Expecting the network connection to be cancelled too.

Unexpectedly, by monitoring the network activity, the generation does not stop even if the task has been cancelled.

This is due to (by my poor understanding) the underlying StreamingSession does not stop alongside with the task accordingly.

// func performStreamingRequest<ResultType: Codable>(request: any URLRequestBuildable, onResult: @escaping (Result<ResultType, Error>) -> Void, completion: ((Error?) -> Void)?)
let session = StreamingSession<ResultType>(urlRequest: request)
// ...
session.perform() // Once initialized never stopped

Describe the solution you'd like Use the onTermination method to handle cancellation from the outside task.

This pull request might also help

Describe alternatives you've considered Empty

Additional context Empty

thekoc avatar Apr 22 '24 02:04 thekoc

This might be useful to you @thekoc

https://github.com/longseespace/OpenAI/commit/a290a1a119fc465cdb1740472242fbfca57069b9

longseespace avatar May 01 '24 11:05 longseespace

I also need this feature, and would like to be able to proactively cancel stream requests.

Why hasn't the developer handled this issue?

tisfeng avatar May 11 '24 12:05 tisfeng

I needed this feature, but nobody responded, so I implemented it myself, thanks https://github.com/longseespace/OpenAI/commit/a290a1a119fc465cdb1740472242fbfca57069b9 .

In case others need it too, I submitted a PR https://github.com/MacPaw/OpenAI/pull/214 .

tisfeng avatar Jun 03 '24 15:06 tisfeng