spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.35k stars 866 forks source link

Add support for custom WebClient.Builder in org.springframework.ai.openai.api.OpenAiApi #786

Closed jocax closed 2 months ago

jocax commented 6 months ago

Please do a quick search on GitHub issues first, the feature you are about to request might have already been requested.

Expected Behavior

It should be possible to use a custom WebClient.Builder in the Spring configuration. Currently only a RestClient.Builder is supported.

/**
 * Create a new chat completion api.
 *
 * @param baseUrl api base URL.
 * @param openAiToken OpenAI apiKey.
 * @param restClientBuilder RestClient builder.
 * @param webClientBuilder RestClient builder.
 * @param responseErrorHandler Response error handler.
 */
public OpenAiApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {

    this.restClient = restClientBuilder
            .baseUrl(baseUrl)
            .defaultHeaders(ApiUtils.getJsonContentHeaders(openAiToken))
            .defaultStatusHandler(responseErrorHandler)
            .build();

    this.webClient = webClientBuilder
            .baseUrl(baseUrl)
            .defaultHeaders(ApiUtils.getJsonContentHeaders(openAiToken))
            .build();
}

The class org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration needs to be updated as well to accept the WebClient.Builder.

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = OpenAiEmbeddingProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
        matchIfMissing = true)
public OpenAiEmbeddingClient openAiEmbeddingClient(OpenAiConnectionProperties commonProperties,
        OpenAiEmbeddingProperties embeddingProperties, RestClient.Builder restClientBuilder,

WebClient.Builder webClientBuilder, RetryTemplate retryTemplate, ResponseErrorHandler responseErrorHandler) {

    var openAiApi = openAiApi(embeddingProperties.getBaseUrl(), commonProperties.getBaseUrl(),
            embeddingProperties.getApiKey(), commonProperties.getApiKey(), restClientBuilder, webClientBuilder, responseErrorHandler);

    return new OpenAiEmbeddingClient(openAiApi, embeddingProperties.getMetadataMode(),
            embeddingProperties.getOptions(), retryTemplate);
} 

Current Behavior

Currently it is not possible to inject a custom WebClient.Builder into the constructor.

Context

Can not use the pre configured WebClient.Builder.

tha2015 commented 5 months ago

This is a duplicate of https://github.com/spring-projects/spring-ai/issues/609 which was fixed recently. See commit https://github.com/spring-projects/spring-ai/commit/377b5ffa61427ce94f49793b00f4617edfcc59bc

Adakole2020 commented 4 months ago

This is a duplicate of #609 which was fixed recently. See commit 377b5ff

This problem seems to continue to persist even in the recent release. It doesn't expect the rest-client builder to be nullable and as such operates on the assumption that both configurations and builders are available.

markpollack commented 4 months ago

Thanks for the feedback @Adakole2020 Will investigate.