openai-java
openai-java copied to clipboard
Always getting a connection timeout error.java.net.SocketTimeoutException: connect timed out
@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.
要vpn,代码里面也要设置代理
设置代理的示例有嘛
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
设置代理的示例有嘛
Hello, have you solved it?
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).
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.
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))