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

java.net.sockettimeoutexception read timed out

Open cehio opened this issue 2 years ago • 10 comments

Randomically I receive "java.net.sockettimeoutexception read timed out " Maybe chatgpt´s server is full and delays sending response. After 10 seconds with no answer a Timeout Exceptions appears. Could this time out be increased? This my code

String token = System.getenv("OPENAI_TOKEN"); ChatResponse = ""; OpenAiService service = new OpenAiService(token); Engine davinci = service.getEngine("text-davinci-003"); ArrayList<CompletionChoice> storyArray = new ArrayList<CompletionChoice>(); System.out.println("\nBrewing up a story..."); CompletionRequest completionRequest = CompletionRequest.builder() .prompt(message) .temperature(0.6) //0.9 .maxTokens(450) .topP(1.0) //1.0 .frequencyPenalty(0.4) //0.0 .presencePenalty(0.1) //0.6 .build(); service.createCompletion("text-davinci-003", completionRequest).getChoices().forEach(line -> { storyArray.add(line); }); ChatResponse = storyArray.get(0).toString();

cehio avatar Feb 03 '23 19:02 cehio

Absolutely.

OpenAiService service = new OpenAiService(token, 55);

cryptoapebot avatar Feb 03 '23 20:02 cryptoapebot

I also encountered this problem "java.net.SocketException: Connection reset" with HTTP response code 500

CodeSPA avatar Feb 13 '23 08:02 CodeSPA

I think we need a api document about client

KiritosHacks avatar Feb 13 '23 11:02 KiritosHacks

I also encountered this problem "java.net.SocketException: Connection reset" with HTTP response code 500

you can use OpenAiService service = new OpenAiService("token", Duration.ofSeconds(55));

KiritosHacks avatar Feb 13 '23 12:02 KiritosHacks

Also, due to the popularity of ChatGPT, you do get random failures on the system. Make sure your http.getCode handles non-successful http codes.

cryptoapebot avatar Feb 13 '23 16:02 cryptoapebot

What version are you using, I tried, but it didn't work

liangtian123 avatar Mar 04 '23 08:03 liangtian123

So whenever we receive a:

java.net.SocketTimeoutException: Read timed out

We can assume it's ChatGPT? Because we get these when running multiple threads, and the other threads are doing just fine.

githubuser100007 avatar Mar 19 '23 16:03 githubuser100007

They did recently change the concurrent call limits. Check for 429 status code on those.

https://community.openai.com/t/chatgpt-usage-limits/23920 https://help.openai.com/en/articles/6891829-error-code-429-rate-limit-reached-for-requests

cryptoapebot avatar Mar 19 '23 16:03 cryptoapebot

All I am getting is a read timeout error. No error code from the chat completion call since it is throwing an exception. Is there a best practice for running this multi-threaded? I am creating multiple service objects, should I not be? Should I just have one service object?

githubuser100007 avatar Mar 19 '23 16:03 githubuser100007

Interesting, I've only used one service. I think you can run one service per key and it should be fine. I'll try to test multiple services in the same program, but it should be thread safe.

Just another thought. If you are using List or Array structures for loading/feeding messages or grabbing results, make sure that they are not being re-used. One technique is to use a guava concurrent hashmap on user, service, or conversation id. The other trick is to try to use a Java synchronized keyword on a method, lamda, or block of code to try to isolate if there is an issue.

cryptoapebot avatar Mar 19 '23 16:03 cryptoapebot