TheoKanning / openai-java

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

java.net.sockettimeoutexception read timed out #84

Closed cehio closed 1 year ago

cehio commented 1 year ago

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 storyArray = new ArrayList(); 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();

cryptoapebot commented 1 year ago

Absolutely.

OpenAiService service = new OpenAiService(token, 55);

CodeSPA commented 1 year ago

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

KiritosHacks commented 1 year ago

I think we need a api document about client

KiritosHacks commented 1 year ago

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));

cryptoapebot commented 1 year ago

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.

liangtian123 commented 1 year ago

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

githubuser100007 commented 1 year ago

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.

cryptoapebot commented 1 year ago

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

githubuser100007 commented 1 year ago

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?

cryptoapebot commented 1 year ago

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.