| 此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT! | 
基岩版 Converse API
Amazon Bedrock Converse API 为对话式 AI 模型提供了一个统一的接口,具有增强功能,包括函数/工具调用、多模态输入和流式响应。
Bedrock Converse API 具有以下高级功能:
- 
工具/函数调用:支持在对话期间使用函数定义和工具 
- 
多模态输入:能够在对话中处理文本和图像输入 
- 
流式处理支持:模型响应的实时流式处理 
- 
系统消息:支持系统级指令和上下文设置 
| Bedrock Converse API 提供跨多个模型提供商的统一接口,同时处理特定于 AWS 的身份验证和基础设施问题。
目前,Converse API 支持的模型包括: Amazon Titan,Amazon Nova,AI21 Labs,Anthropic Claude,Cohere Command,Meta Llama,Mistral AI. | 
| 根据 Bedrock 的建议,Spring AI 正在过渡到使用 Amazon Bedrock 的 Converse API 来实现 Spring AI 中的所有聊天对话。 虽然现有的 InvokeModel API 支持对话应用程序,但我们强烈建议对所有 Char 对话模型采用 Converse API。 Converse API 不支持嵌入作,因此这些作将保留在当前 API 中,而嵌入模型功能将保留在现有的 | 
先决条件
请参阅 Amazon Bedrock 入门以设置 API 访问
- 
获取AWS凭证:如果您还没有配置AWS账户和AWS CLI,此视频指南可以帮助您配置它:AWS CLI和SDK设置在不到4分钟的时间内,只需不到4分钟)。您应该能够获取您的访问密钥和安全密钥。 
- 
启用要使用的模型:转到 Amazon Bedrock,然后从左侧的 Model Access(模型访问)菜单中,配置对要使用的模型的访问。 
自动配置
添加spring-ai-bedrock-converse-spring-boot-starter依赖项添加到项目的 Mavenpom.xml或 Gradlebuild.gradlebuild 文件:
- 
Maven 
- 
Gradle 
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock-converse-spring-boot-starter</artifactId>
</dependency>dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock-converse-spring-boot-starter'
}| 请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 | 
聊天属性
前缀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.aws.session-token | 用于临时凭证的 AWS 会话令牌。 | - | 
前缀spring.ai.bedrock.converse.chat是为 Converse API 配置聊天模型实现的属性前缀。
| 财产 | 描述 | 违约 | 
|---|---|---|
| spring.ai.bedrock.converse.chat.enabled | 启用 Bedrock Converse 聊天模型。 | 真 | 
| spring.ai.bedrock.converse.chat.options.model | 要使用的模型 ID。您可以使用 Supported models (支持的模型) 和 model (模型) 功能 | 没有。从 AWS Bedrock 控制台中选择您的 modelId。 | 
| spring.ai.bedrock.converse.chat.options.temperature | 控制输出的随机性。值的范围可以超过 [0.0,1.0] | 0.8 | 
| spring.ai.bedrock.converse.chat.options.top-p | 采样时要考虑的 token 的最大累积概率。 | AWS Bedrock 默认 | 
| spring.ai.bedrock.converse.chat.options.top-k | 用于生成下一个令牌的令牌选项数。 | AWS Bedrock 默认 | 
| spring.ai.bedrock.converse.chat.options.max 代币 | 生成的响应中的最大令牌数。 | 500 | 
运行时选项
使用便携式ChatOptions或ToolCallingChatOptions用于创建模型配置的便携式构建器,例如 temperature、maxToken、topP 等。
启动时,可以使用BedrockConverseProxyChatModel(api, options)constructor 或spring.ai.bedrock.converse.chat.options.*性能。
在运行时,您可以通过向Prompt叫:
var options = ToolCallingChatOptions.builder()
        .model("anthropic.claude-3-5-sonnet-20240620-v1:0")
        .temperature(0.6)
        .maxTokens(300)
        .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new WeatherService())
            .description("Get the weather in location. Return temperature in 36°F or 36°C format. Use multi-turn if needed.")
            .inputType(WeatherService.Request.class)
            .build()))
        .build();
String response = ChatClient.create(this.chatModel)
    .prompt("What is current weather in Amsterdam?")
    .options()
    .call(options)
    .content();工具调用
Bedrock Converse API 支持工具调用功能,允许模型在对话期间使用工具。 以下是如何定义和使用基于 @Tool 的工具的示例:
public class WeatherService {
    @Tool(description = "Get the weather in location")
    public String weatherByLocation(@ToolParam(description= "City or state name") String location) {
        ...
    }
}
String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .tools(new WeatherService())
        .call()
        .content();您也可以将 java.util.function bean 用作工具:
@Bean
@Description("Get the weather in location. Return temperature in 36°F or 36°C format.")
public Function<Request, Response> weatherFunction() {
    return new MockWeatherService();
}
String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .tools("weatherFunction")
        .inputType(Request.class)
        .call()
        .content();在工具文档中查找更多信息。
模 态
多模态是指模型同时理解和处理来自各种来源的信息的能力,包括文本、图像、视频、pdf、doc、html、md 和更多数据格式。
Bedrock Converse API 支持多模态输入,包括文本和图像输入,并且可以根据组合输入生成文本响应。
您需要一个支持多模态输入的模型,例如 Anthropic Claude 或 Amazon Nova 模型。
图像
对于支持视觉多模态的模型,例如 Amazon Nova、Anthropic Claude、Llama 3.2,Amazon 的 Bedrock Converse API 允许您在负载中包含多个图像。这些模型可以分析传递的图像并回答问题、对图像进行分类,以及根据提供的说明对图像进行汇总。
目前,Bedrock Converse 支持base64的编码图像image/jpeg,image/png,image/gif和image/webpMIME 类型。
Spring AI 的Message界面通过引入Media类型。
它包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType以及java.lang.Object对于原始媒体数据。
下面是一个简单的代码示例,演示了用户文本与图像的组合。
String response = ChatClient.create(chatModel)
    .prompt()
    .user(u -> u.text("Explain what do you see on this picture?")
        .media(Media.Format.IMAGE_PNG, new ClassPathResource("/test.png")))
    .call()
    .content();
logger.info(response);它将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. ...
视频
Amazon Nova 模型允许您在负载中包含单个视频,该视频可以采用 base64 格式或通过 Amazon S3 URI 提供。
目前,Bedrock Nova 支持video/x-matros,video/quicktime,video/mp4,video/video/webm,video/x-flv,video/mpeg,video/x-ms-wmv和image/3gppMIME 类型。
Spring AI 的Message界面通过引入Media`类型。
它包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType以及java.lang.Object对于原始媒体数据。
下面是一个简单的代码示例,演示了用户文本与视频的组合。
String response = ChatClient.create(chatModel)
    .prompt()
    .user(u -> u.text("Explain what do you see in this video?")
        .media(Media.Format.VIDEO_MP4, new ClassPathResource("/test.video.mp4")))
    .call()
    .content();
logger.info(response);它将test.video.mp4图像:
 
以及文本消息“Explain what do you see in this video?”,并生成如下响应:
The video shows a group of baby chickens, also known as chicks, huddled together on a surface ...
文件
对于某些模型,Bedrock 允许您通过 Converse API 文档支持将文档包含在有效负载中,该支持可以以字节为单位提供。 文档支持有两种不同的变体,如下所述:
- 
文本文档类型(txt、csv、html、md 等),其中重点是文本理解。这些用例包括根据文档的文本元素进行回答。 
- 
媒体文档类型(pdf、docx、xlsx),其中重点是基于视觉的理解来回答问题。这些使用案例包括根据图表、图形等回答问题。 
目前,Anthropic PDF 支持(测试版)和 Amazon Bedrock Nova 模型支持文档多模态。
下面是一个简单的代码示例,演示了用户文本与媒体文档的组合。
String response = ChatClient.create(chatModel)
    .prompt()
    .user(u -> u.text(
            "You are a very professional document summarization specialist. Please summarize the given document.")
        .media(Media.Format.DOC_PDF, new ClassPathResource("/spring-ai-reference-overview.pdf")))
    .call()
    .content();
logger.info(response);它将spring-ai-reference-overview.pdf公文:
 
伴随着短信“您是一位非常专业的文档摘要专家。请总结给定的文档“,并生成如下响应:
**Introduction:** - Spring AI is designed to simplify the development of applications with artificial intelligence (AI) capabilities, aiming to avoid unnecessary complexity. ...
Samples控制器
创建一个新的 Spring Boot 项目并添加spring-ai-bedrock-converse-spring-boot-starter添加到您的依赖项中。
添加application.properties文件src/main/resources:
spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.timeout=10m
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
# session token is only required for temporary credentials
spring.ai.bedrock.aws.session-token=${AWS_SESSION_TOKEN}
spring.ai.bedrock.converse.chat.options.temperature=0.8
spring.ai.bedrock.converse.chat.options.top-k=15下面是一个使用 chat 模型的示例控制器:
@RestController
public class ChatController {
    private final ChatClient chatClient;
    @Autowired
    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }
    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatClient.prompt(message).call().content());
    }
    @GetMapping("/ai/generateStream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return this.chatClient.prompt(message).stream().content();
    }
}