openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Is it Possible to Use OpenAI's Chatbot GPT-4 with this library?

Open ercan2158 opened this issue 1 year ago • 2 comments

Hey there, I'm curious if anyone has had success using OpenAI's Chatbot GPT-4 with this library. And if not, do you think it's possible to do so in the future? Thanks for your insights!

ercan2158 avatar Mar 20 '23 20:03 ercan2158

I use it with gpt-3.5-turbo without problem. And from what I read so far, the api did not change from gpt-3.5-turbo to gpt-4.

kubecutle avatar Mar 21 '23 01:03 kubecutle

(This answer was written in March 2023) GPT-4 is available for selected users. Like the user above said, the api did not change from turbo to 4. You can join the waitlist here https://openai.com/waitlist/gpt-4-api

brunodpoliveira avatar Mar 23 '23 14:03 brunodpoliveira

I don't even have access to gpt-4 yet, but as long as the api doesn't change it will work fine. I updated the readme to show that.

TheoKanning avatar Apr 01 '23 19:04 TheoKanning

@TheoKanning Thank you for writing and maintaining this library. I have GPT-4 access and am having trouble getting it to work reliably with this API. The same code works if I change gpt-4 to gpt-3.5-turbo, but with gpt-4 I get the following exception most of the time:

Caused by: java.net.SocketTimeoutException: timeout
	at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:678)
	at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:686)
	at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.java:154)
	at okhttp3.internal.http2.Http2ExchangeCodec.readResponseHeaders(Http2ExchangeCodec.java:136)
	at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115)
	at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at com.theokanning.openai.service.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:28)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
	at okhttp3.RealCall.execute(RealCall.java:81)
	at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
	at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:46)
	at io.reactivex.Observable.subscribe(Observable.java:10151)
	at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35)
	at io.reactivex.Observable.subscribe(Observable.java:10151)
	at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
	at io.reactivex.Single.subscribe(Single.java:2517)
	at io.reactivex.Single.blockingGet(Single.java:2001)
	... 5 more

called from:

Exception in thread "main" java.lang.RuntimeException: java.net.SocketTimeoutException: timeout
	at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:45)
	at io.reactivex.internal.observers.BlockingMultiObserver.blockingGet(BlockingMultiObserver.java:90)
	at io.reactivex.Single.blockingGet(Single.java:2002)
	at com.theokanning.openai.service.OpenAiService.execute(OpenAiService.java:260)
	at com.theokanning.openai.service.OpenAiService.createChatCompletion(OpenAiService.java:129)

I say most of the time because occasionally when I switch from 3.5 turbo to 4, it works the first time, but then if I try and rerun it again it fails. Mentioning it in case it is useful.

jchucarroll avatar Apr 03 '23 21:04 jchucarroll

A couple things to check. First make sure that your chatgpt-4 is properly turned on. Mine took a day or two to "work itself out" after I got the notice. You can confirm using curl and the following json.

https://github.com/TheoKanning/openai-java/issues/200#issuecomment-1487432947

Second, and apologies if you already know from your logs, but make sure you are using the ChatCompletion. (And it looks like you are).

I'll retry using the GPT-4 model. But one last thing, try using the model "gpt-4-0314" instead of "gpt-4" as I think OpenAI just maps the shorter to whatever model is longer which might just be some small part of their API mapping getting overwhelmed.

Finally, do you set a timeout in our OpenAiService?

cryptoapebot avatar Apr 03 '23 23:04 cryptoapebot

Thanks for your quick response. I have been using ChatGpt-4 in both the playground and with their Python API for a bit, so I know that the API token works.

I tried both gpt-4 and gpt-4-0314 and got the same behavior. And yes, I am using ChatCompletion in the code.

Also no, I did not set a timeout.

jchucarroll avatar Apr 03 '23 23:04 jchucarroll

Answering my own question -- I extended the default timeout and it appears to be working fine now.

jchucarroll avatar Apr 05 '23 16:04 jchucarroll