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

@@ -82,7 +82,7 @@ public class AnthropicChatModel extends AbstractToolCallSupport implements ChatM
public static final Integer DEFAULT_MAX_TOKENS = 500;
public static final Float DEFAULT_TEMPERATURE = 0.8f;
public static final Double DEFAULT_TEMPERATURE = 0.8;
/**
* The lower-level API for the Anthropic service.

View File

@@ -48,8 +48,8 @@ public class AnthropicChatOptions implements ChatOptions, FunctionCallingOptions
private @JsonProperty("max_tokens") Integer maxTokens;
private @JsonProperty("metadata") ChatCompletionRequest.Metadata metadata;
private @JsonProperty("stop_sequences") List<String> stopSequences;
private @JsonProperty("temperature") Float temperature;
private @JsonProperty("top_p") Float topP;
private @JsonProperty("temperature") Double temperature;
private @JsonProperty("top_p") Double topP;
private @JsonProperty("top_k") Integer topK;
/**
@@ -112,12 +112,12 @@ public class AnthropicChatOptions implements ChatOptions, FunctionCallingOptions
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;
}
@@ -186,20 +186,20 @@ public class AnthropicChatOptions implements ChatOptions, FunctionCallingOptions
}
@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;
}
@@ -236,13 +236,13 @@ public class AnthropicChatOptions implements ChatOptions, FunctionCallingOptions
@Override
@JsonIgnore
public Float getFrequencyPenalty() {
public Double getFrequencyPenalty() {
return null;
}
@Override
@JsonIgnore
public Float getPresencePenalty() {
public Double getPresencePenalty() {
return null;
}

View File

@@ -241,19 +241,19 @@ public class AnthropicApi {
@JsonProperty("metadata") Metadata metadata,
@JsonProperty("stop_sequences") List<String> stopSequences,
@JsonProperty("stream") Boolean stream,
@JsonProperty("temperature") Float temperature,
@JsonProperty("top_p") Float topP,
@JsonProperty("temperature") Double temperature,
@JsonProperty("top_p") Double topP,
@JsonProperty("top_k") Integer topK,
@JsonProperty("tools") List<Tool> tools) {
// @formatter:on
public ChatCompletionRequest(String model, List<AnthropicMessage> messages, String system, Integer maxTokens,
Float temperature, Boolean stream) {
Double temperature, Boolean stream) {
this(model, messages, system, maxTokens, null, null, stream, temperature, null, null, null);
}
public ChatCompletionRequest(String model, List<AnthropicMessage> messages, String system, Integer maxTokens,
List<String> stopSequences, Float temperature, Boolean stream) {
List<String> stopSequences, Double temperature, Boolean stream) {
this(model, messages, system, maxTokens, null, stopSequences, stream, temperature, null, null, null);
}
@@ -292,9 +292,9 @@ public class AnthropicApi {
private Boolean stream = false;
private Float temperature;
private Double temperature;
private Float topP;
private Double topP;
private Integer topK;
@@ -357,12 +357,12 @@ public class AnthropicApi {
return this;
}
public ChatCompletionRequestBuilder withTemperature(Float temperature) {
public ChatCompletionRequestBuilder withTemperature(Double temperature) {
this.temperature = temperature;
return this;
}
public ChatCompletionRequestBuilder withTopP(Float topP) {
public ChatCompletionRequestBuilder withTopP(Double topP) {
this.topP = topP;
return this;
}

View File

@@ -120,7 +120,7 @@ class AnthropicChatModelIT {
@Test
void streamingWithTokenUsage() {
var promptOptions = AnthropicChatOptions.builder().withTemperature(0f).build();
var promptOptions = AnthropicChatOptions.builder().withTemperature(0.0).build();
var prompt = new Prompt("List two colors of the Polish flag. Be brief.", promptOptions);
var streamingTokenUsage = this.chatModel.stream(prompt).blockLast().getMetadata().getUsage();

View File

@@ -71,9 +71,9 @@ public class AnthropicChatModelObservationIT {
.withModel(AnthropicApi.ChatModel.CLAUDE_3_HAIKU.getValue())
.withMaxTokens(2048)
.withStopSequences(List.of("this-is-the-end"))
.withTemperature(0.7f)
.withTemperature(0.7)
.withTopK(1)
.withTopP(1f)
.withTopP(1.0)
.build();
Prompt prompt = new Prompt("Why does a raven look like a desk?", options);
@@ -93,9 +93,9 @@ public class AnthropicChatModelObservationIT {
.withModel(AnthropicApi.ChatModel.CLAUDE_3_HAIKU.getValue())
.withMaxTokens(2048)
.withStopSequences(List.of("this-is-the-end"))
.withTemperature(0.7f)
.withTemperature(0.7)
.withTopK(1)
.withTopP(1f)
.withTopP(1.0)
.build();
Prompt prompt = new Prompt("Why does a raven look like a desk?", options);

View File

@@ -31,7 +31,7 @@ public class ChatCompletionRequestTests {
public void createRequestWithChatOptions() {
var client = new AnthropicChatModel(new AnthropicApi("TEST"),
AnthropicChatOptions.builder().withModel("DEFAULT_MODEL").withTemperature(66.6f).build());
AnthropicChatOptions.builder().withModel("DEFAULT_MODEL").withTemperature(66.6).build());
var request = client.createRequest(new Prompt("Test message content"), false);
@@ -39,16 +39,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",
AnthropicChatOptions.builder().withModel("PROMPT_MODEL").withTemperature(99.9f).build()), true);
AnthropicChatOptions.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);
}
}

View File

@@ -45,7 +45,7 @@ public class AnthropicApiIT {
Role.USER);
ResponseEntity<ChatCompletionResponse> response = anthropicApi
.chatCompletionEntity(new ChatCompletionRequest(AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
List.of(chatCompletionMessage), null, 100, 0.8f, false));
List.of(chatCompletionMessage), null, 100, 0.8, false));
System.out.println(response);
assertThat(response).isNotNull();
@@ -58,9 +58,8 @@ public class AnthropicApiIT {
AnthropicMessage chatCompletionMessage = new AnthropicMessage(List.of(new ContentBlock("Tell me a Joke?")),
Role.USER);
Flux<ChatCompletionResponse> response = anthropicApi
.chatCompletionStream(new ChatCompletionRequest(AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
List.of(chatCompletionMessage), null, 100, 0.8f, true));
Flux<ChatCompletionResponse> response = anthropicApi.chatCompletionStream(new ChatCompletionRequest(
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), null, 100, 0.8, true));
assertThat(response).isNotNull();

View File

@@ -107,8 +107,8 @@ public class AnthropicApiLegacyToolIT {
Role.USER);
ChatCompletionRequest chatCompletionRequest = new ChatCompletionRequest(
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), systemPrompt, 500,
0.8f, false);
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), systemPrompt, 500, 0.8,
false);
ResponseEntity<ChatCompletionResponse> chatCompletion = doCall(chatCompletionRequest);
@@ -147,7 +147,7 @@ public class AnthropicApiLegacyToolIT {
AnthropicMessage chatCompletionMessage2 = new AnthropicMessage(List.of(new ContentBlock(content)), Role.USER);
return doCall(new ChatCompletionRequest(AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
List.of(chatCompletionMessage2), null, 500, 0.8f, false));
List.of(chatCompletionMessage2), null, 500, 0.8, false));
}
}

View File

@@ -108,7 +108,7 @@ public class AnthropicApiToolIT {
.withModel(AnthropicApi.ChatModel.CLAUDE_3_OPUS)
.withMessages(messageConversation)
.withMaxTokens(1500)
.withTemperature(0.8f)
.withTemperature(0.8)
.withTools(tools)
.build();