Use Double instead of Float for portable ChatOptions
This change updates the type of portable chat options from Float to Double. Affected options include: - frequencyPenalty - presencePenalty - temperature - topP The motivation for this change is to simplify coding. In Java, Float values require an "f" suffix (e.g., 0.5f), while Double values don't need any suffix. This makes Double easier to type and reduces potential errors from forgetting the "f" suffix. APIs, tests, and documentation have been updated to reflect this change. Fixes gh-712 Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
This commit is contained in:
committed by
Mark Pollack
parent
40714c984d
commit
4b123a7516
@@ -53,7 +53,7 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
* make the output more random, while lower values like 0.2 will make it more focused
|
||||
* and deterministic. We generally recommend altering this or top_p but not both.
|
||||
*/
|
||||
private @JsonProperty("temperature") Float temperature;
|
||||
private @JsonProperty("temperature") Double temperature;
|
||||
|
||||
/**
|
||||
* An alternative to sampling with temperature, called nucleus sampling, where the
|
||||
@@ -61,7 +61,7 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
* only the tokens comprising the top 10% probability mass are considered. We
|
||||
* generally recommend altering this or temperature but not both.
|
||||
*/
|
||||
private @JsonProperty("top_p") Float topP;
|
||||
private @JsonProperty("top_p") Double topP;
|
||||
|
||||
/**
|
||||
* How many chat completion choices to generate for each input message. Note that you
|
||||
@@ -75,14 +75,14 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
* they appear in the text so far, increasing the model's likelihood to talk about new
|
||||
* topics.
|
||||
*/
|
||||
private @JsonProperty("presence_penalty") Float presencePenalty;
|
||||
private @JsonProperty("presence_penalty") Double presencePenalty;
|
||||
|
||||
/**
|
||||
* Number between -2.0 and 2.0. Positive values penalize new tokens based on their
|
||||
* existing frequency in the text so far, decreasing the model's likelihood to repeat
|
||||
* the same line verbatim.
|
||||
*/
|
||||
private @JsonProperty("frequency_penalty") Float frequencyPenalty;
|
||||
private @JsonProperty("frequency_penalty") Double frequencyPenalty;
|
||||
|
||||
/**
|
||||
* Up to 5 sequences where the API will stop generating further tokens.
|
||||
@@ -182,12 +182,12 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withTemperature(Float temperature) {
|
||||
public Builder withTemperature(Double temperature) {
|
||||
this.options.temperature = temperature;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withTopP(Float topP) {
|
||||
public Builder withTopP(Double topP) {
|
||||
this.options.topP = topP;
|
||||
return this;
|
||||
}
|
||||
@@ -197,12 +197,12 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withPresencePenalty(Float presencePenalty) {
|
||||
public Builder withPresencePenalty(Double presencePenalty) {
|
||||
this.options.presencePenalty = presencePenalty;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withFrequencyPenalty(Float frequencyPenalty) {
|
||||
public Builder withFrequencyPenalty(Double frequencyPenalty) {
|
||||
this.options.frequencyPenalty = frequencyPenalty;
|
||||
return this;
|
||||
}
|
||||
@@ -260,11 +260,11 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getFrequencyPenalty() {
|
||||
public Double getFrequencyPenalty() {
|
||||
return this.frequencyPenalty;
|
||||
}
|
||||
|
||||
public void setFrequencyPenalty(Float frequencyPenalty) {
|
||||
public void setFrequencyPenalty(Double frequencyPenalty) {
|
||||
this.frequencyPenalty = frequencyPenalty;
|
||||
}
|
||||
|
||||
@@ -286,11 +286,11 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getPresencePenalty() {
|
||||
public Double getPresencePenalty() {
|
||||
return this.presencePenalty;
|
||||
}
|
||||
|
||||
public void setPresencePenalty(Float presencePenalty) {
|
||||
public void setPresencePenalty(Double presencePenalty) {
|
||||
this.presencePenalty = presencePenalty;
|
||||
}
|
||||
|
||||
@@ -314,20 +314,20 @@ public class MoonshotChatOptions implements FunctionCallingOptions, ChatOptions
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getTemperature() {
|
||||
public Double getTemperature() {
|
||||
return this.temperature;
|
||||
}
|
||||
|
||||
public void setTemperature(Float temperature) {
|
||||
public void setTemperature(Double temperature) {
|
||||
this.temperature = temperature;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getTopP() {
|
||||
public Double getTopP() {
|
||||
return this.topP;
|
||||
}
|
||||
|
||||
public void setTopP(Float topP) {
|
||||
public void setTopP(Double topP) {
|
||||
this.topP = topP;
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ import static org.springframework.ai.moonshot.api.MoonshotConstants.DEFAULT_BASE
|
||||
* </p>
|
||||
*
|
||||
* @author Geng Rong
|
||||
* @author Thomas Vitale
|
||||
*/
|
||||
public class MoonshotApi {
|
||||
|
||||
@@ -155,11 +156,11 @@ public class MoonshotApi {
|
||||
@JsonProperty("messages") List<ChatCompletionMessage> messages,
|
||||
@JsonProperty("model") String model,
|
||||
@JsonProperty("max_tokens") Integer maxTokens,
|
||||
@JsonProperty("temperature") Float temperature,
|
||||
@JsonProperty("top_p") Float topP,
|
||||
@JsonProperty("temperature") Double temperature,
|
||||
@JsonProperty("top_p") Double topP,
|
||||
@JsonProperty("n") Integer n,
|
||||
@JsonProperty("frequency_penalty") Float frequencyPenalty,
|
||||
@JsonProperty("presence_penalty") Float presencePenalty,
|
||||
@JsonProperty("frequency_penalty") Double frequencyPenalty,
|
||||
@JsonProperty("presence_penalty") Double presencePenalty,
|
||||
@JsonProperty("stop") List<String> stop,
|
||||
@JsonProperty("stream") Boolean stream,
|
||||
@JsonProperty("tools") List<FunctionTool> tools,
|
||||
@@ -174,7 +175,7 @@ public class MoonshotApi {
|
||||
* @param model ID of the model to use.
|
||||
*/
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model) {
|
||||
this(messages, model, null, 0.3f, 1f, null, null, null, null, false, null, null);
|
||||
this(messages, model, null, 0.3, 1.0, null, null, null, null, false, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,9 +188,9 @@ public class MoonshotApi {
|
||||
* @param stream Whether to stream back partial progress. If set, tokens will be
|
||||
* sent
|
||||
*/
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Float temperature,
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Double temperature,
|
||||
boolean stream) {
|
||||
this(messages, model, null, temperature, 1f, null, null, null, null, stream, null, null);
|
||||
this(messages, model, null, temperature, 1.0, null, null, null, null, stream, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,8 +201,8 @@ public class MoonshotApi {
|
||||
* @param model ID of the model to use.
|
||||
* @param temperature What sampling temperature to use, between 0.0 and 1.0.
|
||||
*/
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Float temperature) {
|
||||
this(messages, model, null, temperature, 1f, null, null, null, null, false, null, null);
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Double temperature) {
|
||||
this(messages, model, null, temperature, 1.0, null, null, null, null, false, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -216,7 +217,7 @@ public class MoonshotApi {
|
||||
*/
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, List<FunctionTool> tools,
|
||||
Object toolChoice) {
|
||||
this(messages, model, null, null, 1f, null, null, null, null, false, tools, toolChoice);
|
||||
this(messages, model, null, null, 1.0, null, null, null, null, false, tools, toolChoice);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,7 +225,7 @@ public class MoonshotApi {
|
||||
* stream.
|
||||
*/
|
||||
public ChatCompletionRequest(List<ChatCompletionMessage> messages, Boolean stream) {
|
||||
this(messages, DEFAULT_CHAT_MODEL, null, 0.7f, 1F, null, null, null, null, stream, null, null);
|
||||
this(messages, DEFAULT_CHAT_MODEL, null, 0.7, 1.0, null, null, null, null, stream, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,12 +45,12 @@ public class MoonshotChatCompletionRequestTest {
|
||||
|
||||
@Test
|
||||
void chatCompletionRequestWithOptionsTest() {
|
||||
var options = MoonshotChatOptions.builder().withTemperature(0.5f).withTopP(0.8f).build();
|
||||
var options = MoonshotChatOptions.builder().withTemperature(0.5).withTopP(0.8).build();
|
||||
var request = chatModel.createRequest(new Prompt("test content", options), true);
|
||||
|
||||
assertThat(request.messages().size()).isEqualTo(1);
|
||||
assertThat(request.topP()).isEqualTo(0.8f);
|
||||
assertThat(request.temperature()).isEqualTo(0.5f);
|
||||
assertThat(request.topP()).isEqualTo(0.8);
|
||||
assertThat(request.temperature()).isEqualTo(0.5);
|
||||
assertThat(request.stream()).isTrue();
|
||||
}
|
||||
|
||||
|
||||
@@ -85,8 +85,8 @@ public class MoonshotRetryTests {
|
||||
|
||||
chatModel = new MoonshotChatModel(moonshotApi,
|
||||
MoonshotChatOptions.builder()
|
||||
.withTemperature(0.7f)
|
||||
.withTopP(1f)
|
||||
.withTemperature(0.7)
|
||||
.withTopP(1.0)
|
||||
.withModel(MoonshotApi.ChatModel.MOONSHOT_V1_32K.getValue())
|
||||
.build(),
|
||||
null, retryTemplate);
|
||||
|
||||
@@ -41,7 +41,7 @@ public class MoonshotApiIT {
|
||||
void chatCompletionEntity() {
|
||||
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
|
||||
ResponseEntity<ChatCompletion> response = moonshotApi.chatCompletionEntity(new ChatCompletionRequest(
|
||||
List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8f, false));
|
||||
List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8, false));
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
@@ -58,7 +58,7 @@ public class MoonshotApiIT {
|
||||
""", Role.SYSTEM);
|
||||
|
||||
ResponseEntity<ChatCompletion> response = moonshotApi.chatCompletionEntity(new ChatCompletionRequest(
|
||||
List.of(systemMessage, userMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8f, false));
|
||||
List.of(systemMessage, userMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8, false));
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
@@ -68,7 +68,7 @@ public class MoonshotApiIT {
|
||||
void chatCompletionStream() {
|
||||
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
|
||||
Flux<ChatCompletionChunk> response = moonshotApi.chatCompletionStream(new ChatCompletionRequest(
|
||||
List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8f, true));
|
||||
List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8, true));
|
||||
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.collectList().block()).isNotNull();
|
||||
|
||||
@@ -125,7 +125,7 @@ public class MoonshotApiToolFunctionCallIT {
|
||||
}
|
||||
|
||||
var functionResponseRequest = new ChatCompletionRequest(messages,
|
||||
MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.5F);
|
||||
MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.5);
|
||||
|
||||
ResponseEntity<ChatCompletion> chatCompletion2 = moonshotApi.chatCompletionEntity(functionResponseRequest);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user