Bedrock Anthropic 3
Anthropic Claude 是一系列基礎 AI 模型,可用於各種應用程式。
Claude 模型具有以下高階功能
-
20 萬 Token 上下文視窗:Claude 擁有高達 20 萬的 token 容量,使其非常適合處理大量資訊,例如技術文件、程式碼庫和文學作品。
-
支援任務:Claude 的多功能性涵蓋摘要、問答、趨勢預測和文件比較等任務,實現從對話到內容生成的廣泛應用。
-
AI 安全功能:Claude 建基於 Anthropic 的安全研究,在其互動中優先考慮有幫助、誠實和無害,降低品牌風險並確保負責任的 AI 行為。
關於如何使用 AWS 託管模型的詳細資訊,請參閱 AWS Bedrock Anthropic 模型頁面 和 Amazon Bedrock 使用者指南。
Anthropic 的 Claude 2 和 3 模型也可直接在 Anthropic 自己的雲端平台上使用。Spring AI 提供了專用的 Anthropic Claude 用戶端來存取它。 |
先決條件
關於設定 API 存取,請參閱 Spring AI 關於 Amazon Bedrock 的文件。
自動組態
將 spring-ai-bedrock-ai-spring-boot-starter
依賴項新增到您的專案的 Maven pom.xml
檔案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
</dependency>
或您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
關於將 Spring AI BOM 新增到您的建置檔案,請參閱 依賴性管理 章節。 |
啟用 Anthropic 聊天
預設情況下,Anthropic 模型已停用。若要啟用它,請將 spring.ai.bedrock.anthropic3.chat.enabled
屬性設定為 true
。匯出環境變數是設定此組態屬性的一種方式
export SPRING_AI_BEDROCK_ANTHROPIC3_CHAT_ENABLED=true
聊天屬性
字首 spring.ai.bedrock.aws
是用於組態與 AWS Bedrock 連線的屬性字首。
屬性 | 描述 | 預設值 |
---|---|---|
spring.ai.bedrock.aws.region |
要使用的 AWS 區域。 |
us-east-1 |
spring.ai.bedrock.aws.timeout |
要使用的 AWS 超時。 |
5m |
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 聊天模型。預設停用 |
false |
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 |
指定生成器用於生成下一個 token 的 token 選項數量。 |
AWS Bedrock 預設值 |
spring.ai.bedrock.anthropic3.chat.options.stop-sequences |
組態最多四個生成器可辨識的序列。在停止序列之後,生成器會停止生成進一步的 token。傳回的文字不包含停止序列。 |
10 |
spring.ai.bedrock.anthropic3.chat.options.anthropic-version |
要使用的生成器版本。 |
bedrock-2023-05-31 |
spring.ai.bedrock.anthropic3.chat.options.max-tokens |
指定在產生的回應中使用的 token 最大數量。請注意,模型可能會在達到此最大值之前停止。此參數僅指定要生成的 token 絕對最大數量。我們建議限制為 4,000 個 token 以獲得最佳效能。 |
500 |
關於其他模型 ID,請查看 AnthropicChatModel。支援的值為: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)
建構子或 spring.ai.bedrock.anthropic3.chat.options.*
屬性來組態預設選項。
在執行時,您可以透過將新的、請求特定的選項新增到 Prompt
呼叫來覆寫預設選項。例如,覆寫特定請求的預設 temperature
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
Anthropic3ChatOptions.builder()
.withTemperature(0.4)
.build()
));
除了模型特定的 AnthropicChatOptions 之外,您還可以使用可攜式的 ChatOptions 實例,使用 ChatOptionsBuilder#builder() 建立。 |
多模態
多模態是指模型同時理解和處理來自各種來源的資訊的能力,包括文字、影像、音訊和其他資料格式。這種範例代表了 AI 模型的一項重大進展。
目前,Anthropic Claude 3 支援 images
的 base64
來源類型,以及 image/jpeg
、image/png
、image/gif
和 image/webp
媒體類型。查看 Vision 指南 以取得更多資訊。
Spring AI 的 Message
介面透過引入媒體類型來支援多模態 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.
範例控制器
建立 新的 Spring Boot 專案,並將 spring-ai-bedrock-ai-spring-boot-starter
新增到您的 pom(或 gradle)依賴項。
在 src/main/resources
目錄下新增 application.properties
檔案,以啟用和組態 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
類別範例,該類別使用聊天模型進行文字生成。
@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
,並使用 低階 Anthropic3ChatBedrockApi 用戶端 連接到 Bedrock Anthropic 服務。
將 spring-ai-bedrock
依賴項新增到您的專案的 Maven pom.xml
檔案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
或您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
關於將 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。