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

How to set proxy for completion requests ?

Open jiangying000 opened this issue 2 years ago • 5 comments

I am seeing a lot of timeout exception, and setting larger timeout like https://github.com/TheoKanning/openai-java/issues/125 did not resolve this issue, how can I set proxy for requests ?

java.lang.RuntimeException: java.net.SocketTimeoutException: connect timed out
	at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:46) ~[rxjava-2.2.21.jar:na]
	at io.reactivex.internal.observers.BlockingMultiObserver.blockingGet(BlockingMultiObserver.java:93) ~[rxjava-2.2.21.jar:na]
	at io.reactivex.Single.blockingGet(Single.java:2870) ~[rxjava-2.2.21.jar:na]
	at com.theokanning.openai.service.OpenAiService.execute(OpenAiService.java:217) ~[service-0.11.0.jar:na]
	at com.theokanning.openai.service.OpenAiService.createCompletion(OpenAiService.java:88) ~[service-0.11.0.jar:na]

jiangying000 avatar Mar 03 '23 02:03 jiangying000

relate to #140

jiangying000 avatar Mar 03 '23 03:03 jiangying000

temp solution: run with

-DsocksProxyHost=localhost -DsocksProxyPort=1080

jiangying000 avatar Mar 03 '23 06:03 jiangying000

temp solution: run with

-DsocksProxyHost=localhost -DsocksProxyPort=1080

it did't work for me, so i transform the socket5 proxy to http proxy, then use -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=1087 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=1087 the tool which i used is: https://github.com/oyyd/http-proxy-to-socks

coldairance avatar Mar 03 '23 07:03 coldairance

https://www.cnblogs.com/crazyyang/articles/6281432.html

luoweiwei0908 avatar Mar 03 '23 08:03 luoweiwei0908

一个临时性的解决方案,使用OpenAiService(OpenAiApi api)的构造函数,生成OpenaiService的实例

我将我使用的解决方案附上,希望能提供帮助,也希望作者能更新api。我不喜欢墙

public class OpenAIServiceCustom extends OpenAiService {
    public static OkHttpClient defaultClient(String token, Duration timeout) {
        return new OkHttpClient.Builder()
                .addInterceptor(new AuthenticationInterceptor(token))
                .connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
                .readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
                //请求都是基于okhttp发出的,只要更改了okhttp的代理设置,那么retrofit就会自动使用代理
                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ip","port")))
                .build();
    }
    public static OpenAiApi buildApi(String token, Duration timeout) {
        ObjectMapper mapper = defaultObjectMapper();
        OkHttpClient client = defaultClient(token, timeout);
        Retrofit retrofit = defaultRetrofit(client, mapper);
        return retrofit.create(OpenAiApi.class);
    }
    public OpenAIServiceCustom(String token, Duration timeout) {
        super(buildApi(token, timeout));
    }
}

FHamster avatar Mar 05 '23 13:03 FHamster