spring-projects / spring-ai

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

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

Open jocax opened 1 month ago

jocax commented 1 month 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 1 month 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