How to cancel request ?
renaudcerrato opened this issue · 4 comments
renaudcerrato commented
I'm using your API in kotlin's coroutines, but the async API doesn't return any cancellable to cancel the HTTP request. What's the recommended way cancel pending requests?
CJCrafter commented
There isn't a native way to do this besides using a future (and using sync on that future instead of async)
If you want a native method added, you should consider opening a PR.
CJCrafter commented
I'm going to go ahead and close this since OpenAI doesn't provide a way to stop a request. Either ignore the response or use a future and cancel that (Which is the same as ignoring the response). You waste tokens, unfortunately, but this is the only solution.
renaudcerrato commented
Of course, you can cancel an OKHTTP request. The streaming can last several
seconds, and cancelling the request WILL close the socket and cancel the
stream.
renaudcerrato commented
Call call = new OkHttpClient().newCall(your request);
call.enqueue(new Callback() { // call off UI thread.
@Override
public void onFailure(Call call, IOException e) {
...
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
call.cancel(); // this is the proper way to cancel an async call.
Why not returning a cancellable wrapping that call? 🤔