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

TImeout being ignored

Open jareid opened this issue 1 year ago • 1 comments

I am currently getting tijavameouts.

I have the following code when setting up the OpenAIService.

            service = new OpenAiService(apiKey, Duration.ZERO);

and the call to set up the request

 // Process the user's message with OpenAI
        ChatCompletionRequest chatRequest = ChatCompletionRequest.builder( )
                                                                 .model( OPENAI_MODEL ) // see https://platform.openai.com/docs/models
                                                                  // if option enabled, send history
                                                                 .messages( !options.get( "disableSendingChatGPTHistory" ) ? returnHistoryAsList() : null )
                                                                 .maxTokens( 256 )
                                                                 .build( );

        ChatMessage response = service.createChatCompletion( chatRequest ).getChoices( )
                                                                          .get( 0 )
                                                                          .getMessage( );

This should be set uo to have zero timeout but I still get the following stack trace.

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.actionPerformed (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)

Does anyone have any idea what I am doing wrong and why it is still timing out

jareid avatar Sep 14 '23 13:09 jareid

I had the same problem and was resolved expliciting Duration.ZERO

I suggest you to debug the constructor method in order to assure that you are passing the correct duration

    public OpenAiService(final String token, final Duration timeout) {
        ObjectMapper mapper = defaultObjectMapper();
        OkHttpClient client = defaultClient(token, timeout);
        Retrofit retrofit = defaultRetrofit(client, mapper);

        this.api = retrofit.create(OpenAiApi.class);
        this.executorService = client.dispatcher().executorService();
    }

And i also suggest to be sure that you are using the same instance of the api service, let me know if you need further help, in case provide an example to replicate it

Vavaste avatar Oct 29 '23 15:10 Vavaste