Add Bedrock Anthropic Claude 3 models support

- Created low-leverl anthropic messages API for Claude 3.
   Use the new message API.
 - Add Chat Client with tests.
 - Add bedrok anthropic 3 docs.
 - Add multibudality support + tests.
 - Add auto-configuraiton & tests.
 - Rename Athropic to Athropic3 in class names to avoid confusion with previous Bedrock Anthropic 2 impl.
This commit is contained in:
Ben Middleton
2024-03-07 16:36:11 +00:00
committed by Christian Tzolov
parent 7634c6b780
commit e004751842
20 changed files with 1839 additions and 8 deletions

View File

@@ -115,7 +115,6 @@ public class AnthropicChatBedrockApi extends
private Integer topK;// = 10;
private Float topP;
private List<String> stopSequences;
// private String anthropicVersion = DEFAULT_ANTHROPIC_VERSION;
private String anthropicVersion;
private Builder(String prompt) {

View File

@@ -0,0 +1,166 @@
/*
* Copyright 2023 - 2024 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.bedrock.anthropic3;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.chat.prompt.ChatOptions;
import java.util.List;
/**
* @author Ben Middleton
* @since 1.0.0
*/
@JsonInclude(Include.NON_NULL)
public class Anthropic3ChatOptions implements ChatOptions {
// @formatter:off
/**
* Controls the randomness of the output. Values can range over [0.0,1.0], inclusive. A value closer to 1.0 will
* produce responses that are more varied, while a value closer to 0.0 will typically result in less surprising
* responses from the generative. This value specifies default to be used by the backend while making the call to
* the generative.
*/
private @JsonProperty("temperature") Float temperature;
/**
* Specify the maximum number of tokens to use in the generated response. Note that the models may stop before
* reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate. We
* recommend a limit of 4,000 tokens for optimal performance.
*/
private @JsonProperty("max_tokens") Integer maxTokens;
/**
* Specify the number of token choices the generative uses to generate the next token.
*/
private @JsonProperty("top_k") Integer topK;
/**
* The maximum cumulative probability of tokens to consider when sampling. The generative uses combined Top-k and
* nucleus sampling. Nucleus sampling considers the smallest set of tokens whose probability sum is at least topP.
*/
private @JsonProperty("top_p") Float topP;
/**
* Configure up to four sequences that the generative recognizes. After a stop sequence, the generative stops
* generating further tokens. The returned text doesn't contain the stop sequence.
*/
private @JsonProperty("stop_sequences") List<String> stopSequences;
/**
* The version of the generative to use. The default value is bedrock-2023-05-31.
*/
private @JsonProperty("anthropic_version") String anthropicVersion;
// @formatter:on
public static Builder builder() {
return new Builder();
}
public static class Builder {
private final Anthropic3ChatOptions options = new Anthropic3ChatOptions();
public Builder withTemperature(Float temperature) {
this.options.setTemperature(temperature);
return this;
}
public Builder withMaxTokens(Integer maxTokens) {
this.options.setMaxTokens(maxTokens);
return this;
}
public Builder withTopK(Integer topK) {
this.options.setTopK(topK);
return this;
}
public Builder withTopP(Float topP) {
this.options.setTopP(topP);
return this;
}
public Builder withStopSequences(List<String> stopSequences) {
this.options.setStopSequences(stopSequences);
return this;
}
public Builder withAnthropicVersion(String anthropicVersion) {
this.options.setAnthropicVersion(anthropicVersion);
return this;
}
public Anthropic3ChatOptions build() {
return this.options;
}
}
@Override
public Float getTemperature() {
return this.temperature;
}
public void setTemperature(Float temperature) {
this.temperature = temperature;
}
public Integer getMaxTokens() {
return this.maxTokens;
}
public void setMaxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
}
@Override
public Integer getTopK() {
return this.topK;
}
public void setTopK(Integer topK) {
this.topK = topK;
}
@Override
public Float getTopP() {
return this.topP;
}
public void setTopP(Float topP) {
this.topP = topP;
}
public List<String> getStopSequences() {
return this.stopSequences;
}
public void setStopSequences(List<String> stopSequences) {
this.stopSequences = stopSequences;
}
public String getAnthropicVersion() {
return this.anthropicVersion;
}
public void setAnthropicVersion(String anthropicVersion) {
this.anthropicVersion = anthropicVersion;
}
}

View File

@@ -0,0 +1,190 @@
/*
* Copyright 2023 - 2024 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.bedrock.anthropic3;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatRequest;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatResponse;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse.StreamingType;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.MediaContent;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.ChatCompletionMessage;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.ChatCompletionMessage.Role;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.metadata.ChatGenerationMetadata;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.util.CollectionUtils;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/**
* Java {@link ChatClient} and {@link StreamingChatClient} for the Bedrock Anthropic chat
* generative.
*
* @author Ben Middleton
* @author Christian Tzolov
* @since 1.0.0
*/
public class BedrockAnthropic3ChatClient implements ChatClient, StreamingChatClient {
private final Anthropic3ChatBedrockApi anthropicChatApi;
private final Anthropic3ChatOptions defaultOptions;
public BedrockAnthropic3ChatClient(Anthropic3ChatBedrockApi chatApi) {
this(chatApi,
Anthropic3ChatOptions.builder()
.withTemperature(0.8f)
.withMaxTokens(500)
.withTopK(10)
.withAnthropicVersion(Anthropic3ChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
.build());
}
public BedrockAnthropic3ChatClient(Anthropic3ChatBedrockApi chatApi, Anthropic3ChatOptions options) {
this.anthropicChatApi = chatApi;
this.defaultOptions = options;
}
@Override
public ChatResponse call(Prompt prompt) {
AnthropicChatRequest request = createRequest(prompt);
AnthropicChatResponse response = this.anthropicChatApi.chatCompletion(request);
return new ChatResponse(List.of(new Generation(response.content().get(0).text())));
}
@Override
public Flux<ChatResponse> stream(Prompt prompt) {
AnthropicChatRequest request = createRequest(prompt);
Flux<Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse> fluxResponse = this.anthropicChatApi
.chatCompletionStream(request);
AtomicReference<Integer> inputTokens = new AtomicReference<>(0);
return fluxResponse.map(response -> {
if (response.type() == StreamingType.MESSAGE_START) {
inputTokens.set(response.message().usage().inputTokens());
}
String content = response.type() == StreamingType.CONTENT_BLOCK_DELTA ? response.delta().text() : "";
var generation = new Generation(content);
if (response.type() == StreamingType.MESSAGE_DELTA) {
generation = generation.withGenerationMetadata(ChatGenerationMetadata
.from(response.delta().stopReason(), new Anthropic3ChatBedrockApi.AnthropicUsage(inputTokens.get(),
response.usage().outputTokens())));
}
return new ChatResponse(List.of(generation));
});
}
/**
* Accessible for testing.
*/
AnthropicChatRequest createRequest(Prompt prompt) {
AnthropicChatRequest request = AnthropicChatRequest.builder(toAnthropicMessages(prompt))
.withSystem(toAnthropicSystemContext(prompt))
.build();
if (this.defaultOptions != null) {
request = ModelOptionsUtils.merge(request, this.defaultOptions, AnthropicChatRequest.class);
}
if (prompt.getOptions() != null) {
if (prompt.getOptions() instanceof ChatOptions runtimeOptions) {
Anthropic3ChatOptions updatedRuntimeOptions = ModelOptionsUtils.copyToTarget(runtimeOptions,
ChatOptions.class, Anthropic3ChatOptions.class);
request = ModelOptionsUtils.merge(updatedRuntimeOptions, request, AnthropicChatRequest.class);
}
else {
throw new IllegalArgumentException("Prompt options are not of type ChatOptions: "
+ prompt.getOptions().getClass().getSimpleName());
}
}
return request;
}
/**
* Extracts system context from prompt.
* @param prompt The prompt.
* @return The system context.
*/
private String toAnthropicSystemContext(Prompt prompt) {
return prompt.getInstructions()
.stream()
.filter(m -> m.getMessageType() == MessageType.SYSTEM)
.map(Message::getContent)
.collect(Collectors.joining(System.lineSeparator()));
}
/**
* Extracts list of messages from prompt.
* @param prompt The prompt.
* @return The list of {@link ChatCompletionMessage}.
*/
private List<ChatCompletionMessage> toAnthropicMessages(Prompt prompt) {
return prompt.getInstructions()
.stream()
.filter(m -> m.getMessageType() == MessageType.USER || m.getMessageType() == MessageType.ASSISTANT)
.map(message -> {
List<MediaContent> contents = new ArrayList<>(List.of(new MediaContent(message.getContent())));
if (!CollectionUtils.isEmpty(message.getMedia())) {
List<MediaContent> mediaContent = message.getMedia()
.stream()
.map(media -> new MediaContent(media.getMimeType().toString(),
this.fromMediaData(media.getData())))
.toList();
contents.addAll(mediaContent);
}
return new ChatCompletionMessage(contents, Role.valueOf(message.getMessageType().name()));
})
.toList();
}
private String fromMediaData(Object mediaData) {
if (mediaData instanceof byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
else if (mediaData instanceof String text) {
return text;
}
else {
throw new IllegalArgumentException("Unsupported media data type: " + mediaData.getClass().getSimpleName());
}
}
}

View File

@@ -0,0 +1,446 @@
/*
* Copyright 2023 - 2024 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.bedrock.anthropic3.api;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatRequest;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatResponse;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import java.util.List;
/**
* Based on Bedrock's <a href=
* "https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html">Anthropic
* Claude Messages API</a>.
*
* It is meant to replace the previous Chat API, which is now deprecated.
*
* @author Ben Middleton
* @author Christian Tzolov
* @since 1.0.0
*/
// @formatter:off
public class Anthropic3ChatBedrockApi extends
AbstractBedrockApi<AnthropicChatRequest, AnthropicChatResponse, AnthropicChatStreamingResponse> {
/**
* Default version of the Anthropic chat model.
*/
public static final String DEFAULT_ANTHROPIC_VERSION = "bedrock-2023-05-31";
/**
* Create a new AnthropicChatBedrockApi instance using the default credentials provider chain, the default object.
* @param modelId The model id to use. See the {@link AnthropicChatModel} for the supported models.
* @param region The AWS region to use.
*/
public Anthropic3ChatBedrockApi(String modelId, String region) {
super(modelId, region);
}
/**
* Create a new AnthropicChatBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use. See the {@link AnthropicChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
*/
public Anthropic3ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, String region,
ObjectMapper objectMapper) {
super(modelId, credentialsProvider, region, objectMapper);
}
// https://github.com/build-on-aws/amazon-bedrock-java-examples/blob/main/example_code/bedrock-runtime/src/main/java/aws/community/examples/InvokeBedrockStreamingAsync.java
// https://docs.anthropic.com/claude/reference/complete_post
// https://docs.aws.amazon.com/bedrock/latest/userguide/br-product-ids.html
// Anthropic Claude models: https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
/**
* AnthropicChatRequest encapsulates the request parameters for the Anthropic messages model.
* https://docs.anthropic.com/claude/reference/messages_post
*
* @param messages A list of messages comprising the conversation so far.
* @param system A system prompt, providing context and instructions to Claude, such as specifying a particular goal
* or role.
* @param temperature (default 0.5) The temperature to use for the chat. You should either alter temperature or
* top_p, but not both.
* @param maxTokens (default 200) Specify the maximum number of tokens to use in the generated response.
* Note that the models may stop before reaching this maximum. This parameter only specifies the absolute maximum
* number of tokens to generate. We recommend a limit of 4,000 tokens for optimal performance.
* @param topK (default 250) Specify the number of token choices the model uses to generate the next token.
* @param topP (default 1) Nucleus sampling to specify the cumulative probability of the next token in range [0,1].
* In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in
* decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You
* should either alter temperature or top_p, but not both.
* @param stopSequences (defaults to "\n\nHuman:") Configure up to four sequences that the model recognizes. After a
* stop sequence, the model stops generating further tokens. The returned text doesn't contain the stop sequence.
* @param anthropicVersion The version of the model to use. The default value is bedrock-2023-05-31.
*/
@JsonInclude(Include.NON_NULL)
public record AnthropicChatRequest(
@JsonProperty("messages") List<ChatCompletionMessage> messages,
@JsonProperty("system") String system,
@JsonProperty("temperature") Float temperature,
@JsonProperty("max_tokens") Integer maxTokens,
@JsonProperty("top_k") Integer topK,
@JsonProperty("top_p") Float topP,
@JsonProperty("stop_sequences") List<String> stopSequences,
@JsonProperty("anthropic_version") String anthropicVersion) {
public static Builder builder(List<ChatCompletionMessage> messages) {
return new Builder(messages);
}
public static class Builder {
private final List<ChatCompletionMessage> messages;
private String system;
private Float temperature;// = 0.7f;
private Integer maxTokens;// = 500;
private Integer topK;// = 10;
private Float topP;
private List<String> stopSequences;
private String anthropicVersion;
private Builder(List<ChatCompletionMessage> messages) {
this.messages = messages;
}
public Builder withSystem(String system) {
this.system = system;
return this;
}
public Builder withTemperature(Float temperature) {
this.temperature = temperature;
return this;
}
public Builder withMaxTokens(Integer maxTokens) {
this.maxTokens = maxTokens;
return this;
}
public Builder withTopK(Integer topK) {
this.topK = topK;
return this;
}
public Builder withTopP(Float tpoP) {
this.topP = tpoP;
return this;
}
public Builder withStopSequences(List<String> stopSequences) {
this.stopSequences = stopSequences;
return this;
}
public Builder withAnthropicVersion(String anthropicVersion) {
this.anthropicVersion = anthropicVersion;
return this;
}
public AnthropicChatRequest build() {
return new AnthropicChatRequest(
messages,
system,
temperature,
maxTokens,
topK,
topP,
stopSequences,
anthropicVersion
);
}
}
}
/**
* @param type the content type can be "text" or "image".
* @param source The source of the media content. Applicable for "image" types only.
* @param text The text of the message. Applicable for "text" types only.
* @param index The index of the content block. Applicable only for streaming
* responses.
*/
@JsonInclude(Include.NON_NULL)
public record MediaContent( // @formatter:off
@JsonProperty("type") Type type,
@JsonProperty("source") Source source,
@JsonProperty("text") String text,
@JsonProperty("index") Integer index // applicable only for streaming responses.
) {
// @formatter:on
public MediaContent(String mediaType, String data) {
this(new Source(mediaType, data));
}
public MediaContent(Source source) {
this(Type.IMAGE, source, null, null);
}
public MediaContent(String text) {
this(Type.TEXT, null, text, null);
}
/**
* The type of this message.
*/
public enum Type {
/**
* Text message.
*/
@JsonProperty("text")
TEXT,
/**
* Image message.
*/
@JsonProperty("image")
IMAGE
}
/**
* The source of the media content. (Applicable for "image" types only)
*
* @param type The type of the media content. Only "base64" is supported at the
* moment.
* @param mediaType The media type of the content. For example, "image/png" or
* "image/jpeg".
* @param data The base64-encoded data of the content.
*/
@JsonInclude(Include.NON_NULL)
public record Source( // @formatter:off
@JsonProperty("type") String type,
@JsonProperty("media_type") String mediaType,
@JsonProperty("data") String data) {
// @formatter:on
public Source(String mediaType, String data) {
this("base64", mediaType, data);
}
}
}
/**
* Message comprising the conversation.
*
* @param content The contents of the message.
* @param role The role of the messages author. Could be one of the {@link Role}
* types.
*/
@JsonInclude(Include.NON_NULL)
public record ChatCompletionMessage(@JsonProperty("content") List<MediaContent> content,
@JsonProperty("role") Role role) {
/**
* The role of the author of this message.
*/
public enum Role {
/**
* User message.
*/
@JsonProperty("user")
USER,
/**
* Assistant message.
*/
@JsonProperty("assistant")
ASSISTANT
}
}
/**
* Encapsulates the metrics about the model invocation.
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html#model-parameters-anthropic-claude-messages-request-response
*
* @param inputTokens The number of tokens in the input prompt.
* @param outputTokens The number of tokens in the generated text.
*/
@JsonInclude(Include.NON_NULL)
public record AnthropicUsage(@JsonProperty("input_tokens") Integer inputTokens,
@JsonProperty("output_tokens") Integer outputTokens) {
}
/**
* AnthropicChatResponse encapsulates the response parameters for the Anthropic
* messages model.
*
* @param id The unique response identifier.
* @param model The ID for the Anthropic Claude model that made the request.
* @param type The type of the response.
* @param role The role of the response.
* @param content The list of generated text.
* @param stopReason The reason the model stopped generating text: end_turn The
* model reached a natural stopping point. max_tokens The generated text exceeded
* the value of the max_tokens input field or exceeded the maximum number of tokens
* that the model supports. stop_sequence The model generated one of the stop
* sequences that you specified in the stop_sequences input field.
* @param stopSequence The stop sequence that caused the model to stop generating
* text.
* @param usage Metrics about the model invocation.
*/
@JsonInclude(Include.NON_NULL)
public record AnthropicChatResponse(@JsonProperty("id") String id, @JsonProperty("model") String model,
@JsonProperty("type") String type, @JsonProperty("role") String role,
@JsonProperty("content") List<MediaContent> content, @JsonProperty("stop_reason") String stopReason,
@JsonProperty("stop_sequence") String stopSequence, @JsonProperty("usage") AnthropicUsage usage) {
}
/**
* AnthropicChatStreamingResponse encapsulates the streaming response parameters for
* the Anthropic messages model.
* https://docs.anthropic.com/claude/reference/messages-streaming
*
* @param type The streaming type.
* @param message The message details that made the request.
* @param index The delta index.
* @param contentBlock The generated text.
* @param delta The delta.
* @param usage The usage data.
*/
@JsonInclude(Include.NON_NULL)
public record AnthropicChatStreamingResponse(@JsonProperty("type") StreamingType type,
@JsonProperty("message") AnthropicChatResponse message, @JsonProperty("index") Integer index,
@JsonProperty("content_block") MediaContent contentBlock, @JsonProperty("delta") Delta delta,
@JsonProperty("usage") AnthropicUsage usage) {
/**
* The streaming type of this message.
*/
public enum StreamingType {
/**
* Message start.
*/
@JsonProperty("message_start")
MESSAGE_START,
/**
* Content block start.
*/
@JsonProperty("content_block_start")
CONTENT_BLOCK_START,
/**
* Ping.
*/
@JsonProperty("ping")
PING,
/**
* Content block delta.
*/
@JsonProperty("content_block_delta")
CONTENT_BLOCK_DELTA,
/**
* Content block stop.
*/
@JsonProperty("content_block_stop")
CONTENT_BLOCK_STOP,
/**
* Message delta.
*/
@JsonProperty("message_delta")
MESSAGE_DELTA,
/**
* Message stop.
*/
@JsonProperty("message_stop")
MESSAGE_STOP
}
/**
* Encapsulates a delta.
* https://docs.anthropic.com/claude/reference/messages-streaming *
*
* @param type The type of the message.
* @param text The text message.
* @param stopReason The stop reason.
* @param stopSequence The stop sequence.
*/
@JsonInclude(Include.NON_NULL)
public record Delta(@JsonProperty("type") String type, @JsonProperty("text") String text,
@JsonProperty("stop_reason") String stopReason, @JsonProperty("stop_sequence") String stopSequence) {
}
}
/**
* Anthropic models version.
*/
public enum AnthropicChatModel {
/**
* anthropic.claude-instant-v1
*/
CLAUDE_INSTANT_V1("anthropic.claude-instant-v1"),
/**
* anthropic.claude-v2
*/
CLAUDE_V2("anthropic.claude-v2"),
/**
* anthropic.claude-v2:1
*/
CLAUDE_V21("anthropic.claude-v2:1"),
/**
* anthropic.claude-3-sonnet-20240229-v1:0
*/
CLAUDE_V3_SONNET("anthropic.claude-3-sonnet-20240229-v1:0"),
/**
* anthropic.claude-3-haiku-20240307-v1:0
*/
CLAUDE_V3_HAIKU("anthropic.claude-3-haiku-20240307-v1:0");
private final String id;
/**
* @return The model id.
*/
public String id() {
return id;
}
AnthropicChatModel(String value) {
this.id = value;
}
}
@Override
public AnthropicChatResponse chatCompletion(AnthropicChatRequest anthropicRequest) {
Assert.notNull(anthropicRequest, "'anthropicRequest' must not be null");
return this.internalInvocation(anthropicRequest, AnthropicChatResponse.class);
}
@Override
public Flux<AnthropicChatStreamingResponse> chatCompletionStream(AnthropicChatRequest anthropicRequest) {
Assert.notNull(anthropicRequest, "'anthropicRequest' must not be null");
return this.internalInvocationStream(anthropicRequest, AnthropicChatStreamingResponse.class);
}
}
// @formatter:on

View File

@@ -17,6 +17,8 @@ package org.springframework.ai.bedrock.aot;
import org.springframework.ai.bedrock.anthropic.AnthropicChatOptions;
import org.springframework.ai.bedrock.anthropic.api.AnthropicChatBedrockApi;
import org.springframework.ai.bedrock.anthropic3.Anthropic3ChatOptions;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi;
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.cohere.BedrockCohereChatOptions;
import org.springframework.ai.bedrock.cohere.BedrockCohereEmbeddingOptions;
@@ -77,6 +79,11 @@ public class BedrockRuntimeHints implements RuntimeHintsRegistrar {
hints.reflection().registerType(tr, mcs);
for (var tr : findJsonAnnotatedClassesInPackage(AnthropicChatOptions.class))
hints.reflection().registerType(tr, mcs);
for (var tr : findJsonAnnotatedClassesInPackage(Anthropic3ChatBedrockApi.class))
hints.reflection().registerType(tr, mcs);
for (var tr : findJsonAnnotatedClassesInPackage(Anthropic3ChatOptions.class))
hints.reflection().registerType(tr, mcs);
}
}

View File

@@ -18,11 +18,13 @@ package org.springframework.ai.bedrock.anthropic.api;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import org.springframework.ai.bedrock.anthropic.api.AnthropicChatBedrockApi.AnthropicChatRequest;
@@ -41,7 +43,7 @@ public class AnthropicChatBedrockApiIT {
private final Logger logger = LoggerFactory.getLogger(AnthropicChatBedrockApiIT.class);
private AnthropicChatBedrockApi anthropicChatApi = new AnthropicChatBedrockApi(AnthropicChatModel.CLAUDE_V2.id(),
Region.EU_CENTRAL_1.id());
EnvironmentVariableCredentialsProvider.create(), Region.US_WEST_2.id(), new ObjectMapper());
@Test
public void chatCompletion() {

View File

@@ -0,0 +1,207 @@
/*
* Copyright 2023 - 2024 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.bedrock.anthropic3;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Media;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.parser.BeanOutputParser;
import org.springframework.ai.parser.ListOutputParser;
import org.springframework.ai.parser.MapOutputParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "AWS_ACCESS_KEY_ID", matches = ".*")
@EnabledIfEnvironmentVariable(named = "AWS_SECRET_ACCESS_KEY", matches = ".*")
class BedrockAnthropic3ChatClientIT {
private static final Logger logger = LoggerFactory.getLogger(BedrockAnthropic3ChatClientIT.class);
@Autowired
private BedrockAnthropic3ChatClient client;
@Value("classpath:/prompts/system-message.st")
private Resource systemResource;
@Test
void roleTest() {
UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate"));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
ChatResponse response = client.call(prompt);
assertThat(response.getResult().getOutput().getContent()).contains("Blackbeard");
}
@Test
void outputParser() {
DefaultConversionService conversionService = new DefaultConversionService();
ListOutputParser outputParser = new ListOutputParser(conversionService);
String format = outputParser.getFormat();
String template = """
List five {subject}
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "ice cream flavors.", "format", format));
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = this.client.call(prompt).getResult();
List<String> list = outputParser.parse(generation.getOutput().getContent());
assertThat(list).hasSize(5);
}
@Test
void mapOutputParser() {
MapOutputParser outputParser = new MapOutputParser();
String format = outputParser.getFormat();
String template = """
Provide me a List of {subject}
{format}
Remove the ```json code blocks from the output.
""";
PromptTemplate promptTemplate = new PromptTemplate(template,
Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = client.call(prompt).getResult();
Map<String, Object> result = outputParser.parse(generation.getOutput().getContent());
assertThat(result.get("numbers")).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
}
record ActorsFilmsRecord(String actor, List<String> movies) {
}
@Test
void beanOutputParserRecords() {
BeanOutputParser<ActorsFilmsRecord> outputParser = new BeanOutputParser<>(ActorsFilmsRecord.class);
String format = outputParser.getFormat();
String template = """
Generate the filmography of 5 movies for Tom Hanks.
Remove non JSON tex blocks from the output.
{format}
Provide your answer in the JSON format with the feature names as the keys.
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = client.call(prompt).getResult();
ActorsFilmsRecord actorsFilms = outputParser.parse(generation.getOutput().getContent());
assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
assertThat(actorsFilms.movies()).hasSize(5);
}
@Test
void beanStreamOutputParserRecords() {
BeanOutputParser<ActorsFilmsRecord> outputParser = new BeanOutputParser<>(ActorsFilmsRecord.class);
String format = outputParser.getFormat();
String template = """
Generate the filmography of 5 movies for Tom Hanks.
{format}
Remove Markdown code blocks from the output.
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
Prompt prompt = new Prompt(promptTemplate.createMessage());
String generationTextFromStream = client.stream(prompt)
.collectList()
.block()
.stream()
.map(ChatResponse::getResults)
.flatMap(List::stream)
.map(Generation::getOutput)
.map(AssistantMessage::getContent)
.collect(Collectors.joining());
ActorsFilmsRecord actorsFilms = outputParser.parse(generationTextFromStream);
logger.info("" + actorsFilms);
assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
assertThat(actorsFilms.movies()).hasSize(5);
}
@Test
void multiModalityTest() throws IOException {
byte[] imageData = new ClassPathResource("/test.png").getContentAsByteArray();
var userMessage = new UserMessage("Explain what do you see o this picture?",
List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData)));
ChatResponse response = client.call(new Prompt(List.of(userMessage)));
logger.info(response.getResult().getOutput().getContent());
assertThat(response.getResult().getOutput().getContent()).contains("bananas", "apple", "basket");
}
@SpringBootConfiguration
public static class TestConfiguration {
@Bean
public Anthropic3ChatBedrockApi anthropicApi() {
return new Anthropic3ChatBedrockApi(Anthropic3ChatBedrockApi.AnthropicChatModel.CLAUDE_V3_SONNET.id(),
EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new ObjectMapper());
}
@Bean
public BedrockAnthropic3ChatClient anthropicChatClient(Anthropic3ChatBedrockApi anthropicApi) {
return new BedrockAnthropic3ChatClient(anthropicApi);
}
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2023 - 2024 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.bedrock.anthropic3;
import org.junit.jupiter.api.Test;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import software.amazon.awssdk.regions.Region;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Christian Tzolov
*/
public class BedrockAnthropic3CreateRequestTests {
private Anthropic3ChatBedrockApi anthropicChatApi = new Anthropic3ChatBedrockApi(AnthropicChatModel.CLAUDE_V2.id(),
Region.EU_CENTRAL_1.id());
@Test
public void createRequestWithChatOptions() {
var client = new BedrockAnthropic3ChatClient(anthropicChatApi,
Anthropic3ChatOptions.builder()
.withTemperature(66.6f)
.withTopK(66)
.withTopP(0.66f)
.withMaxTokens(666)
.withAnthropicVersion("X.Y.Z")
.withStopSequences(List.of("stop1", "stop2"))
.build());
var request = client.createRequest(new Prompt("Test message content"));
assertThat(request.messages()).isNotEmpty();
assertThat(request.temperature()).isEqualTo(66.6f);
assertThat(request.topK()).isEqualTo(66);
assertThat(request.topP()).isEqualTo(0.66f);
assertThat(request.maxTokens()).isEqualTo(666);
assertThat(request.anthropicVersion()).isEqualTo("X.Y.Z");
assertThat(request.stopSequences()).containsExactly("stop1", "stop2");
request = client.createRequest(new Prompt("Test message content",
Anthropic3ChatOptions.builder()
.withTemperature(99.9f)
.withTopP(0.99f)
.withMaxTokens(999)
.withAnthropicVersion("zzz")
.withStopSequences(List.of("stop3", "stop4"))
.build()
));
assertThat(request.messages()).isNotEmpty();
assertThat(request.temperature()).isEqualTo(99.9f);
assertThat(request.topK()).as("unchanged from the default options").isEqualTo(66);
assertThat(request.topP()).isEqualTo(0.99f);
assertThat(request.maxTokens()).isEqualTo(999);
assertThat(request.anthropicVersion()).isEqualTo("zzz");
assertThat(request.stopSequences()).containsExactly("stop3", "stop4");
}
}

View File

@@ -0,0 +1,142 @@
/*
* Copyright 2023 - 2024 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.bedrock.anthropic3.api;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatModel;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatRequest;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatResponse;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse.StreamingType;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.MediaContent;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.ChatCompletionMessage;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.ChatCompletionMessage.Role;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.DEFAULT_ANTHROPIC_VERSION;
/**
* @author Ben Middleton
*/
@EnabledIfEnvironmentVariable(named = "AWS_ACCESS_KEY_ID", matches = ".*")
@EnabledIfEnvironmentVariable(named = "AWS_SECRET_ACCESS_KEY", matches = ".*")
public class Anthropic3ChatBedrockApiIT {
private final Logger logger = LoggerFactory.getLogger(Anthropic3ChatBedrockApiIT.class);
private Anthropic3ChatBedrockApi anthropicChatApi = new Anthropic3ChatBedrockApi(
AnthropicChatModel.CLAUDE_INSTANT_V1.id(), EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(), new ObjectMapper());
@Test
public void chatCompletion() {
MediaContent anthropicMessage = new MediaContent("Name 3 famous pirates");
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage(List.of(anthropicMessage), Role.USER);
AnthropicChatRequest request = AnthropicChatRequest.builder(List.of(chatCompletionMessage))
.withTemperature(0.8f)
.withMaxTokens(300)
.withTopK(10)
.withAnthropicVersion(DEFAULT_ANTHROPIC_VERSION)
.build();
AnthropicChatResponse response = anthropicChatApi.chatCompletion(request);
System.out.println(response.content());
assertThat(response).isNotNull();
assertThat(response.content().get(0).text()).isNotEmpty();
assertThat(response.content().get(0).text()).contains("Blackbeard");
assertThat(response.stopReason()).isEqualTo("end_turn");
assertThat(response.stopSequence()).isNull();
assertThat(response.usage().inputTokens()).isGreaterThan(10);
assertThat(response.usage().outputTokens()).isGreaterThan(100);
logger.info("" + response);
}
@Test
public void chatMultiCompletion() {
MediaContent anthropicInitialMessage = new MediaContent("Name 3 famous pirates");
ChatCompletionMessage chatCompletionInitialMessage = new ChatCompletionMessage(List.of(anthropicInitialMessage),
Role.USER);
MediaContent anthropicAssistantMessage = new MediaContent(
"Here are 3 famous pirates: Blackbeard, Calico Jack, Henry Morgan");
ChatCompletionMessage chatCompletionAssistantMessage = new ChatCompletionMessage(
List.of(anthropicAssistantMessage), Role.ASSISTANT);
MediaContent anthropicFollowupMessage = new MediaContent("Why are they famous?");
ChatCompletionMessage chatCompletionFollowupMessage = new ChatCompletionMessage(
List.of(anthropicFollowupMessage), Role.USER);
AnthropicChatRequest request = AnthropicChatRequest
.builder(List.of(chatCompletionInitialMessage, chatCompletionAssistantMessage,
chatCompletionFollowupMessage))
.withTemperature(0.8f)
.withMaxTokens(400)
.withTopK(10)
.withAnthropicVersion(DEFAULT_ANTHROPIC_VERSION)
.build();
AnthropicChatResponse response = anthropicChatApi.chatCompletion(request);
System.out.println(response.content());
assertThat(response).isNotNull();
assertThat(response.content().get(0).text()).isNotEmpty();
assertThat(response.content().get(0).text()).contains("Blackbeard");
assertThat(response.stopReason()).isEqualTo("end_turn");
assertThat(response.stopSequence()).isNull();
assertThat(response.usage().inputTokens()).isGreaterThan(30);
assertThat(response.usage().outputTokens()).isGreaterThan(200);
logger.info("" + response);
}
@Test
public void chatCompletionStream() {
MediaContent anthropicMessage = new MediaContent("Name 3 famous pirates");
ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage(List.of(anthropicMessage), Role.USER);
AnthropicChatRequest request = AnthropicChatRequest.builder(List.of(chatCompletionMessage))
.withTemperature(0.8f)
.withMaxTokens(300)
.withTopK(10)
.withAnthropicVersion(DEFAULT_ANTHROPIC_VERSION)
.build();
Flux<Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse> responseStream = anthropicChatApi
.chatCompletionStream(request);
List<Anthropic3ChatBedrockApi.AnthropicChatStreamingResponse> responses = responseStream.collectList().block();
assertThat(responses).isNotNull();
assertThat(responses).hasSizeGreaterThan(10);
assertThat(responses.stream()
.filter(message -> message.type() == StreamingType.CONTENT_BLOCK_DELTA)
.map(message -> message.delta().text())
.collect(Collectors.joining())).contains("Blackbeard");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

View File

@@ -117,7 +117,7 @@
<azure-open-ai-client.version>1.0.0-beta.7</azure-open-ai-client.version>
<jtokkit.version>1.0.0</jtokkit.version>
<victools.version>4.31.1</victools.version>
<bedrockruntime.version>2.24.8</bedrockruntime.version>
<bedrockruntime.version>2.25.3</bedrockruntime.version>
<jackson.version>2.16.1</jackson.version>
<djl.version>0.26.0</djl.version>
<onnxruntime.version>1.17.0</onnxruntime.version>

View File

@@ -112,7 +112,7 @@
<scope>test</scope>
</dependency>
</dependencies>
</dependencies>
<profiles>
<profile>

View File

@@ -9,7 +9,8 @@
*** xref:api/chat/azure-openai-chat.adoc[Azure OpenAI]
**** xref:api/chat/functions/azure-open-ai-chat-functions.adoc[Function Calling]
*** xref:api/bedrock-chat.adoc[Amazon Bedrock]
**** xref:api/chat/bedrock/bedrock-anthropic.adoc[Anthropic]
**** xref:api/chat/bedrock/bedrock-anthropic3.adoc[Anthropic3]
**** xref:api/chat/bedrock/bedrock-anthropic.adoc[Anthropic2]
**** xref:api/chat/bedrock/bedrock-llama2.adoc[Llama2]
**** xref:api/chat/bedrock/bedrock-cohere.adoc[Cohere]
**** xref:api/chat/bedrock/bedrock-titan.adoc[Titan]
@@ -21,7 +22,7 @@
***** xref:api/chat/functions/vertexai-gemini-chat-functions.adoc[Function Calling]
*** xref:api/chat/mistralai-chat.adoc[Mistral AI]
**** xref:api/chat/functions/mistralai-chat-functions.adoc[Function Calling]
*** xref:api/chat/anthropic-chat.adoc[Anthropic]
*** xref:api/chat/anthropic-chat.adoc[Anthropic 3]
** xref:api/embeddings.adoc[]
*** xref:api/embeddings/openai-embeddings.adoc[OpenAI]
*** xref:api/embeddings/ollama-embeddings.adoc[Ollama]

View File

@@ -1,4 +1,4 @@
= Anthropic Chat
= Anthropic 3 Chat
link:https://www.anthropic.com/[Anthropic Claude] is a family of foundational AI models that can be used in a variety of applications.
For developers and businesses, you can leverage the API access and build directly on top of link:https://www.anthropic.com/api[Anthropic's AI infrastructure].

View File

@@ -1,4 +1,7 @@
= Anthropic Chat
= Bedrock Anthropic 2 Chat
NOTE: The Anthropic 2 Chat API is deprecated and replaced by the new Anthropic Claude 3 Message API.
Please use the xref:api/chat/bedrock/bedrock-anthropic3.adoc[Anthropic Claude 3 Message API] for new projects.
https://www.anthropic.com/product[Anthropic's Claude] is an AI assistant based on Anthropics research into training helpful, honest, and harmless AI systems.
The Claude model has the following high level features
@@ -9,6 +12,9 @@ The Claude model has the following high level features
The https://aws.amazon.com/bedrock/claude[AWS Bedrock Anthropic Model Page] and https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html[Amazon Bedrock User Guide] contains detailed information on how to use the AWS hosted model.
TIP: Anthropics Claude 2 and 3 models are also available directly on the Anthropic's own cloud platform.
Spring AI provides dedicated xref:api/chat/anthropic-chat.adoc[Anthropic Claude] client to access it.
== Prerequisites
Refer to the xref:api/bedrock.adoc[Spring AI documentation on Amazon Bedrock] for setting up API access.

View File

@@ -0,0 +1,288 @@
= Bedrock Anthropic 3
link:https://www.anthropic.com/[Anthropic Claude] is a family of foundational AI models that can be used in a variety of applications.
The Claude model has the following high level features
* 200k Token Context Window: Claude boasts a generous token capacity of 200,000, making it ideal for handling extensive information in applications like technical documentation, codebase, and literary works.
* Supported Tasks: Claude's versatility spans tasks such as summarization, Q&A, trend forecasting, and document comparisons, enabling a wide range of applications from dialogues to content generation.
* AI Safety Features: Built on Anthropic's safety research, Claude prioritizes helpfulness, honesty, and harmlessness in its interactions, reducing brand risk and ensuring responsible AI behavior.
The https://aws.amazon.com/bedrock/claude[AWS Bedrock Anthropic Model Page] and https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html[Amazon Bedrock User Guide] contains detailed information on how to use the AWS hosted model.
TIP: Anthropics Claude 2 and 3 models are also available directly on the Anthropic's own cloud platform.
Spring AI provides dedicated xref:api/chat/anthropic-chat.adoc[Anthropic Claude] client to access it.
== Prerequisites
Refer to the xref:api/bedrock.adoc[Spring AI documentation on Amazon Bedrock] for setting up API access.
=== Add Repositories and BOM
Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the xref:getting-started.adoc#repositories[Repositories] section to add these repositories to your build system.
To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system.
== Auto-configuration
Add the `spring-ai-bedrock-ai-spring-boot-starter` dependency to your project's Maven `pom.xml` file:
[source,xml]
----
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
</dependency>
----
or to your Gradle `build.gradle` build file.
[source,gradle]
----
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
----
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
=== Enable Anthropic Chat
By default the Anthropic model is disabled.
To enable it set the `spring.ai.bedrock.anthropic3.chat.enabled` property to `true`.
Exporting environment variable is one way to set this configuration property:
[source,shell]
----
export SPRING_AI_BEDROCK_ANTHROPIC3_CHAT_ENABLED=true
----
=== Chat Properties
The prefix `spring.ai.bedrock.aws` is the property prefix to configure the connection to AWS Bedrock.
[cols="3,3,1"]
|====
| Property | Description | Default
| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1
| spring.ai.bedrock.aws.access-key | AWS access key. | -
| spring.ai.bedrock.aws.secret-key | AWS secret key. | -
|====
The prefix `spring.ai.bedrock.anthropic3.chat` is the property prefix that configures the chat client implementation for Claude.
[cols="2,5,1"]
|====
| Property | Description | Default
| spring.ai.bedrock.anthropic3.chat.enable | Enable Bedrock Anthropic chat client. Disabled by default | false
| spring.ai.bedrock.anthropic3.chat.model | The model id to use. Supports the `anthropic.claude-3-sonnet-20240229-v1:0`,`anthropic.claude-3-haiku-20240307-v1:0` and the legacy `anthropic.claude-v2`, `anthropic.claude-v2:1` and `anthropic.claude-instant-v1` models for both synchronous and streaming responses. | `anthropic.claude-3-sonnet-20240229-v1:0`
| spring.ai.bedrock.anthropic3.chat.options.temperature | Controls the randomness of the output. Values can range over [0.0,1.0] | 0.8
| spring.ai.bedrock.anthropic3.chat.options.top-p | The maximum cumulative probability of tokens to consider when sampling. | AWS Bedrock default
| spring.ai.bedrock.anthropic3.chat.options.top-k | Specify the number of token choices the generative uses to generate the next token. | AWS Bedrock default
| spring.ai.bedrock.anthropic3.chat.options.stop-sequences | Configure up to four sequences that the generative recognizes. After a stop sequence, the generative stops generating further tokens. The returned text doesn't contain the stop sequence. | 10
| spring.ai.bedrock.anthropic3.chat.options.anthropic-version | The version of the generative to use. | bedrock-2023-05-31
| spring.ai.bedrock.anthropic3.chat.options.max-tokens | Specify the maximum number of tokens to use in the generated response. Note that the models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate. We recommend a limit of 4,000 tokens for optimal performance. | 500
|====
Look at the https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/api/Anthropic3ChatBedrockApi.java[AnthropicChatModel] for other model IDs.
Supported values are: `anthropic.claude-instant-v1`, `anthropic.claude-v2` and `anthropic.claude-v2:1`.
Model ID values can also be found in the https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids-arns.html[AWS Bedrock documentation for base model IDs].
TIP: All properties prefixed with `spring.ai.bedrock.anthropic3.chat.options` can be overridden at runtime by adding a request specific <<chat-options>> to the `Prompt` call.
== Runtime Options [[chat-options]]
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/Anthropic3ChatOptions.java[Anthropic3ChatOptions.java] provides model configurations, such as temperature, topK, topP, etc.
On start-up, the default options can be configured with the `BedrockAnthropicChatClient(api, options)` constructor or the `spring.ai.bedrock.anthropic3.chat.options.*` properties.
At run-time you can override the default options by adding new, request specific, options to the `Prompt` call.
For example to override the default temperature for a specific request:
[source,java]
----
ChatResponse response = chatClient.call(
new Prompt(
"Generate the names of 5 famous pirates.",
Anthropic3ChatOptions.builder()
.withTemperature(0.4)
.build()
));
----
TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/Anthropic3ChatOptions.java[AnthropicChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()].
== Multimodal
Multimodality refers to a model's ability to simultaneously understand and process information from various sources, including text, images, audio, and other data formats. This paradigm represents a significant advancement in AI models.
Currently, Anthropic Claude 3 supports the `base64` source type for `images`, and the `image/jpeg`, `image/png`, `image/gif`, and `image/webp` media types.
Check the link:https://docs.anthropic.com/claude/docs/vision[Vision guide] for more information.
Spring AI's `Message` interface supports multimodal AI models by introducing the Media type.
This type contains data and information about media attachments in messages, using Spring's `org.springframework.util.MimeType` and a `java.lang.Object` for the raw media data.
Below is a simple code example extracted from https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic3/Anthropic3ChatClientIT.java[Anthropic3ChatClientIT.java], demonstrating the combination of user text with an image.
[source,java]
----
byte[] imageData = new ClassPathResource("/test.png").getContentAsByteArray();
var userMessage = new UserMessage("Explain what do you see o this picture?",
List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData)));
ChatResponse response = chatClient.call(new Prompt(List.of(userMessage)));
assertThat(response.getResult().getOutput().getContent()).contains("bananas", "apple", "basket");
----
It takes as an input the `test.png` image:
image::multimodal.test.png[Multimodal Test Image, 200, 200, align="left"]
along with the text message "Explain what do you see on this picture?", and generates a response something like:
----
The image shows a close-up view of a wire fruit basket containing several pieces of fruit.
The basket appears to be made of thin metal wires formed into a round shape with an elevated handle.
Inside the basket, there are a few yellow bananas and a couple of red apples or possibly tomatoes.
The vibrant colors of the fruit contrast nicely against the metallic tones of the wire basket.
The shallow depth of field in the photograph puts the focus squarely on the fruit in the foreground, while the basket handle extending upwards is slightly blurred, creating a pleasing bokeh effect in the background.
The composition and lighting give the image a clean, minimalist aesthetic that highlights the natural beauty and freshness of the fruit displayed in this elegant wire basket.
----
== Sample Controller
https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-bedrock-ai-spring-boot-starter` to your pom (or gradle) dependencies.
Add a `application.properties` file, under the `src/main/resources` directory, to enable and configure the Anthropic Chat client:
[source]
----
spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
spring.ai.bedrock.anthropic3.chat.enabled=true
spring.ai.bedrock.anthropic3.chat.options.temperature=0.8
spring.ai.bedrock.anthropic3.chat.options.top-k=15
----
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
This will create a `BedrockAnthropicChatClient` implementation that you can inject into your class.
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
[source,java]
----
@RestController
public class ChatController {
private final BedrockAnthropic3ChatClient chatClient;
@Autowired
public ChatController(BedrockAnthropic3ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai/generate")
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", chatClient.call(message));
}
@GetMapping("/ai/generateStream")
public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return chatClient.stream(prompt);
}
}
----
== Manual Configuration
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/BedrockAnthropic3ChatClient.java[BedrockAnthropic3ChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
[source,xml]
----
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
----
or to your Gradle `build.gradle` build file.
[source,gradle]
----
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
----
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
Next, create an https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/BedrockAnthropic3ChatClient.java[BedrockAnthropic3ChatClient] and use it for text generations:
[source,java]
----
Anthropic3ChatBedrockApi anthropicApi = new Anthropic3ChatBedrockApi(
AnthropicChatBedrockApi.AnthropicModel.CLAUDE_V3_SONNET.id(),
EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(),
new ObjectMapper());
BedrockAnthropic3ChatClient chatClient = new BedrockAnthropic3ChatClient(anthropicApi,
AnthropicChatOptions.builder()
.withTemperature(0.6f)
.withTopK(10)
.withTopP(0.8f)
.withMaxTokensToSample(100)
.withAnthropicVersion(AnthropicChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
.build());
ChatResponse response = chatClient.call(
new Prompt("Generate the names of 5 famous pirates."));
// Or with streaming responses
Flux<ChatResponse> response = chatClient.stream(
new Prompt("Generate the names of 5 famous pirates."));
----
=== Low-level Anthropic3ChatBedrockApi Client [[low-level-api]]
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/api/Anthropic3ChatBedrockApi.java[Anthropic3ChatBedrockApi] provides is lightweight Java client on top of AWS Bedrock link:https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html[Anthropic Claude models].
Client supports the `anthropic.claude-3-sonnet-20240229-v1:0`,`anthropic.claude-3-haiku-20240307-v1:0` and the legacy `anthropic.claude-v2`, `anthropic.claude-v2:1` and `anthropic.claude-instant-v1` models for both synchronous (e.g. `chatCompletion()`) and streaming (e.g. `chatCompletionStream()`) responses.
Here is a simple snippet how to use the api programmatically:
[source,java]
----
Anthropic3ChatBedrockApi anthropicChatApi = new Anthropic3ChatBedrockApi(
AnthropicModel.CLAUDE_V2.id(), Region.EU_CENTRAL_1.id());
AnthropicChatRequest request = AnthropicChatRequest
.builder(String.format(Anthropic3ChatBedrockApi.PROMPT_TEMPLATE, "Name 3 famous pirates"))
.withTemperature(0.8f)
.withMaxTokensToSample(300)
.withTopK(10)
.build();
// Sync request
AnthropicChatResponse response = anthropicChatApi.chatCompletion(request);
// Streaming request
Flux<AnthropicChatResponse> responseStream = anthropicChatApi.chatCompletionStream(request);
List<AnthropicChatResponse> responses = responseStream.collectList().block();
----
Follow the https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/api/Anthropic3ChatBedrockApi.java[Anthropic3ChatBedrockApi.java]'s JavaDoc for further information.

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2023 - 2024 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.autoconfigure.bedrock.anthropic3;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.autoconfigure.bedrock.BedrockAwsConnectionConfiguration;
import org.springframework.ai.autoconfigure.bedrock.BedrockAwsConnectionProperties;
import org.springframework.ai.bedrock.anthropic3.BedrockAnthropic3ChatClient;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
/**
* {@link AutoConfiguration Auto-configuration} for Bedrock Anthropic Chat Client.
*
* Leverages the Spring Cloud AWS to resolve the {@link AwsCredentialsProvider}.
*
* @author Christian Tzolov
* @since 0.8.0
*/
@AutoConfiguration
@ConditionalOnClass(Anthropic3ChatBedrockApi.class)
@EnableConfigurationProperties({ BedrockAnthropic3ChatProperties.class, BedrockAwsConnectionProperties.class })
@ConditionalOnProperty(prefix = BedrockAnthropic3ChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true")
@Import(BedrockAwsConnectionConfiguration.class)
public class BedrockAnthropic3ChatAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Anthropic3ChatBedrockApi anthropicApi(AwsCredentialsProvider credentialsProvider,
BedrockAnthropic3ChatProperties properties, BedrockAwsConnectionProperties awsProperties) {
return new Anthropic3ChatBedrockApi(properties.getModel(), credentialsProvider, awsProperties.getRegion(),
new ObjectMapper());
}
@Bean
public BedrockAnthropic3ChatClient anthropicChatClient(Anthropic3ChatBedrockApi anthropicApi,
BedrockAnthropic3ChatProperties properties) {
return new BedrockAnthropic3ChatClient(anthropicApi, properties.getOptions());
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2023 - 2024 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.autoconfigure.bedrock.anthropic3;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi;
import org.springframework.ai.bedrock.anthropic3.Anthropic3ChatOptions;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatModel;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.util.Assert;
/**
* Configuration properties for Bedrock Anthropic Claude 3.
*
* @author Christian Tzolov
* @since 1.0.0
*/
@ConfigurationProperties(BedrockAnthropic3ChatProperties.CONFIG_PREFIX)
public class BedrockAnthropic3ChatProperties {
public static final String CONFIG_PREFIX = "spring.ai.bedrock.anthropic3.chat";
/**
* Enable Bedrock Anthropic chat client. Disabled by default.
*/
private boolean enabled = false;
/**
* The generative id to use. See the {@link AnthropicChatModel} for the supported
* models.
*/
private String model = AnthropicChatModel.CLAUDE_V3_SONNET.id();
@NestedConfigurationProperty
private Anthropic3ChatOptions options = Anthropic3ChatOptions.builder()
.withTemperature(0.7f)
.withMaxTokens(300)
.withTopK(10)
.withAnthropicVersion(Anthropic3ChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
// .withStopSequences(List.of("\n\nHuman:"))
.build();
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getModel() {
return this.model;
}
public void setModel(String model) {
this.model = model;
}
public Anthropic3ChatOptions getOptions() {
return options;
}
public void setOptions(Anthropic3ChatOptions options) {
Assert.notNull(options, "AnthropicChatOptions must not be null");
Assert.notNull(options.getTemperature(), "AnthropicChatOptions.temperature must not be null");
this.options = options;
}
}

View File

@@ -10,6 +10,7 @@ org.springframework.ai.autoconfigure.bedrock.llama2.BedrockLlama2ChatAutoConfigu
org.springframework.ai.autoconfigure.bedrock.cohere.BedrockCohereChatAutoConfiguration
org.springframework.ai.autoconfigure.bedrock.cohere.BedrockCohereEmbeddingAutoConfiguration
org.springframework.ai.autoconfigure.bedrock.anthropic.BedrockAnthropicChatAutoConfiguration
org.springframework.ai.autoconfigure.bedrock.anthropic3.BedrockAnthropic3ChatAutoConfiguration
org.springframework.ai.autoconfigure.bedrock.titan.BedrockTitanChatAutoConfiguration
org.springframework.ai.autoconfigure.bedrock.titan.BedrockTitanEmbeddingAutoConfiguration
org.springframework.ai.autoconfigure.ollama.OllamaAutoConfiguration

View File

@@ -0,0 +1,153 @@
/*
* Copyright 2023 - 2024 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.autoconfigure.bedrock.anthropic3;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.chat.messages.AssistantMessage;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.regions.Region;
import org.springframework.ai.autoconfigure.bedrock.BedrockAwsConnectionProperties;
import org.springframework.ai.bedrock.anthropic3.BedrockAnthropic3ChatClient;
import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi.AnthropicChatModel;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Christian Tzolov
* @since 1.0.0
*/
@EnabledIfEnvironmentVariable(named = "AWS_ACCESS_KEY_ID", matches = ".*")
@EnabledIfEnvironmentVariable(named = "AWS_SECRET_ACCESS_KEY", matches = ".*")
public class BedrockAnthropic3ChatAutoConfigurationIT {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.ai.bedrock.anthropic3.chat.enabled=true",
"spring.ai.bedrock.aws.access-key=" + System.getenv("AWS_ACCESS_KEY_ID"),
"spring.ai.bedrock.aws.secret-key=" + System.getenv("AWS_SECRET_ACCESS_KEY"),
"spring.ai.bedrock.aws.region=" + Region.US_EAST_1.id(),
"spring.ai.bedrock.anthropic3.chat.model=" + AnthropicChatModel.CLAUDE_V3_SONNET.id(),
"spring.ai.bedrock.anthropic3.chat.options.temperature=0.5")
.withConfiguration(AutoConfigurations.of(BedrockAnthropic3ChatAutoConfiguration.class));
private final Message systemMessage = new SystemPromptTemplate("""
You are a helpful AI assistant. Your name is {name}.
You are an AI assistant that helps people find information.
Your name is {name}
You should reply to the user's request with your name and also in the style of a {voice}.
""").createMessage(Map.of("name", "Bob", "voice", "pirate"));
private final UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
@Test
public void chatCompletion() {
contextRunner.run(context -> {
BedrockAnthropic3ChatClient anthropicChatClient = context.getBean(BedrockAnthropic3ChatClient.class);
ChatResponse response = anthropicChatClient.call(new Prompt(List.of(userMessage, systemMessage)));
assertThat(response.getResult().getOutput().getContent()).contains("Blackbeard");
});
}
@Test
public void chatCompletionStreaming() {
contextRunner.run(context -> {
BedrockAnthropic3ChatClient anthropicChatClient = context.getBean(BedrockAnthropic3ChatClient.class);
Flux<ChatResponse> response = anthropicChatClient.stream(new Prompt(List.of(userMessage, systemMessage)));
List<ChatResponse> responses = response.collectList().block();
assertThat(responses.size()).isGreaterThan(2);
String stitchedResponseContent = responses.stream()
.map(ChatResponse::getResults)
.flatMap(List::stream)
.map(Generation::getOutput)
.map(AssistantMessage::getContent)
.collect(Collectors.joining());
assertThat(stitchedResponseContent).contains("Blackbeard");
});
}
@Test
public void propertiesTest() {
new ApplicationContextRunner()
.withPropertyValues("spring.ai.bedrock.anthropic3.chat.enabled=true",
"spring.ai.bedrock.aws.access-key=ACCESS_KEY", "spring.ai.bedrock.aws.secret-key=SECRET_KEY",
"spring.ai.bedrock.anthropic3.chat.model=MODEL_XYZ",
"spring.ai.bedrock.aws.region=" + Region.EU_CENTRAL_1.id(),
"spring.ai.bedrock.anthropic3.chat.options.temperature=0.55")
.withConfiguration(AutoConfigurations.of(BedrockAnthropic3ChatAutoConfiguration.class))
.run(context -> {
var anthropicChatProperties = context.getBean(BedrockAnthropic3ChatProperties.class);
var awsProperties = context.getBean(BedrockAwsConnectionProperties.class);
assertThat(anthropicChatProperties.isEnabled()).isTrue();
assertThat(awsProperties.getRegion()).isEqualTo(Region.EU_CENTRAL_1.id());
assertThat(anthropicChatProperties.getOptions().getTemperature()).isEqualTo(0.55f);
assertThat(anthropicChatProperties.getModel()).isEqualTo("MODEL_XYZ");
assertThat(awsProperties.getAccessKey()).isEqualTo("ACCESS_KEY");
assertThat(awsProperties.getSecretKey()).isEqualTo("SECRET_KEY");
});
}
@Test
public void chatCompletionDisabled() {
// It is disabled by default
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(BedrockAnthropic3ChatAutoConfiguration.class))
.run(context -> {
assertThat(context.getBeansOfType(BedrockAnthropic3ChatProperties.class)).isEmpty();
assertThat(context.getBeansOfType(BedrockAnthropic3ChatClient.class)).isEmpty();
});
// Explicitly enable the chat auto-configuration.
new ApplicationContextRunner().withPropertyValues("spring.ai.bedrock.anthropic3.chat.enabled=true")
.withConfiguration(AutoConfigurations.of(BedrockAnthropic3ChatAutoConfiguration.class))
.run(context -> {
assertThat(context.getBeansOfType(BedrockAnthropic3ChatProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(BedrockAnthropic3ChatClient.class)).isNotEmpty();
});
// Explicitly disable the chat auto-configuration.
new ApplicationContextRunner().withPropertyValues("spring.ai.bedrock.anthropic3.chat.enabled=false")
.withConfiguration(AutoConfigurations.of(BedrockAnthropic3ChatAutoConfiguration.class))
.run(context -> {
assertThat(context.getBeansOfType(BedrockAnthropic3ChatProperties.class)).isEmpty();
assertThat(context.getBeansOfType(BedrockAnthropic3ChatClient.class)).isEmpty();
});
}
}