TheoKanning / openai-java

OpenAI Api Client in Java
MIT License
4.68k stars 1.16k forks source link

When using the API to request the interface, a 400 error is returned. #462

Open Xxm-YX opened 5 months ago

Xxm-YX commented 5 months ago

I used an nginx proxy server.

Nginx access_log

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort));
        OkHttpClient client = defaultClient(token, Duration.ofDays(1))
                .newBuilder()
                .proxy(proxy)
                .build();
        Retrofit retrofit = defaultRetrofit(client, mapper);
        OpenAiApi api = retrofit.create(OpenAiApi.class);
        OpenAiService service = new OpenAiService(api);
172.18.0.3 - - [04/Feb/2024:09:34:53 +0000] "CONNECT api.openai.com:443 HTTP/1.1" 400 157 "-" "-"

Console log

Caused by: java.io.IOException: Unexpected response code for CONNECT: 400
    at okhttp3.internal.connection.RealConnection.createTunnel(RealConnection.kt:483) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.RealConnection.connectTunnel(RealConnection.kt:262) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:201) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.9.3.jar!/:na]
    at com.theokanning.openai.service.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:28) ~[service-0.18.2.jar!/:na]
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) ~[okhttp-4.9.3.jar!/:na]
    at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154) ~[okhttp-4.9.3.jar!/:na]
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:204) ~[retrofit-2.9.0.jar!/:na]
    at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:46) ~[adapter-rxjava2-2.9.0.jar!/:na]
    at io.reactivex.Observable.subscribe(Observable.java:12284) ~[rxjava-2.2.21.jar!/:na]
    at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35) ~[adapter-rxjava2-2.9.0.jar!/:na]
    at io.reactivex.Observable.subscribe(Observable.java:12284) ~[rxjava-2.2.21.jar!/:na]
    at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35) ~[rxjava-2.2.21.jar!/:na]
    at io.reactivex.Single.subscribe(Single.java:3666) ~[rxjava-2.2.21.jar!/:na]
    at io.reactivex.Single.blockingGet(Single.java:2869) ~[rxjava-2.2.21.jar!/:na]
    ... 59 common frames omitted

I use nginx as a proxy to request data from Google and YouTube, and it returns data normally.

cfanlu commented 2 months ago

change base url, sample code: ObjectMapper mapper = defaultObjectMapper(); OkHttpClient client = defaultClient(apiKey, Duration.of(timeout, ChronoUnit.SECONDS)) .newBuilder() .build(); Retrofit retrofit = OpenAiService.defaultRetrofit(client, mapper) .newBuilder() .baseUrl(baseUrl) .build(); OpenAiApi api = retrofit.create(OpenAiApi.class); OpenAiService service = new OpenAiService(api);