springspring-ai

How to change the restclient implementation for springai?


how can we change the RestClient Implementation for spring-ai? We want to use the JdkClientHttpRequestFactory for testing or another implementation.

We tried to create a Bean for the restclient but this does not work.

thanks Frank


Solution

  • here is our solution that worked:

    @RequiredArgsConstructor
    @Configuration
    public class AiConfig {
    
        @Value("${spring.ai.openai.api-key}")
        private final String openAiApiKey;
    
        @Bean
        OpenAiApi openAiApi() {
            final RestClient.Builder builder = RestClient.builder()
                    .requestFactory(new JdkClientHttpRequestFactory());
            return new OpenAiApi(ApiUtils.DEFAULT_BASE_URL, openAiApiKey, builder);
        }
    
        @Bean
        ChatClient chatClient(OpenAiApi openAiApi) {
            return new OpenAiChatClient(openAiApi);
        }
    
        @Bean
        EmbeddingClient embeddingClient(OpenAiApi openAiApi) {
            return new OpenAiEmbeddingClient(openAiApi);
        }