|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 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.azure.openai; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import com.azure.ai.openai.models.AzureChatEnhancementConfiguration; |
| 23 | +import com.azure.ai.openai.models.AzureChatGroundingEnhancementConfiguration; |
| 24 | +import com.azure.ai.openai.models.AzureChatOCREnhancementConfiguration; |
| 25 | +import com.azure.ai.openai.models.ChatCompletionStreamOptions; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link AzureOpenAiChatOptions}. |
| 32 | + * |
| 33 | + * @author Alexandros Pappas |
| 34 | + */ |
| 35 | +class AzureOpenAiChatOptionsTests { |
| 36 | + |
| 37 | + @Test |
| 38 | + void testBuilderWithAllFields() { |
| 39 | + AzureOpenAiResponseFormat responseFormat = AzureOpenAiResponseFormat.TEXT; |
| 40 | + ChatCompletionStreamOptions streamOptions = new ChatCompletionStreamOptions(); |
| 41 | + streamOptions.setIncludeUsage(true); |
| 42 | + |
| 43 | + AzureChatEnhancementConfiguration enhancements = new AzureChatEnhancementConfiguration(); |
| 44 | + enhancements.setOcr(new AzureChatOCREnhancementConfiguration(true)); |
| 45 | + enhancements.setGrounding(new AzureChatGroundingEnhancementConfiguration(true)); |
| 46 | + |
| 47 | + AzureOpenAiChatOptions options = AzureOpenAiChatOptions.builder() |
| 48 | + .deploymentName("test-deployment") |
| 49 | + .frequencyPenalty(0.5) |
| 50 | + .logitBias(Map.of("token1", 1, "token2", -1)) |
| 51 | + .maxTokens(200) |
| 52 | + .N(2) |
| 53 | + .presencePenalty(0.8) |
| 54 | + .stop(List.of("stop1", "stop2")) |
| 55 | + .temperature(0.7) |
| 56 | + .topP(0.9) |
| 57 | + .user("test-user") |
| 58 | + .responseFormat(responseFormat) |
| 59 | + .seed(12345L) |
| 60 | + .logprobs(true) |
| 61 | + .topLogprobs(5) |
| 62 | + .enhancements(enhancements) |
| 63 | + .streamOptions(streamOptions) |
| 64 | + .build(); |
| 65 | + |
| 66 | + assertThat(options) |
| 67 | + .extracting("deploymentName", "frequencyPenalty", "logitBias", "maxTokens", "n", "presencePenalty", "stop", |
| 68 | + "temperature", "topP", "user", "responseFormat", "seed", "logprobs", "topLogProbs", "enhancements", |
| 69 | + "streamOptions") |
| 70 | + .containsExactly("test-deployment", 0.5, Map.of("token1", 1, "token2", -1), 200, 2, 0.8, |
| 71 | + List.of("stop1", "stop2"), 0.7, 0.9, "test-user", responseFormat, 12345L, true, 5, enhancements, |
| 72 | + streamOptions); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testCopy() { |
| 77 | + AzureOpenAiResponseFormat responseFormat = AzureOpenAiResponseFormat.TEXT; |
| 78 | + ChatCompletionStreamOptions streamOptions = new ChatCompletionStreamOptions(); |
| 79 | + streamOptions.setIncludeUsage(true); |
| 80 | + |
| 81 | + AzureChatEnhancementConfiguration enhancements = new AzureChatEnhancementConfiguration(); |
| 82 | + enhancements.setOcr(new AzureChatOCREnhancementConfiguration(true)); |
| 83 | + enhancements.setGrounding(new AzureChatGroundingEnhancementConfiguration(true)); |
| 84 | + |
| 85 | + AzureOpenAiChatOptions originalOptions = AzureOpenAiChatOptions.builder() |
| 86 | + .deploymentName("test-deployment") |
| 87 | + .frequencyPenalty(0.5) |
| 88 | + .logitBias(Map.of("token1", 1, "token2", -1)) |
| 89 | + .maxTokens(200) |
| 90 | + .N(2) |
| 91 | + .presencePenalty(0.8) |
| 92 | + .stop(List.of("stop1", "stop2")) |
| 93 | + .temperature(0.7) |
| 94 | + .topP(0.9) |
| 95 | + .user("test-user") |
| 96 | + .responseFormat(responseFormat) |
| 97 | + .seed(12345L) |
| 98 | + .logprobs(true) |
| 99 | + .topLogprobs(5) |
| 100 | + .enhancements(enhancements) |
| 101 | + .streamOptions(streamOptions) |
| 102 | + .build(); |
| 103 | + |
| 104 | + AzureOpenAiChatOptions copiedOptions = originalOptions.copy(); |
| 105 | + |
| 106 | + assertThat(copiedOptions).isNotSameAs(originalOptions).isEqualTo(originalOptions); |
| 107 | + // Ensure deep copy |
| 108 | + assertThat(copiedOptions.getStop()).isNotSameAs(originalOptions.getStop()); |
| 109 | + assertThat(copiedOptions.getToolContext()).isNotSameAs(originalOptions.getToolContext()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testSetters() { |
| 114 | + AzureOpenAiResponseFormat responseFormat = AzureOpenAiResponseFormat.TEXT; |
| 115 | + ChatCompletionStreamOptions streamOptions = new ChatCompletionStreamOptions(); |
| 116 | + streamOptions.setIncludeUsage(true); |
| 117 | + AzureChatEnhancementConfiguration enhancements = new AzureChatEnhancementConfiguration(); |
| 118 | + |
| 119 | + AzureOpenAiChatOptions options = new AzureOpenAiChatOptions(); |
| 120 | + options.setDeploymentName("test-deployment"); |
| 121 | + options.setFrequencyPenalty(0.5); |
| 122 | + options.setLogitBias(Map.of("token1", 1, "token2", -1)); |
| 123 | + options.setMaxTokens(200); |
| 124 | + options.setN(2); |
| 125 | + options.setPresencePenalty(0.8); |
| 126 | + options.setStop(List.of("stop1", "stop2")); |
| 127 | + options.setTemperature(0.7); |
| 128 | + options.setTopP(0.9); |
| 129 | + options.setUser("test-user"); |
| 130 | + options.setResponseFormat(responseFormat); |
| 131 | + options.setSeed(12345L); |
| 132 | + options.setLogprobs(true); |
| 133 | + options.setTopLogProbs(5); |
| 134 | + options.setEnhancements(enhancements); |
| 135 | + options.setStreamOptions(streamOptions); |
| 136 | + |
| 137 | + assertThat(options.getDeploymentName()).isEqualTo("test-deployment"); |
| 138 | + options.setModel("test-model"); |
| 139 | + assertThat(options.getDeploymentName()).isEqualTo("test-model"); |
| 140 | + |
| 141 | + assertThat(options.getFrequencyPenalty()).isEqualTo(0.5); |
| 142 | + assertThat(options.getLogitBias()).isEqualTo(Map.of("token1", 1, "token2", -1)); |
| 143 | + assertThat(options.getMaxTokens()).isEqualTo(200); |
| 144 | + assertThat(options.getN()).isEqualTo(2); |
| 145 | + assertThat(options.getPresencePenalty()).isEqualTo(0.8); |
| 146 | + assertThat(options.getStop()).isEqualTo(List.of("stop1", "stop2")); |
| 147 | + assertThat(options.getTemperature()).isEqualTo(0.7); |
| 148 | + assertThat(options.getTopP()).isEqualTo(0.9); |
| 149 | + assertThat(options.getUser()).isEqualTo("test-user"); |
| 150 | + assertThat(options.getResponseFormat()).isEqualTo(responseFormat); |
| 151 | + assertThat(options.getSeed()).isEqualTo(12345L); |
| 152 | + assertThat(options.isLogprobs()).isTrue(); |
| 153 | + assertThat(options.getTopLogProbs()).isEqualTo(5); |
| 154 | + assertThat(options.getEnhancements()).isEqualTo(enhancements); |
| 155 | + assertThat(options.getStreamOptions()).isEqualTo(streamOptions); |
| 156 | + assertThat(options.getModel()).isEqualTo("test-model"); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testDefaultValues() { |
| 161 | + AzureOpenAiChatOptions options = new AzureOpenAiChatOptions(); |
| 162 | + |
| 163 | + assertThat(options.getDeploymentName()).isNull(); |
| 164 | + assertThat(options.getFrequencyPenalty()).isNull(); |
| 165 | + assertThat(options.getLogitBias()).isNull(); |
| 166 | + assertThat(options.getMaxTokens()).isNull(); |
| 167 | + assertThat(options.getN()).isNull(); |
| 168 | + assertThat(options.getPresencePenalty()).isNull(); |
| 169 | + assertThat(options.getStop()).isNull(); |
| 170 | + assertThat(options.getTemperature()).isNull(); |
| 171 | + assertThat(options.getTopP()).isNull(); |
| 172 | + assertThat(options.getUser()).isNull(); |
| 173 | + assertThat(options.getResponseFormat()).isNull(); |
| 174 | + assertThat(options.getSeed()).isNull(); |
| 175 | + assertThat(options.isLogprobs()).isNull(); |
| 176 | + assertThat(options.getTopLogProbs()).isNull(); |
| 177 | + assertThat(options.getEnhancements()).isNull(); |
| 178 | + assertThat(options.getStreamOptions()).isNull(); |
| 179 | + assertThat(options.getModel()).isNull(); |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments