fix: AzureOpenAiChatOptions to handle null values for presencePenalty and frequencyPenalty

This commit is contained in:
Hyune-c
2024-04-01 21:53:52 +09:00
committed by Christian Tzolov
parent 6753e242e8
commit fefc6f4173
2 changed files with 46 additions and 2 deletions

View File

@@ -174,7 +174,9 @@ public class AzureOpenAiChatOptions implements FunctionCallingOptions, ChatOptio
}
public Builder withFrequencyPenalty(Float frequencyPenalty) {
this.options.frequencyPenalty = frequencyPenalty.doubleValue();
if(frequencyPenalty != null) {
this.options.frequencyPenalty = frequencyPenalty.doubleValue();
}
return this;
}
@@ -194,7 +196,9 @@ public class AzureOpenAiChatOptions implements FunctionCallingOptions, ChatOptio
}
public Builder withPresencePenalty(Float presencePenalty) {
this.options.presencePenalty = presencePenalty.doubleValue();
if(presencePenalty != null) {
this.options.presencePenalty = presencePenalty.doubleValue();
}
return this;
}

View File

@@ -17,10 +17,15 @@ package org.springframework.ai.azure.openai;
import com.azure.ai.openai.OpenAIClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;
import org.springframework.ai.chat.prompt.Prompt;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -51,4 +56,39 @@ public class AzureChatCompletionsOptionsTests {
assertThat(requestOptions.getTemperature()).isEqualTo(99.9f);
}
private static Stream<Arguments> providePresencePenaltyAndFrequencyPenaltyTest() {
return Stream.of(
Arguments.of(0.0f, 0.0f),
Arguments.of(0.0f, 1.0f),
Arguments.of(1.0f, 0.0f),
Arguments.of(1.0f, 1.0f),
Arguments.of(1.0f, null),
Arguments.of(null, 1.0f),
Arguments.of(null, null)
);
}
@ParameterizedTest
@MethodSource("providePresencePenaltyAndFrequencyPenaltyTest")
public void createChatOptionsWithPresencePenaltyAndFrequencyPenalty(Float presencePenalty, Float frequencyPenalty) {
var options = AzureOpenAiChatOptions.builder()
.withMaxTokens(800)
.withTemperature(0.7F)
.withTopP(0.95F)
.withPresencePenalty(presencePenalty)
.withFrequencyPenalty(frequencyPenalty)
.build();
if (presencePenalty == null) {
assertThat(options.getPresencePenalty()).isEqualTo(null);
} else {
assertThat(options.getPresencePenalty().floatValue()).isEqualTo(presencePenalty);
}
if (frequencyPenalty == null) {
assertThat(options.getFrequencyPenalty()).isEqualTo(null);
} else {
assertThat(options.getFrequencyPenalty().floatValue()).isEqualTo(frequencyPenalty);
}
}
}