Skip to content

Commit fefc6f4

Browse files
Hyune-ctzolov
authored andcommitted
fix: AzureOpenAiChatOptions to handle null values for presencePenalty and frequencyPenalty
1 parent 6753e24 commit fefc6f4

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

models/spring-ai-azure-openai/src/main/java/org/springframework/ai/azure/openai/AzureOpenAiChatOptions.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ public Builder withDeploymentName(String deploymentName) {
174174
}
175175

176176
public Builder withFrequencyPenalty(Float frequencyPenalty) {
177-
this.options.frequencyPenalty = frequencyPenalty.doubleValue();
177+
if(frequencyPenalty != null) {
178+
this.options.frequencyPenalty = frequencyPenalty.doubleValue();
179+
}
178180
return this;
179181
}
180182

@@ -194,7 +196,9 @@ public Builder withN(Integer n) {
194196
}
195197

196198
public Builder withPresencePenalty(Float presencePenalty) {
197-
this.options.presencePenalty = presencePenalty.doubleValue();
199+
if(presencePenalty != null) {
200+
this.options.presencePenalty = presencePenalty.doubleValue();
201+
}
198202
return this;
199203
}
200204

models/spring-ai-azure-openai/src/test/java/org/springframework/ai/azure/openai/AzureChatCompletionsOptionsTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717

1818
import com.azure.ai.openai.OpenAIClient;
1919
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.Arguments;
22+
import org.junit.jupiter.params.provider.MethodSource;
2023
import org.mockito.Mockito;
2124

2225
import org.springframework.ai.chat.prompt.Prompt;
2326

27+
import java.util.stream.Stream;
28+
2429
import static org.assertj.core.api.Assertions.assertThat;
2530

2631
/**
@@ -51,4 +56,39 @@ public void createRequestWithChatOptions() {
5156
assertThat(requestOptions.getTemperature()).isEqualTo(99.9f);
5257
}
5358

59+
private static Stream<Arguments> providePresencePenaltyAndFrequencyPenaltyTest() {
60+
return Stream.of(
61+
Arguments.of(0.0f, 0.0f),
62+
Arguments.of(0.0f, 1.0f),
63+
Arguments.of(1.0f, 0.0f),
64+
Arguments.of(1.0f, 1.0f),
65+
Arguments.of(1.0f, null),
66+
Arguments.of(null, 1.0f),
67+
Arguments.of(null, null)
68+
);
69+
}
70+
71+
@ParameterizedTest
72+
@MethodSource("providePresencePenaltyAndFrequencyPenaltyTest")
73+
public void createChatOptionsWithPresencePenaltyAndFrequencyPenalty(Float presencePenalty, Float frequencyPenalty) {
74+
var options = AzureOpenAiChatOptions.builder()
75+
.withMaxTokens(800)
76+
.withTemperature(0.7F)
77+
.withTopP(0.95F)
78+
.withPresencePenalty(presencePenalty)
79+
.withFrequencyPenalty(frequencyPenalty)
80+
.build();
81+
82+
if (presencePenalty == null) {
83+
assertThat(options.getPresencePenalty()).isEqualTo(null);
84+
} else {
85+
assertThat(options.getPresencePenalty().floatValue()).isEqualTo(presencePenalty);
86+
}
87+
88+
if (frequencyPenalty == null) {
89+
assertThat(options.getFrequencyPenalty()).isEqualTo(null);
90+
} else {
91+
assertThat(options.getFrequencyPenalty().floatValue()).isEqualTo(frequencyPenalty);
92+
}
93+
}
5494
}

0 commit comments

Comments
 (0)