|
| 1 | +package org.springframework.ai.model.openai.autoconfigure; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.ai.chat.prompt.ChatOptions; |
| 5 | +import org.springframework.ai.openai.OpenAiChatModel; |
| 6 | +import org.springframework.ai.openai.OpenAiChatOptions; |
| 7 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 8 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +class OpenAiChatAutoConfigurationTest { |
| 15 | + |
| 16 | + private final ApplicationContextRunner runner = new ApplicationContextRunner() |
| 17 | + .withConfiguration(AutoConfigurations.of(OpenAiChatAutoConfiguration.class)) |
| 18 | + .withPropertyValues("spring.ai.openai.apiKey=" + System.getenv("OPENAI_API_KEY"), |
| 19 | + "spring.ai.openai.chat.options.temperature=0.8"); |
| 20 | + |
| 21 | + @Test |
| 22 | + void whenUserDefinesTemperature_thenOverride() { |
| 23 | + runner.withUserConfiguration(UserOptionsConfig.class) |
| 24 | + .run(context -> { |
| 25 | + assertThat(context).hasSingleBean(OpenAiChatModel.class); |
| 26 | + ChatOptions opts = context.getBean(OpenAiChatModel.class).getDefaultOptions(); |
| 27 | + assertThat(opts.getTemperature()).isEqualTo(0.42); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void whenNoUserOptions_thenDefaultTemperatureRetained() { |
| 33 | + runner.run(context -> { |
| 34 | + assertThat(context).hasSingleBean(OpenAiChatModel.class); |
| 35 | + ChatOptions opts = context.getBean(OpenAiChatModel.class).getDefaultOptions(); |
| 36 | + assertThat(opts.getTemperature()).isEqualTo(0.8); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void whenSearchModel_thenTemperatureOmitted() { |
| 42 | + runner.withPropertyValues("spring.ai.openai.chat.options.model=text-search-abc") |
| 43 | + .run(context -> { |
| 44 | + assertThat(context).hasSingleBean(OpenAiChatModel.class); |
| 45 | + ChatOptions opts = context.getBean(OpenAiChatModel.class).getDefaultOptions(); |
| 46 | + assertThat(opts.getTemperature()).isNull(); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Configuration |
| 51 | + static class UserOptionsConfig { |
| 52 | + @Bean |
| 53 | + public OpenAiChatOptions userOptions() { |
| 54 | + OpenAiChatOptions openAiChatOptions = new OpenAiChatOptions(); |
| 55 | + openAiChatOptions.setTemperature(0.42); |
| 56 | + return openAiChatOptions; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments