Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance Chat Options #2235

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -90,14 +91,15 @@ public static AnthropicChatOptions fromOptions(AnthropicChatOptions fromOptions)
return builder().model(fromOptions.getModel())
.maxTokens(fromOptions.getMaxTokens())
.metadata(fromOptions.getMetadata())
.stopSequences(fromOptions.getStopSequences())
.stopSequences(
fromOptions.getStopSequences() != null ? new ArrayList<>(fromOptions.getStopSequences()) : null)
.temperature(fromOptions.getTemperature())
.topP(fromOptions.getTopP())
.topK(fromOptions.getTopK())
.toolCallbacks(fromOptions.getToolCallbacks())
.toolNames(fromOptions.getToolNames())
.internalToolExecutionEnabled(fromOptions.isInternalToolExecutionEnabled())
.toolContext(fromOptions.getToolContext())
.toolContext(fromOptions.getToolContext() != null ? new HashMap<>(fromOptions.getToolContext()) : null)
.build();
}

Expand Down Expand Up @@ -271,10 +273,35 @@ public void setToolContext(Map<String, Object> toolContext) {
}

@Override
@SuppressWarnings("unchecked")
public AnthropicChatOptions copy() {
return fromOptions(this);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AnthropicChatOptions that)) {
return false;
}
return Objects.equals(this.model, that.model) && Objects.equals(this.maxTokens, that.maxTokens)
&& Objects.equals(this.metadata, that.metadata)
&& Objects.equals(this.stopSequences, that.stopSequences)
&& Objects.equals(this.temperature, that.temperature) && Objects.equals(this.topP, that.topP)
&& Objects.equals(this.topK, that.topK) && Objects.equals(this.toolCallbacks, that.toolCallbacks)
&& Objects.equals(this.toolNames, that.toolNames)
&& Objects.equals(this.internalToolExecutionEnabled, that.internalToolExecutionEnabled)
&& Objects.equals(this.toolContext, that.toolContext);
}

@Override
public int hashCode() {
return Objects.hash(model, maxTokens, metadata, stopSequences, temperature, topP, topK, toolCallbacks,
toolNames, internalToolExecutionEnabled, toolContext);
}

public static class Builder {

private final AnthropicChatOptions options = new AnthropicChatOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.anthropic;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import org.springframework.ai.anthropic.api.AnthropicApi.ChatCompletionRequest.Metadata;

/**
* Tests for {@link AnthropicChatOptions}.
*
* @author Alexandros Pappas
*/
class AnthropicChatOptionsTests {

@Test
void testBuilderWithAllFields() {
AnthropicChatOptions options = AnthropicChatOptions.builder()
.model("test-model")
.maxTokens(100)
.stopSequences(List.of("stop1", "stop2"))
.temperature(0.7)
.topP(0.8)
.topK(50)
.metadata(new Metadata("userId_123"))
.build();

assertThat(options).extracting("model", "maxTokens", "stopSequences", "temperature", "topP", "topK", "metadata")
.containsExactly("test-model", 100, List.of("stop1", "stop2"), 0.7, 0.8, 50, new Metadata("userId_123"));
}

@Test
void testCopy() {
AnthropicChatOptions original = AnthropicChatOptions.builder()
.model("test-model")
.maxTokens(100)
.stopSequences(List.of("stop1", "stop2"))
.temperature(0.7)
.topP(0.8)
.topK(50)
.metadata(new Metadata("userId_123"))
.toolContext(Map.of("key1", "value1"))
.build();

AnthropicChatOptions copied = original.copy();

assertThat(copied).isNotSameAs(original).isEqualTo(original);
// Ensure deep copy
assertThat(copied.getStopSequences()).isNotSameAs(original.getStopSequences());
assertThat(copied.getToolContext()).isNotSameAs(original.getToolContext());
}

@Test
void testSetters() {
AnthropicChatOptions options = new AnthropicChatOptions();
options.setModel("test-model");
options.setMaxTokens(100);
options.setTemperature(0.7);
options.setTopK(50);
options.setTopP(0.8);
options.setStopSequences(List.of("stop1", "stop2"));
options.setMetadata(new Metadata("userId_123"));

assertThat(options.getModel()).isEqualTo("test-model");
assertThat(options.getMaxTokens()).isEqualTo(100);
assertThat(options.getTemperature()).isEqualTo(0.7);
assertThat(options.getTopK()).isEqualTo(50);
assertThat(options.getTopP()).isEqualTo(0.8);
assertThat(options.getStopSequences()).isEqualTo(List.of("stop1", "stop2"));
assertThat(options.getMetadata()).isEqualTo(new Metadata("userId_123"));
}

@Test
void testDefaultValues() {
AnthropicChatOptions options = new AnthropicChatOptions();
assertThat(options.getModel()).isNull();
assertThat(options.getMaxTokens()).isNull();
assertThat(options.getTemperature()).isNull();
assertThat(options.getTopK()).isNull();
assertThat(options.getTopP()).isNull();
assertThat(options.getStopSequences()).isNull();
assertThat(options.getMetadata()).isNull();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.azure.ai.openai.models.AzureChatEnhancementConfiguration;
Expand All @@ -46,6 +47,7 @@
* @author Thomas Vitale
* @author Soby Chacko
* @author Ilayaperumal Gopinathan
* @author Alexandros Pappas
*/
@JsonInclude(Include.NON_NULL)
public class AzureOpenAiChatOptions implements ToolCallingChatOptions {
Expand Down Expand Up @@ -250,18 +252,18 @@ public static AzureOpenAiChatOptions fromOptions(AzureOpenAiChatOptions fromOpti
.maxTokens(fromOptions.getMaxTokens())
.N(fromOptions.getN())
.presencePenalty(fromOptions.getPresencePenalty() != null ? fromOptions.getPresencePenalty() : null)
.stop(fromOptions.getStop())
.stop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null)
.temperature(fromOptions.getTemperature())
.topP(fromOptions.getTopP())
.user(fromOptions.getUser())
.functionCallbacks(fromOptions.getFunctionCallbacks())
.functions(fromOptions.getFunctions())
.functions(fromOptions.getFunctions() != null ? new HashSet<>(fromOptions.getFunctions()) : null)
.responseFormat(fromOptions.getResponseFormat())
.seed(fromOptions.getSeed())
.logprobs(fromOptions.isLogprobs())
.topLogprobs(fromOptions.getTopLogProbs())
.enhancements(fromOptions.getEnhancements())
.toolContext(fromOptions.getToolContext())
.toolContext(fromOptions.getToolContext() != null ? new HashMap<>(fromOptions.getToolContext()) : null)
.internalToolExecutionEnabled(fromOptions.isInternalToolExecutionEnabled())
.streamOptions(fromOptions.getStreamOptions())
.toolCallbacks(fromOptions.getToolCallbacks())
Expand Down Expand Up @@ -479,10 +481,44 @@ public void setStreamOptions(ChatCompletionStreamOptions streamOptions) {
}

@Override
@SuppressWarnings("unchecked")
public AzureOpenAiChatOptions copy() {
return fromOptions(this);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AzureOpenAiChatOptions that)) {
return false;
}
return Objects.equals(this.logitBias, that.logitBias) && Objects.equals(this.user, that.user)
&& Objects.equals(this.n, that.n) && Objects.equals(this.stop, that.stop)
&& Objects.equals(this.deploymentName, that.deploymentName)
&& Objects.equals(this.responseFormat, that.responseFormat)

&& Objects.equals(this.toolCallbacks, that.toolCallbacks)
&& Objects.equals(this.toolNames, that.toolNames)
&& Objects.equals(this.internalToolExecutionEnabled, that.internalToolExecutionEnabled)
&& Objects.equals(this.logprobs, that.logprobs) && Objects.equals(this.topLogProbs, that.topLogProbs)
&& Objects.equals(this.enhancements, that.enhancements)
&& Objects.equals(this.streamOptions, that.streamOptions)
&& Objects.equals(this.toolContext, that.toolContext) && Objects.equals(this.maxTokens, that.maxTokens)
&& Objects.equals(this.frequencyPenalty, that.frequencyPenalty)
&& Objects.equals(this.presencePenalty, that.presencePenalty)
&& Objects.equals(this.temperature, that.temperature) && Objects.equals(this.topP, that.topP);
}

@Override
public int hashCode() {
return Objects.hash(this.logitBias, this.user, this.n, this.stop, this.deploymentName, this.responseFormat,
this.toolCallbacks, this.toolNames, this.internalToolExecutionEnabled, this.seed, this.logprobs,
this.topLogProbs, this.enhancements, this.streamOptions, this.toolContext, this.maxTokens,
this.frequencyPenalty, this.presencePenalty, this.temperature, this.topP);
}

public static class Builder {

protected AzureOpenAiChatOptions options;
Expand Down
Loading