CJCrafter/ChatGPT-Java-API

How to cancel request ?

renaudcerrato opened this issue · 4 comments

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?

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.

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.

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? 🤔