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

@@ -57,13 +57,13 @@ public class WatsonxAiChatModel implements ChatModel, StreamingChatModel {
public WatsonxAiChatModel(WatsonxAiApi watsonxAiApi) {
this(watsonxAiApi,
WatsonxAiChatOptions.builder()
.withTemperature(0.7f)
.withTopP(1.0f)
.withTemperature(0.7)
.withTopP(1.0)
.withTopK(50)
.withDecodingMethod("greedy")
.withMaxNewTokens(20)
.withMinNewTokens(0)
.withRepetitionPenalty(1.0f)
.withRepetitionPenalty(1.0)
.withStopSequences(List.of())
.build());
}

View File

@@ -48,14 +48,14 @@ public class WatsonxAiChatOptions implements ChatOptions {
* The temperature of the model. Increasing the temperature will
* make the model answer more creatively. (Default: 0.7)
*/
@JsonProperty("temperature") private Float temperature;
@JsonProperty("temperature") private Double temperature;
/**
* Works together with top-k. A higher value (e.g., 0.95) will lead to
* more diverse text, while a lower value (e.g., 0.2) will generate more focused and
* conservative text. (Default: 1.0)
*/
@JsonProperty("top_p") private Float topP;
@JsonProperty("top_p") private Double topP;
/**
* Reduces the probability of generating nonsense. A higher value (e.g.
@@ -104,7 +104,7 @@ public class WatsonxAiChatOptions implements ChatOptions {
* (e.g., 1.8) will penalize repetitions more strongly, while a lower value (e.g.,
* 1.1) will be more lenient. (Default: 1.0)
*/
@JsonProperty("repetition_penalty") private Float repetitionPenalty;
@JsonProperty("repetition_penalty") private Double repetitionPenalty;
/**
* Produce repeatable results, set the same random seed value every time. (Default: randomly generated)
@@ -126,20 +126,20 @@ public class WatsonxAiChatOptions implements ChatOptions {
private ObjectMapper mapper = new ObjectMapper();
@Override
public Float getTemperature() {
public Double getTemperature() {
return temperature;
}
public void setTemperature(Float temperature) {
public void setTemperature(Double temperature) {
this.temperature = temperature;
}
@Override
public Float getTopP() {
public Double getTopP() {
return topP;
}
public void setTopP(Float topP) {
public void setTopP(Double topP) {
this.topP = topP;
}
@@ -198,20 +198,20 @@ public class WatsonxAiChatOptions implements ChatOptions {
@Override
@JsonIgnore
public Float getPresencePenalty() {
public Double getPresencePenalty() {
return getRepetitionPenalty();
}
@JsonIgnore
public void setPresencePenalty(Float presencePenalty) {
public void setPresencePenalty(Double presencePenalty) {
setRepetitionPenalty(presencePenalty);
}
public Float getRepetitionPenalty() {
public Double getRepetitionPenalty() {
return repetitionPenalty;
}
public void setRepetitionPenalty(Float repetitionPenalty) {
public void setRepetitionPenalty(Double repetitionPenalty) {
this.repetitionPenalty = repetitionPenalty;
}
@@ -248,7 +248,7 @@ public class WatsonxAiChatOptions implements ChatOptions {
@Override
@JsonIgnore
public Float getFrequencyPenalty() {
public Double getFrequencyPenalty() {
return null;
}
@@ -260,12 +260,12 @@ public class WatsonxAiChatOptions implements ChatOptions {
WatsonxAiChatOptions options = new WatsonxAiChatOptions();
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;
}
@@ -295,7 +295,7 @@ public class WatsonxAiChatOptions implements ChatOptions {
return this;
}
public Builder withRepetitionPenalty(Float repetitionPenalty) {
public Builder withRepetitionPenalty(Double repetitionPenalty) {
this.options.repetitionPenalty = repetitionPenalty;
return this;
}

View File

@@ -52,7 +52,7 @@ public class WatsonxAiChatModelTest {
@Test
public void testCreateRequestWithNoModelId() {
var options = ChatOptionsBuilder.builder().withTemperature(0.9f).withTopK(100).withTopP(0.6f).build();
var options = ChatOptionsBuilder.builder().withTemperature(0.9).withTopK(100).withTopP(0.6).build();
Prompt prompt = new Prompt("Test message", options);
@@ -93,12 +93,12 @@ public class WatsonxAiChatModelTest {
WatsonxAiChatOptions modelOptions = WatsonxAiChatOptions.builder()
.withModel("meta-llama/llama-2-70b-chat")
.withDecodingMethod("sample")
.withTemperature(0.1f)
.withTopP(0.2f)
.withTemperature(0.1)
.withTopP(0.2)
.withTopK(10)
.withMaxNewTokens(30)
.withMinNewTokens(10)
.withRepetitionPenalty(1.4f)
.withRepetitionPenalty(1.4)
.withStopSequences(List.of("\n\n\n"))
.withRandomSeed(4)
.build();
@@ -127,12 +127,12 @@ public class WatsonxAiChatModelTest {
WatsonxAiChatOptions modelOptions = WatsonxAiChatOptions.builder()
.withModel("meta-llama/llama-2-70b-chat")
.withDecodingMethod("sample")
.withTemperature(0.1f)
.withTopP(0.2f)
.withTemperature(0.1)
.withTopP(0.2)
.withTopK(10)
.withMaxNewTokens(30)
.withMinNewTokens(10)
.withRepetitionPenalty(1.4f)
.withRepetitionPenalty(1.4)
.withStopSequences(List.of("\n\n\n"))
.withRandomSeed(4)
.build();

View File

@@ -34,13 +34,13 @@ public class WatsonxAiChatOptionTest {
public void testOptions() {
WatsonxAiChatOptions options = WatsonxAiChatOptions.builder()
.withDecodingMethod("sample")
.withTemperature(1.2f)
.withTemperature(1.2)
.withTopK(20)
.withTopP(0.5f)
.withTopP(0.5)
.withMaxNewTokens(100)
.withMinNewTokens(20)
.withStopSequences(List.of("\n\n\n"))
.withRepetitionPenalty(1.1f)
.withRepetitionPenalty(1.1)
.withRandomSeed(4)
.build();
@@ -61,13 +61,13 @@ public class WatsonxAiChatOptionTest {
public void testOptionsWithAdditionalParamsOneByOne() {
WatsonxAiChatOptions options = WatsonxAiChatOptions.builder()
.withDecodingMethod("sample")
.withTemperature(1.2f)
.withTemperature(1.2)
.withTopK(20)
.withTopP(0.5f)
.withTopP(0.5)
.withMaxNewTokens(100)
.withMinNewTokens(20)
.withStopSequences(List.of("\n\n\n"))
.withRepetitionPenalty(1.1f)
.withRepetitionPenalty(1.1)
.withRandomSeed(4)
.withAdditionalProperty("HAP", true)
.withAdditionalProperty("typicalP", 0.5f)
@@ -92,13 +92,13 @@ public class WatsonxAiChatOptionTest {
public void testOptionsWithAdditionalParamsMap() {
WatsonxAiChatOptions options = WatsonxAiChatOptions.builder()
.withDecodingMethod("sample")
.withTemperature(1.2f)
.withTemperature(1.2)
.withTopK(20)
.withTopP(0.5f)
.withTopP(0.5)
.withMaxNewTokens(100)
.withMinNewTokens(20)
.withStopSequences(List.of("\n\n\n"))
.withRepetitionPenalty(1.1f)
.withRepetitionPenalty(1.1)
.withRandomSeed(4)
.withAdditionalProperties(Map.of("HAP", true, "typicalP", 0.5f, "test_value", "test"))
.build();