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

Always getting a connection timeout error.java.net.SocketTimeoutException: connect timed out

Open 1453069556 opened this issue 1 year ago • 5 comments

@Test void contextLoads() { // 创建 OpenAI 客户端 OpenAiService service = new OpenAiService( TOKEN, Duration.ofSeconds(500L)); // 创建测试消息 ChatMessage testMessage = new ChatMessage(); testMessage.setContent("说这是一个测试。"); testMessage.setRole("user");

    // 创建消息列表
    List<ChatMessage> messages = new ArrayList<>();
    messages.add(testMessage);

    // 设置请求参数
    ChatCompletionRequest request = ChatCompletionRequest.builder()
            .messages(messages)
            .model("gpt-3.5-turbo")
            .build();
    // 调用 GPT-3 API
    ChatCompletionResult chatCompletion = service.createChatCompletion(request);

    // 打印 API 返回结果
    chatCompletion.getChoices().forEach(System.out::println);
}

java.lang.RuntimeException: java.net.SocketTimeoutException: connect timed out

My network can connect to the OpenAI website.

1453069556 avatar Mar 10 '23 01:03 1453069556

image

1453069556 avatar Mar 10 '23 01:03 1453069556

要vpn,代码里面也要设置代理

ruweizhong avatar Mar 10 '23 09:03 ruweizhong

设置代理的示例有嘛

ay15172073139 avatar Mar 11 '23 16:03 ay15172073139

Readme file for the project now includes a section on setting up a proxy.

https://github.com/TheoKanning/openai-java https://github.com/TheoKanning/openai-java#adding-a-proxy

cryptoapebot avatar Mar 11 '23 16:03 cryptoapebot

设置代理的示例有嘛

Hello, have you solved it?

1999LonelyCHEN avatar Mar 15 '23 03:03 1999LonelyCHEN

Hi all, are there any news?

I receive SocketTimeoutException with 50% chance.

First I thought this is because GPT servers are overloaded, but this is not correct - I can use Playground without any problems.

Please help, any hint.

P. S.: timeout is set to 60 sec but this does not help. And I'm not from a China or like that (location is EU).

aiboost avatar Mar 30 '23 10:03 aiboost

Instead or in addition to the code, can you run your java w/ the property to see if it makes any difference?

-Djava.net.useSystemProxies=true

I did find some useful stuff here to try also. https://stackoverflow.com/questions/376101/setting-jvm-jre-to-use-windows-proxy-automatically

I'm wondering if it's a SOCKS vs HTTP proxy issues.

cryptoapebot avatar Mar 30 '23 14:03 cryptoapebot

The default timeout is 10 seconds. If you want to increase this, use the second optional constructor argument when making your service. e.g.

val gpt = OpenAiService(openApiKey, Duration.ofSeconds(30))

robbiebowman avatar Apr 06 '23 02:04 robbiebowman