|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.model.chat.client.autoconfigure; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.ArgumentCaptor; |
| 24 | + |
| 25 | +import org.springframework.ai.chat.client.ChatClient; |
| 26 | +import org.springframework.ai.chat.client.ChatClientCustomizer; |
| 27 | +import org.springframework.ai.chat.messages.AssistantMessage; |
| 28 | +import org.springframework.ai.chat.messages.Message; |
| 29 | +import org.springframework.ai.chat.messages.MessageType; |
| 30 | +import org.springframework.ai.chat.model.ChatModel; |
| 31 | +import org.springframework.ai.chat.model.ChatResponse; |
| 32 | +import org.springframework.ai.chat.model.Generation; |
| 33 | +import org.springframework.ai.chat.prompt.Prompt; |
| 34 | +import org.springframework.ai.content.Content; |
| 35 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 36 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 37 | +import org.springframework.boot.test.system.OutputCaptureExtension; |
| 38 | +import org.springframework.context.annotation.Bean; |
| 39 | +import org.springframework.context.annotation.Configuration; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | +import static org.assertj.core.api.Assertions.tuple; |
| 43 | +import static org.mockito.ArgumentMatchers.any; |
| 44 | +import static org.mockito.Mockito.mock; |
| 45 | +import static org.mockito.Mockito.verify; |
| 46 | +import static org.mockito.Mockito.when; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Filip Hrisafov |
| 50 | + */ |
| 51 | +@ExtendWith(OutputCaptureExtension.class) |
| 52 | +class ChatClientAutoConfigurationTests { |
| 53 | + |
| 54 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 55 | + .withConfiguration(AutoConfigurations.of(ChatClientAutoConfiguration.class)) |
| 56 | + .withUserConfiguration(MockConfig.class); |
| 57 | + |
| 58 | + @Test |
| 59 | + void implicitlyEnabled() { |
| 60 | + this.contextRunner.run(context -> assertThat(context.getBeansOfType(ChatClient.Builder.class)).isNotEmpty()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void explicitlyEnabled() { |
| 65 | + this.contextRunner.withPropertyValues("spring.ai.chat.client.enabled=true") |
| 66 | + .run(context -> assertThat(context.getBeansOfType(ChatClient.Builder.class)).isNotEmpty()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void explicitlyDisabled() { |
| 71 | + this.contextRunner.withPropertyValues("spring.ai.chat.client.enabled=false") |
| 72 | + .run(context -> assertThat(context.getBeansOfType(ChatClient.Builder.class)).isEmpty()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void generate() { |
| 77 | + this.contextRunner.run(context -> { |
| 78 | + ChatClient.Builder builder = context.getBean(ChatClient.Builder.class); |
| 79 | + |
| 80 | + assertThat(builder).isNotNull(); |
| 81 | + |
| 82 | + ChatClient chatClient = builder.build(); |
| 83 | + ChatModel chatModel = context.getBean(ChatModel.class); |
| 84 | + |
| 85 | + ChatResponse response = ChatResponse.builder() |
| 86 | + .generations(List.of(new Generation(new AssistantMessage("Test")))) |
| 87 | + .build(); |
| 88 | + when(chatModel.call(any(Prompt.class))).thenReturn(response); |
| 89 | + |
| 90 | + ChatResponse chatResponse = chatClient.prompt().user("Hello").call().chatResponse(); |
| 91 | + assertThat(chatResponse).isSameAs(response); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testChatClientCustomizers() { |
| 97 | + this.contextRunner.withUserConfiguration(Config.class).run(context -> { |
| 98 | + |
| 99 | + ChatClient.Builder builder = context.getBean(ChatClient.Builder.class); |
| 100 | + |
| 101 | + ChatClient chatClient = builder.build(); |
| 102 | + |
| 103 | + assertThat(chatClient).isNotNull(); |
| 104 | + |
| 105 | + ChatModel chatModel = context.getBean(ChatModel.class); |
| 106 | + |
| 107 | + ChatResponse response = ChatResponse.builder() |
| 108 | + .generations(List.of(new Generation(new AssistantMessage("Test")))) |
| 109 | + .build(); |
| 110 | + when(chatModel.call(any(Prompt.class))).thenReturn(response); |
| 111 | + chatClient.prompt().user(u -> u.param("actor", "Tom Hanks")).call().chatResponse(); |
| 112 | + |
| 113 | + ArgumentCaptor<Prompt> promptArgument = ArgumentCaptor.forClass(Prompt.class); |
| 114 | + |
| 115 | + verify(chatModel).call(promptArgument.capture()); |
| 116 | + |
| 117 | + Prompt prompt = promptArgument.getValue(); |
| 118 | + assertThat(prompt.getInstructions()).extracting(Message::getMessageType, Content::getText) |
| 119 | + .containsExactly(tuple(MessageType.SYSTEM, "You are a movie expert."), |
| 120 | + tuple(MessageType.USER, "Generate the filmography of 5 movies for Tom Hanks.")); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void withMultipleChatModels() { |
| 126 | + this.contextRunner.withUserConfiguration(SecondChatModelConfig.class).run(context -> { |
| 127 | + assertThat(context).hasNotFailed(); |
| 128 | + assertThat(context.getBeansOfType(ChatClient.Builder.class)).isEmpty(); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + record ActorsFilms(String actor, List<String> movies) { |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + @Configuration |
| 137 | + static class MockConfig { |
| 138 | + |
| 139 | + @Bean |
| 140 | + ChatModel chatModel() { |
| 141 | + return mock(ChatModel.class); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + @Configuration |
| 147 | + static class SecondChatModelConfig { |
| 148 | + |
| 149 | + @Bean |
| 150 | + ChatModel secondChatModel() { |
| 151 | + return mock(ChatModel.class); |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + @Configuration |
| 157 | + static class Config { |
| 158 | + |
| 159 | + @Bean |
| 160 | + public ChatClientCustomizer chatClientCustomizer() { |
| 161 | + return b -> b.defaultSystem("You are a movie expert.") |
| 162 | + .defaultUser("Generate the filmography of 5 movies for {actor}."); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments