0

I have the issue where whenever I attempt to get a response from the ChatGPT Java API by Theo Kanning, I get the error message java.net.SocketTimeoutException: timeout after waiting ~10 seconds. I was wondering if there is a way to extend the limit, as all of the responses I've seen before regarding this error code mention OkHttp which I am not using, or editing CONTEXT.XML which I cannot find in my project files.

The code:

// [rest of function, does not include Socket class]
Thread thread = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      String response = queryGPT();
      System.out.println(response);
    } catch (Exception e) {
      System.out.println("There was an error: "+e);
    }
  }
});
thread.start();
queryGPT() {
  String query = myQuery;
  OpenAiService service = new OpenAiService(myKey);
  CompletionRequest request = CompletionRequest.builder()
    .prompt(myQuery)
    .model("text-davinci-003")
    .temperature(0.5)
    .maxTokens(1500)
    .frequencyPenalty(0.0)
    .presencePenalty(0.0)
    .bestOf(1)
    .echo(false)
    .build();
  List<CompletionChoice> response = service.createCompletion(request).getChoices();
  return response.get(0).getText();
}
3
  • As per TheoKanning openAi documentation you can pass timeout as OkHttpClient client = defaultClient(token, timeout) or OpenAiService service = new OpenAiService(myKey, 30); // The default value is 10 seconds, so you can set it to a higher value, such as 30 seconds Commented May 15, 2023 at 18:16
  • The SocketTimeOut is optional and is designed to close connections that don't have data transmission for a certain time. You can increase it to multiple minutes or simply disable it by setting it to 0. Commented May 15, 2023 at 18:17
  • Ask ChatGPT? ;-) Commented Jun 6, 2023 at 0:48

1 Answer 1

0

The second parameter to OpenAiService is the timeout specified as Duration. Here is 60 seconds.

OpenAiService api = new OpenAiService(OPENAI_API_KEY, Duration.ofSeconds(60));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.