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

How to solve timeout?

Open luoyashuo opened this issue 2 years ago • 3 comments

My code is like following:

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;

import java.util.Arrays;
import java.util.Date;
import java.util.Vector;
import java.util.concurrent.TransferQueue;

public class OpenAiSpeaker {
    private OpenAiService service;
    private Vector<String> humans=new Vector<>();
    private Vector<String> ans=new Vector<>();
    private StringBuilder query=new StringBuilder();
    CompletionRequest completionRequest;
    public OpenAiSpeaker() {
        this.service=new OpenAiService(official.openAiKey);
        completionRequest=CompletionRequest.builder()
                .model("text-davinci-003")
                .temperature(0.0)
                .maxTokens(512)
                .topP(0.1)
                .frequencyPenalty(0.0)
                .presencePenalty(0.6)
                .stop(Arrays.asList("Human:", "AI:"))
                //.echo(true)
                .user("testuser")
                .build();
    }
    public String speak(String q){

        humans.add(q);
        query.append("Human:").append(q);
        System.out.println(query);
        completionRequest.setPrompt(q.toString());
        String ret=this.service.createCompletion(this.completionRequest).getChoices().get(0).getText();
        query.append("AI:").append(ret);
        ans.add(ret);
        System.out.println(new Date());
        return ret;
    }
    public static void main(String[] args){
        System.out.println(new Date());
        OpenAiSpeaker o= new OpenAiSpeaker();
        o.completionRequest.setPrompt(official.testStr+official.testStr2);
        System.out.println(o.service.createCompletion(o.completionRequest).getChoices().get(0).getText());

        System.out.println(o.speak(official.testStr+official.testStr2));

    }
}

The first call of service.createCompletion can process normally,while the second call wrapped bypublic String speak(String q) occurs an error like this: java.net.SocketTimeoutException: timeout sometimes BTW, I can call service.createCompletion always successfully,but if I wrapped it in a function ,it fails usually. Why? Here is full error hint:

Exception in thread "main" java.lang.RuntimeException: java.net.SocketTimeoutException: Connect timed out
	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.OpenAiService.createCompletion(OpenAiService.java:120)
	at Chatgpt.OpenAiSpeaker.main(OpenAiSpeaker.java:60)
Caused by: java.net.SocketTimeoutException: Connect timed out
	at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:546)
	at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
	at java.base/java.net.Socket.connect(Socket.java:633)
	at okhttp3.internal.platform.Platform.connectSocket(Platform.java:130)
	at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:263)
	at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:183)
	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
	at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:169)
	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at com.theokanning.openai.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:26)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
	at okhttp3.RealCall.execute(RealCall.java:81)
	at retrofit2.OkHttpCall.execute(OkHttpCall.java:204)
	at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:46)
	at io.reactivex.Observable.subscribe(Observable.java:10151)
	at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35)
	at io.reactivex.Observable.subscribe(Observable.java:10151)
	at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
	at io.reactivex.Single.subscribe(Single.java:2517)
	at io.reactivex.Single.blockingGet(Single.java:2001)
	... 2 more

Process finished with exit code 1

luoyashuo avatar Feb 27 '23 02:02 luoyashuo

sloves:https://github.com/TheoKanning/openai-java/issues/125

luoyashuo avatar Feb 27 '23 03:02 luoyashuo

See @luoyashuo comment above. The text-davinci-003 needs a longer timeout, especially if you are feeding it a lot of tokens in the prompts.

cryptoapebot avatar Feb 27 '23 04:02 cryptoapebot

please use the SSE protocol。 see https://github.com/TheoKanning/openai-java/pull/129, example repo is https://github.com/mrjiangyan/openai-java/blob/main/example/src/main/java/example/OpenAiApiExample.java

mrjiangyan avatar Feb 27 '23 05:02 mrjiangyan