使用OpenAiService设置Duration.ZERO仍遇SocketTimeoutException求助
问题:OpenAiService设置Duration.ZERO仍出现超时
我用com.theokanning.openai.service.OpenAIService开发ChatGPT的GUI应用,现在碰到超时问题。设置OpenAiService时用了这段代码:
service = new OpenAiService(apiKey, Duration.ZERO);
请求构建代码如下:
// 处理用户消息并调用OpenAI ChatCompletionRequest chatRequest = ChatCompletionRequest.builder() .model(OPENAI_MODEL) // 若未禁用则发送聊天历史 .messages(!options.get("disableSendingChatGPTHistory") ? returnHistoryAsList() : null) .maxTokens(256) .build(); ChatMessage response = service.createChatCompletion(chatRequest).getChoices() .get(0) .getMessage();
本以为设置成无超时就不会有问题,但还是出现了如下堆栈跟踪:
java.lang.RuntimeException: java.net.SocketTimeoutException: timeout at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow (ExceptionHelper.java:45) at io.reactivex.internal.observers.BlockingMultiObserver.blockingGet (BlockingMultiObserver.java:90) at io.reactivex.Single.blockingGet (Single.java:2002) at com.theokanning.openai.service.OpenAiService.execute (OpenAiService.java:321) at com.theokanning.openai.service.OpenAiService.createChatCompletion (OpenAiService.java:135) at com.jareid.openaiapp.api.APIHandler.askGPT_GetResponse (APIHandler.java:372) at com.jareid.openaiapp.ui.UserInterfaceScreen.lambda$new$4 (UserInterfaceScreen.java:121) at javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:1967) at javax.swing.AbstractButton$Handler.actionActionPerformed (AbstractButton.java:2308) at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:405) at javax.swing.DefaultButtonModel.setPressed (DefaultButtonModel.java:262) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased (BasicButtonListener.java:279) at java.awt.Component.processMouseEvent (Component.java:6635) at javax.swing.JComponent.processMouseEvent (JComponent.java:3342) at java.awt.Component.processEvent (Component.java:6400) at java.awt.Container.processEvent (Container.java:2263) at java.awt.Component.dispatchEventImpl (Component.java:5011) at java.awt.Container.dispatchEventImpl (Container.java:2321) at java.awt.Component.dispatchEvent (Component.java:4843) at java.awt.LightweightDispatcher.retargetMouseEvent (Container.java:4918) at java.awt.LightweightDispatcher.processMouseEvent (Container.java:4547) at java.awt.LightweightDispatcher.dispatchEvent (Container.java:4488) at java.awt.Container.dispatchEventImpl (Container.java:2307) at java.awt.Window.dispatchEventImpl (Window.java:2772) at java.awt.Component.dispatchEvent (Component.java:4843) at java.awt.EventQueue.dispatchEventImpl (EventQueue.java:772) at java.awt.EventQueue$4.run (EventQueue.java:721) at java.awt.EventQueue$4.run (EventQueue.java:715) at java.security.AccessController.doPrivileged (Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:85) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:95) at java.awt.EventQueue$5.run (EventQueue.java:745) at java.awt.EventQueue$5.run (EventQueue.java:743) at java.security.AccessController.doPrivileged (Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:85) at java.awt.EventQueue.dispatchEvent (EventQueue.java:742) at java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:203) at java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:124) at java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:109) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:101) at java.awt.EventDispatchThread.run (EventDispatchThread.java:90)
请问哪里操作有误,为什么还是会出现超时?
解答
问题根源
你用Duration.ZERO设置超时是错误的——这个库的OpenAiService构造器中,Duration.ZERO不会被识别为“无超时”,反而会触发默认的超时逻辑,甚至可能直接导致立即超时。
正确设置方式
要实现真正的无超时,需要手动构建OkHttpClient实例,将连接、读写超时都设为0(OkHttp中0代表无超时),再传入OpenAiService的构造器:
OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(0, TimeUnit.SECONDS) .readTimeout(0, TimeUnit.SECONDS) .writeTimeout(0, TimeUnit.SECONDS) .build(); OpenAiService service = new OpenAiService(apiKey, client);
额外注意事项
- 无超时设置在GUI应用中可能导致界面长时间卡顿,建议改用异步调用(比如使用RxJava的订阅方法而非
blockingGet),避免阻塞Swing事件线程。 - OpenAI的API本身存在最长响应时间限制,即使客户端设置了无超时,服务端也可能主动断开连接。
内容的提问来源于stack exchange,提问作者Jamie Reid
相关产品推荐
相关产品推荐

