Amazon Bedrock
Maven 依赖
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-bedrock</artifactId>
<version>1.18.1</version>
</dependency>
AWS 凭证
要使用 Amazon Bedrock 模型,需要配置 AWS 凭证。
其中一种方式是设置 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 环境变量。更多信息见此处。也可以在本地设置 AWS_BEARER_TOKEN_BEDROCK 环境变量,以使用 API Key 进行身份验证。有关 API 密钥的更多细节,请参阅文档。
BedrockChatModel
备注
当前实现不支持 Guardrails(防护栏)。
支持的模型及其功能见此处。
模型 ID 见此处。
配置
ChatModel model = BedrockChatModel.builder()
.client(BedrockRuntimeClient)
.region(...)
.modelId("us.amazon.nova-lite-v1:0")
.returnThinking(...)
.sendThinking(...)
.timeout(...)
.maxRetries(...)
.logRequests(...)
.logResponses(...)
.listeners(...)
.defaultRequestParameters(BedrockChatRequestParameters.builder()
.modelName(...)
.temperature(...)
.topP(...)
.maxOutputTokens(...)
.stopSequences(...)
.toolSpecifications(...)
.toolChoice(...)
.additionalModelRequestFields(...)
.additionalModelRequestField(...)
.enableReasoning(...)
.promptCaching(...)
.build())
.build();
示例
BedrockStreamingChatModel
备注
当前实现不支持 Guardrails(防护栏)。
支持的模型及其功能见此处。
模型 ID 见此处。
配置
StreamingChatModel model = BedrockStreamingChatModel.builder()
.client(BedrockRuntimeAsyncClient)
.region(...)
.modelId("us.amazon.nova-lite-v1:0")
.returnThinking(...)
.sendThinking(...)
.timeout(...)
.logRequests(...)
.logResponses(...)
.listeners(...)
.defaultRequestParameters(BedrockChatRequestParameters.builder()
.modelName(...)
.temperature(...)
.topP(...)
.maxOutputTokens(...)
.stopSequences(...)
.toolSpecifications(...)
.toolChoice(...)
.additionalModelRequestFields(...)
.additionalModelRequestField(...)
.enableReasoning(...)
.promptCaching(...)
.build())
.build();