基岩 Anthropic 3
Anthropic Claude 是一系列基础 AI 模型,可用于各种应用程序。
Claude 模型具有以下高级功能
-
200k 代币上下文窗口:Claude 拥有 200,000 的慷慨代币容量,非常适合处理技术文档、代码库和文学作品等应用程序中的大量信息。
-
支持的任务: Claude 的多功能性涵盖摘要、问答、趋势预测和文档比较等任务,支持从对话到内容生成的广泛应用。
-
AI 安全功能: 基于 Anthropic 的安全研究,Claude 在其互动中优先考虑有用、诚实和无害,从而降低品牌风险并确保负责任的 AI 行为。
AWS Bedrock Anthropic 模型页面和 Amazon Bedrock 用户指南包含有关如何使用 AWS 托管模型的详细信息。
Anthropic 的 Claude 2 和 3 模型也可以直接在 Anthropic 自己的云平台上使用。 Spring AI 提供了专用的 Anthropic Claude 客户端来访问它。 |
先决条件
请参阅 Amazon Bedrock 上的 Spring AI 文档以设置 API 访问。
添加存储库和 BOM
Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。请参阅 Repositories 部分,将这些存储库添加到您的构建系统中。
为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
添加spring-ai-bedrock-ai-spring-boot-starter
依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
启用 Anthropic Chat
默认情况下,Anthropic 模型处于禁用状态。
要启用它,请将spring.ai.bedrock.anthropic3.chat.enabled
property 设置为true
.
导出环境变量是设置此配置属性的一种方法:
export SPRING_AI_BEDROCK_ANTHROPIC3_CHAT_ENABLED=true
聊天属性
前缀spring.ai.bedrock.aws
是用于配置与 AWS Bedrock 的连接的属性前缀。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.bedrock.aws.region |
要使用的 AWS 区域。 |
us-east-1 (美国东部-1) |
spring.ai.bedrock.aws.timeout |
要使用的 AWS 超时。 |
5 分钟 |
spring.ai.bedrock.aws.access-key |
AWS 访问密钥。 |
- |
spring.ai.bedrock.aws.secret-key |
AWS 密钥。 |
- |
前缀spring.ai.bedrock.anthropic3.chat
是为 Claude 配置聊天模型实现的属性前缀。
财产 | 描述 | 违约 |
---|---|---|
spring.ai.bedrock.anthropic3.chat.enabled |
启用 Bedrock Anthropic 聊天模型。默认禁用 |
假 |
spring.ai.bedrock.anthropic3.chat.model |
要使用的模型 ID。支持 |
|
spring.ai.bedrock.anthropic3.chat.options.temperature |
控制输出的随机性。值的范围可以超过 [0.0,1.0] |
0.8 |
spring.ai.bedrock.anthropic3.chat.options.top-p |
采样时要考虑的 token 的最大累积概率。 |
AWS Bedrock 默认 |
spring.ai.bedrock.anthropic3.chat.options.top-k |
指定生成式用于生成下一个令牌的令牌选择数。 |
AWS Bedrock 默认 |
spring.ai.bedrock.anthropic3.chat.options.stop-sequences |
配置生成式识别的最多四个序列。在停止序列之后,生成停止生成更多标记。返回的文本不包含停止序列。 |
10 |
spring.ai.bedrock.anthropic3.chat.options.anthropic-version |
要使用的生成版本。 |
基岩-2023-05-31 |
spring.ai.bedrock.anthropic3.chat.options.max 代币 |
指定要在生成的响应中使用的最大令牌数。请注意,模型可能会在达到此最大值之前停止。此参数仅指定要生成的 Token 的绝对最大数量。我们建议将令牌数限制为 4,000 个,以获得最佳性能。 |
500 |
查看 AnthropicChatModel 以获取其他模型 ID。
支持的值为:anthropic.claude-instant-v1
,anthropic.claude-v2
和anthropic.claude-v2:1
.
模型 ID 值也可以在 AWS Bedrock 文档中找到基本模型 ID。
所有前缀为spring.ai.bedrock.anthropic3.chat.options 可以通过将特定于请求的运行时选项添加到Prompt 叫。 |
运行时选项
Anthropic3ChatOptions.java 提供模型配置,如 temperature、topK、topP 等。
启动时,可以使用BedrockAnthropicChatModel(api, options)
constructor 或spring.ai.bedrock.anthropic3.chat.options.*
性能。
在运行时,您可以通过向Prompt
叫。
例如,要覆盖特定请求的默认温度:
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
Anthropic3ChatOptions.builder()
.withTemperature(0.4)
.build()
));
除了特定于模型的 AnthropicChatOptions 之外,您还可以使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。 |
模 态
多模态是指模型同时理解和处理来自各种来源的信息(包括文本、图像、音频和其他数据格式)的能力。这种范式代表了 AI 模型的重大进步。
目前,Anthropic Claude 3 支持base64
的源类型images
和image/jpeg
,image/png
,image/gif
和image/webp
media 类型。
有关更多信息,请查看 Vision 指南。
Spring AI 的Message
界面通过引入 Media 类型来支持多模态 AI 模型。
此类型包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType
以及java.lang.Object
对于原始媒体数据。
下面是从 Anthropic3ChatModelIT.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, this.imageData)));
ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));
assertThat(response.getResult().getOutput().getContent()).contains("bananas", "apple", "basket");
它将test.png
图像:

以及文本消息“Explain what do you see on this picture?”,并生成如下响应:
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.
Samples控制器
创建一个新的 Spring Boot 项目并添加spring-ai-bedrock-ai-spring-boot-starter
添加到您的 POM(或 Gradle)依赖项中。
添加application.properties
文件中的src/main/resources
目录中,以启用和配置 Anthropic 聊天模型:
spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.timeout=1000ms
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
将regions ,access-key 和secret-key 替换为您的 AWS 凭证。 |
这将创建一个BedrockAnthropicChatModel
实现,您可以将其注入到您的类中。
下面是一个简单的示例@Controller
使用 Chat 模型生成文本的类。
@RestController
public class ChatController {
private final BedrockAnthropic3ChatModel chatModel;
@Autowired
public ChatController(BedrockAnthropic3ChatModel chatModel) {
this.chatModel = chatModel;
}
@GetMapping("/ai/generate")
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", this.chatModel.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 this.chatModel.stream(prompt);
}
}
手动配置
BedrockAnthropic3ChatModel 实现了ChatModel
和StreamingChatModel
并使用 Low-level Anthropic3ChatBedrockApi Client 连接到 Bedrock Anthropic 服务。
添加spring-ai-bedrock
依赖项添加到项目的 Mavenpom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
接下来,创建一个 BedrockAnthropic3ChatModel 并将其用于文本生成:
Anthropic3ChatBedrockApi anthropicApi = new Anthropic3ChatBedrockApi(
AnthropicChatBedrockApi.AnthropicModel.CLAUDE_V3_SONNET.id(),
EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(),
new ObjectMapper(),
Duration.ofMillis(1000L));
BedrockAnthropic3ChatModel chatModel = new BedrockAnthropic3ChatModel(this.anthropicApi,
AnthropicChatOptions.builder()
.withTemperature(0.6)
.withTopK(10)
.withTopP(0.8)
.withMaxTokensToSample(100)
.withAnthropicVersion(AnthropicChatBedrockApi.DEFAULT_ANTHROPIC_VERSION)
.build());
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
// Or with streaming responses
Flux<ChatResponse> response = this.chatModel.stream(
new Prompt("Generate the names of 5 famous pirates."));
低级 Anthropic3ChatBedrockApi 客户端
Anthropic3ChatBedrockApi 提供的是基于 AWS Bedrock Anthropic Claude 模型的轻量级 Java 客户端。
客户端支持anthropic.claude-3-opus-20240229-v1:0
,anthropic.claude-3-sonnet-20240229-v1:0
,anthropic.claude-3-haiku-20240307-v1:0
和遗产anthropic.claude-v2
,anthropic.claude-v2:1
和anthropic.claude-instant-v1
同步模型(例如chatCompletion()
) 和流式处理(例如chatCompletionStream()
) 响应。
以下是如何以编程方式使用 api 的简单代码段:
Anthropic3ChatBedrockApi anthropicChatApi = new Anthropic3ChatBedrockApi(
AnthropicModel.CLAUDE_V2.id(), Region.US_EAST_1.id(), Duration.ofMillis(1000L));
AnthropicChatRequest request = AnthropicChatRequest
.builder(String.format(Anthropic3ChatBedrockApi.PROMPT_TEMPLATE, "Name 3 famous pirates"))
.withTemperature(0.8)
.withMaxTokensToSample(300)
.withTopK(10)
.build();
// Sync request
AnthropicChatResponse response = this.anthropicChatApi.chatCompletion(this.request);
// Streaming request
Flux<AnthropicChatResponse> responseStream = this.anthropicChatApi.chatCompletionStream(this.request);
List<AnthropicChatResponse> responses = this.responseStream.collectList().block();
有关详细信息,请遵循 Anthropic3ChatBedrockApi.java 的 JavaDoc。