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:
Thomas Vitale
2024-09-08 23:24:48 +02:00
committed by Mark Pollack
parent 40714c984d
commit 4b123a7516
146 changed files with 731 additions and 717 deletions

View File

@@ -99,7 +99,7 @@ public class MiniMaxChatModel extends AbstractToolCallSupport implements ChatMod
*/
public MiniMaxChatModel(MiniMaxApi miniMaxApi) {
this(miniMaxApi,
MiniMaxChatOptions.builder().withModel(MiniMaxApi.DEFAULT_CHAT_MODEL).withTemperature(0.7f).build());
MiniMaxChatOptions.builder().withModel(MiniMaxApi.DEFAULT_CHAT_MODEL).withTemperature(0.7).build());
}
/**

View File

@@ -54,7 +54,7 @@ public class MiniMaxChatOptions implements FunctionCallingOptions, ChatOptions {
* 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;
/**
* The maximum number of tokens to generate in the chat completion. The total length of input
* tokens and generated tokens is limited by the model's context length.
@@ -69,7 +69,7 @@ public class MiniMaxChatOptions implements FunctionCallingOptions, ChatOptions {
* Number between -2.0 and 2.0. Positive values penalize new tokens based on whether 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;
/**
* An object specifying the format that the model must output. Setting to { "type":
* "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
@@ -92,13 +92,13 @@ public class MiniMaxChatOptions implements FunctionCallingOptions, ChatOptions {
* 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 model considers the
* results of the tokens with top_p probability mass. So 0.1 means 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;
/**
* Mask the text information in the output that is easy to involve privacy issues,
* including but not limited to email, domain name, link, ID number, home address, etc.
@@ -165,7 +165,7 @@ public class MiniMaxChatOptions implements FunctionCallingOptions, ChatOptions {
return this;
}
public Builder withFrequencyPenalty(Float frequencyPenalty) {
public Builder withFrequencyPenalty(Double frequencyPenalty) {
this.options.frequencyPenalty = frequencyPenalty;
return this;
}
@@ -180,7 +180,7 @@ public class MiniMaxChatOptions implements FunctionCallingOptions, ChatOptions {
return this;
}
public Builder withPresencePenalty(Float presencePenalty) {
public Builder withPresencePenalty(Double presencePenalty) {
this.options.presencePenalty = presencePenalty;
return this;
}
@@ -200,12 +200,12 @@ public class MiniMaxChatOptions 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;
}
@@ -258,11 +258,11 @@ public class MiniMaxChatOptions 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;
}
@@ -284,11 +284,11 @@ public class MiniMaxChatOptions 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;
}
@@ -328,20 +328,20 @@ public class MiniMaxChatOptions 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;
}

View File

@@ -45,6 +45,7 @@ import java.util.function.Predicate;
* <a href="https://www.minimaxi.com/document/guides/Embeddings">MiniMax Embedding API</a>.
*
* @author Geng Rong
* @author Thomas Vitale
* @since 1.0.0 M1
*/
public class MiniMaxApi {
@@ -247,16 +248,16 @@ public class MiniMaxApi {
public record ChatCompletionRequest (
@JsonProperty("messages") List<ChatCompletionMessage> messages,
@JsonProperty("model") String model,
@JsonProperty("frequency_penalty") Float frequencyPenalty,
@JsonProperty("frequency_penalty") Double frequencyPenalty,
@JsonProperty("max_tokens") Integer maxTokens,
@JsonProperty("n") Integer n,
@JsonProperty("presence_penalty") Float presencePenalty,
@JsonProperty("presence_penalty") Double presencePenalty,
@JsonProperty("response_format") ResponseFormat responseFormat,
@JsonProperty("seed") Integer seed,
@JsonProperty("stop") List<String> stop,
@JsonProperty("stream") Boolean stream,
@JsonProperty("temperature") Float temperature,
@JsonProperty("top_p") Float topP,
@JsonProperty("temperature") Double temperature,
@JsonProperty("top_p") Double topP,
@JsonProperty("mask_sensitive_info") Boolean maskSensitiveInfo,
@JsonProperty("tools") List<FunctionTool> tools,
@JsonProperty("tool_choice") Object toolChoice) {
@@ -268,7 +269,7 @@ public class MiniMaxApi {
* @param model ID of the model to use.
* @param temperature What sampling temperature to use, between 0 and 1.
*/
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Float temperature) {
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Double temperature) {
this(messages, model, null, null, null, null,
null, null, null, false, temperature, null,null,
null, null);
@@ -283,7 +284,7 @@ public class MiniMaxApi {
* @param stream If set, partial message deltas will be sent.Tokens will be sent as data-only server-sent events
* as they become available, with the stream terminated by a data: [DONE] message.
*/
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Float temperature, boolean stream) {
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Double temperature, boolean stream) {
this(messages, model, null, null, null, null,
null, null, null, stream, temperature, null,null,
null, null);
@@ -301,7 +302,7 @@ public class MiniMaxApi {
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
List<FunctionTool> tools, Object toolChoice) {
this(messages, model, null, null, null, null,
null, null, null, false, 0.8f, null,null,
null, null, null, false, 0.8, null,null,
tools, toolChoice);
}

View File

@@ -34,7 +34,7 @@ public class ChatCompletionRequestTests {
public void createRequestWithChatOptions() {
var client = new MiniMaxChatModel(new MiniMaxApi("TEST"),
MiniMaxChatOptions.builder().withModel("DEFAULT_MODEL").withTemperature(66.6f).build());
MiniMaxChatOptions.builder().withModel("DEFAULT_MODEL").withTemperature(66.6).build());
var request = client.createRequest(new Prompt("Test message content"), false);
@@ -42,16 +42,16 @@ public class ChatCompletionRequestTests {
assertThat(request.stream()).isFalse();
assertThat(request.model()).isEqualTo("DEFAULT_MODEL");
assertThat(request.temperature()).isEqualTo(66.6f);
assertThat(request.temperature()).isEqualTo(66.6);
request = client.createRequest(new Prompt("Test message content",
MiniMaxChatOptions.builder().withModel("PROMPT_MODEL").withTemperature(99.9f).build()), true);
MiniMaxChatOptions.builder().withModel("PROMPT_MODEL").withTemperature(99.9).build()), true);
assertThat(request.messages()).hasSize(1);
assertThat(request.stream()).isTrue();
assertThat(request.model()).isEqualTo("PROMPT_MODEL");
assertThat(request.temperature()).isEqualTo(99.9f);
assertThat(request.temperature()).isEqualTo(99.9);
}
@Test

View File

@@ -43,7 +43,7 @@ public class MiniMaxApiIT {
void chatCompletionEntity() {
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
ResponseEntity<ChatCompletion> response = miniMaxApi
.chatCompletionEntity(new ChatCompletionRequest(List.of(chatCompletionMessage), "glm-4-air", 0.7f, false));
.chatCompletionEntity(new ChatCompletionRequest(List.of(chatCompletionMessage), "glm-4-air", 0.7, false));
assertThat(response).isNotNull();
assertThat(response.getBody()).isNotNull();
@@ -53,7 +53,7 @@ public class MiniMaxApiIT {
void chatCompletionStream() {
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
Flux<ChatCompletionChunk> response = miniMaxApi
.chatCompletionStream(new ChatCompletionRequest(List.of(chatCompletionMessage), "glm-4-air", 0.7f, true));
.chatCompletionStream(new ChatCompletionRequest(List.of(chatCompletionMessage), "glm-4-air", 0.7, true));
assertThat(response).isNotNull();
assertThat(response.collectList().block()).isNotNull();

View File

@@ -117,7 +117,7 @@ public class MiniMaxApiToolFunctionCallIT {
}
var functionResponseRequest = new ChatCompletionRequest(messages,
org.springframework.ai.minimax.api.MiniMaxApi.ChatModel.ABAB_6_5_Chat.getValue(), 0.5F);
org.springframework.ai.minimax.api.MiniMaxApi.ChatModel.ABAB_6_5_Chat.getValue(), 0.5);
ResponseEntity<ChatCompletion> chatCompletion2 = miniMaxApi.chatCompletionEntity(functionResponseRequest);