TheoKanning / openai-java

OpenAI Api Client in Java
MIT License
4.78k stars 1.2k forks source link

SocketTimeoutException with OpenAiService#createCompletion #5

Closed Ugleh closed 2 years ago

Ugleh commented 3 years ago

Randomly, unbeknownst to me, I get a SocketTimeoutExeption during some executions for createCompletion. I don't know how to increase the connection timeout for OpenAiService and was wondering if I could get some advice, thanks.

TheoKanning commented 3 years ago

There's no way to modify anything within OpenAiService, so if you want to increase any timeouts, you'll have to make your own OkHttpClient and Retrofit instances. Just copy the OpenAiService constructor code and modify the OkHttpClient as shown here https://www.baeldung.com/okhttp-timeouts#read

danielsawan commented 2 years ago

I have the same problem. Is it possible to simply have a setTimeout() method somewhere in the api ?

gbolcer commented 2 years ago

@TheoKanning Thank you for this toolkit. This is really useful.
If you can make the OpenAiApi public that would fix it or else just create a getter and setter for OpenAiApi api in OpenAiService? I'm using the api-0.6.0.jar and cleint-0.6.0.jar so would prefer not to have to have another code copy if i could avoid it.

gbolcer commented 2 years ago

Just more info. After switching to text-davinci-002, it takes more time than the default timeout about 60% of the time.

gbolcer commented 2 years ago

I ended up just importing the project as a gradle project into eclipse and set the dependency. I still think it'd be useful to have a parameter or to be able to access the api object directly to set it, but my problem is solved.

Thank you again!

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new AuthenticationInterceptor(token))
                .connectionPool(new ConnectionPool(8, 42, TimeUnit.SECONDS))
                .connectTimeout(42, TimeUnit.SECONDS)
                .readTimeout(42, TimeUnit.SECONDS)
                .writeTimeout(42, TimeUnit.SECONDS)
                .build();
TheoKanning commented 2 years ago

@gbolcer you beat me to it haha. I just added a timeout option to fix this.

I also added much easier way to provide your own OpenAiApi in case you want to customize something else without copying the entire OpenAiService.

Version 0.7.0 with the changes will be available very soon

TheoKanning commented 2 years ago

Thank you all for pointing this out to me! Version 0.7.0 is live with the new timeout parameter, marking as closed :+1:

gbolcer commented 2 years ago

Again, kudos for the api. This is extremely useful.