Why is there no entry for webclient to set up http proxy? I don’t want to set up a proxy for the entire service. 😭

Comment From: tluo-github

I also need it.

Comment From: Hakenadu

Maybe this is what you need? (Example for OpenAiChatModel Usage)

@Configuration
public class HttpClientConfig {

    @Bean
    public WebClient.Builder webClientBuilder(final HttpClient httpClient) {
        return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient));
    }

    @Bean
    public RestClient.Builder restClientBuilder(final HttpClient httpClient) {
        return RestClient.builder().requestFactory(new ReactorNettyClientRequestFactory(httpClient));
    }

    @Bean
    public HttpClient httpClient(final @Value("${my.proxy.host}") String proxyHost,
                                 final @Value("${my.proxy.port}") Integer proxyPort,
                                 final @Value("${my.proxy.noproxy}") String noProxy) {
        return HttpClient.create().proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host(proxyHost).port(proxyPort).nonProxyHosts(noProxy).build());
    }
}

@Configuration
public class OpenAiConfig {

    @Bean
    public OpenAiChatModel openAiChatModel(final WebClient.Builder webClientBuilder,
                                           final RestClient.Builder restClientBuilder,
                                           final @Value("${my.openai.api-key}") String openAiApiKey) {
        final OpenAiApi openAiApi = new OpenAiApi(
                "https://api.openai.com",
                openAiApiKey,
                restClientBuilder,
                webClientBuilder
        );
        return new OpenAiChatModel(openAiApi);
    }
}

Comment From: hakusai22

Maybe this is what you need? (Example for OpenAiChatModel Usage)

```java @Configuration public class HttpClientConfig {

@Bean
public WebClient.Builder webClientBuilder(final HttpClient httpClient) {
    return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient));
}

@Bean
public RestClient.Builder restClientBuilder(final HttpClient httpClient) {
    return RestClient.builder().requestFactory(new ReactorNettyClientRequestFactory(httpClient));
}

@Bean
public HttpClient httpClient(final @Value("${my.proxy.host}") String proxyHost,
                             final @Value("${my.proxy.port}") Integer proxyPort,
                             final @Value("${my.proxy.noproxy}") String noProxy) {
    return HttpClient.create().proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host(proxyHost).port(proxyPort).nonProxyHosts(noProxy).build());
}

}

@Configuration public class OpenAiConfig {

@Bean
public OpenAiChatModel openAiChatModel(final WebClient.Builder webClientBuilder,
                                       final RestClient.Builder restClientBuilder,
                                       final @Value("${my.openai.api-key}") String openAiApiKey) {
    final OpenAiApi openAiApi = new OpenAiApi(
            "https://api.openai.com",
            openAiApiKey,
            restClientBuilder,
            webClientBuilder
    );
    return new OpenAiChatModel(openAiApi);
}

} ```

yes😍

Comment From: hakusai22

        this.webClient = webClientBuilder
                .baseUrl(baseUrl)
                .defaultHeaders(ApiUtils.getJsonContentHeaders(openAiToken))
                .clientConnector(new ReactorClientHttpConnector(reactor.netty.http.client.HttpClient)))
                .build();

Comment From: tluo-github

add proxy username and password ```java @Configuration public class HttpClientConfig { @Bean public WebClient.Builder webClientBuilder(final HttpClient httpClient) { return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)); }

@Bean
public RestClient.Builder restClientBuilder(final HttpClient httpClient) {
    return RestClient.builder().requestFactory(new ReactorNettyClientRequestFactory(httpClient));
}

@Bean
public HttpClient httpClient(final @Value("${my.proxy.host}") String proxyHost,
                             final @Value("${my.proxy.port}") Integer proxyPort,
                             final @Value("${my.proxy.noproxy}") String noProxy,
                             final @Value("${my.proxy.username}") String proxyUsername,
                             final @Value("${my.proxy.password}") String proxyPassword) {
    HttpClient httpClient = HttpClient.create()
            .proxy(proxy -> {
                ProxyProvider.Builder proxyBuilder = proxy.type(ProxyProvider.Proxy.HTTP)
                        .host(proxyHost)
                        .port(proxyPort)
                        .nonProxyHosts(noProxy);

                if (proxyUsername != null && !proxyUsername.isEmpty() && proxyPassword != null && !proxyPassword.isEmpty()) {
                    proxyBuilder.username(proxyUsername)
                            .password(password -> proxyPassword);
                }
                proxyBuilder.build();
            });
    return httpClient;
}

}```

Comment From: hakusai22

add proxy username and password

```java @Configuration public class HttpClientConfig { @Bean public WebClient.Builder webClientBuilder(final HttpClient httpClient) { return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)); }

@Bean
public RestClient.Builder restClientBuilder(final HttpClient httpClient) {
    return RestClient.builder().requestFactory(new ReactorNettyClientRequestFactory(httpClient));
}

@Bean
public HttpClient httpClient(final @Value("${my.proxy.host}") String proxyHost,
                             final @Value("${my.proxy.port}") Integer proxyPort,
                             final @Value("${my.proxy.noproxy}") String noProxy,
                             final @Value("${my.proxy.username}") String proxyUsername,
                             final @Value("${my.proxy.password}") String proxyPassword) {
    HttpClient httpClient = HttpClient.create()
            .proxy(proxy -> {
                ProxyProvider.Builder proxyBuilder = proxy.type(ProxyProvider.Proxy.HTTP)
                        .host(proxyHost)
                        .port(proxyPort)
                        .nonProxyHosts(noProxy);

                if (proxyUsername != null && !proxyUsername.isEmpty() && proxyPassword != null && !proxyPassword.isEmpty()) {
                    proxyBuilder.username(proxyUsername)
                            .password(password -> proxyPassword);
                }
                proxyBuilder.build();
            });
    return httpClient;
}

}

This requires spring-ai to add this code and release the version?

Comment From: tluo-github

I'm not sure, Spring AI is not yet fully developed, and it is currently evolving very rapidly.

Comment From: hakusai22

I'm not sure, Spring AI is not yet fully developed, and it is currently evolving very rapidly.

What is your current solution? My company has domestic/foreign machines😭

Comment From: tluo-github

I'm not sure, Spring AI is not yet fully developed, and it is currently evolving very rapidly.

What is your current solution? My company has domestic/foreign machines😭

Use an HTTP proxy to route to Singapore.

Comment From: hakusai22

I'm not sure, Spring AI is not yet fully developed, and it is currently evolving very rapidly.

What is your current solution? My company has domestic/foreign machines😭

Use an HTTP proxy to route to Singapore.

Is it a proxy for the entire service or WebClient.Builder?

Comment From: tluo-github

WebClient.Builder

Comment From: tha2015

From my personal perspective, I don't think the logic should be added to the project. OpenAiChatModel is already configurable by accepting WebClient.Builder/RestClient.Builder from outside. That should be enough to support using proxies if needed (some custom logic will be needed from users to build WebClient.Builder/RestClient.Builder).