此版本仍在开发中,尚不被认为是稳定的。对于最新的快照版本,请使用 Spring AI 1.0.1spring-doc.cadn.net.cn

基岩匡威 API

Amazon Bedrock Converse API 为对话式 AI 模型提供了一个统一的界面,具有增强的功能,包括函数/工具调用、多模态输入和流式响应。spring-doc.cadn.net.cn

Bedrock Converse API 具有以下高级功能:spring-doc.cadn.net.cn

Bedrock Converse API 提供跨多个模型提供商的统一接口,同时处理特定于 AWS 的身份验证和基础设施问题。 目前,匡威 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 支持对话应用,但我们强烈建议所有聊天对话模型采用 Converse API。spring-doc.cadn.net.cn

Converse API 不支持嵌入作,因此这些作将保留在当前 API 中,而嵌入模型功能将保留在现有InvokeModel API将被维护spring-doc.cadn.net.cn

前提条件

请参阅 Amazon Bedrock 入门以设置 API 访问spring-doc.cadn.net.cn

自动配置

Spring AI 自动配置、入门模块的工件名称发生了重大变化。 有关更多信息,请参阅升级说明spring-doc.cadn.net.cn

添加spring-ai-starter-model-bedrock-converse对项目 Maven 的依赖pom.xml或 Gradlebuild.gradle构建文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-bedrock-converse</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-bedrock-converse'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。

聊天属性

前缀spring.ai.bedrock.aws是配置与 AWS Bedrock 的连接的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.基岩.aws.regionspring-doc.cadn.net.cn

要使用的 AWS 区域。spring-doc.cadn.net.cn

美国东部 1spring-doc.cadn.net.cn

spring.ai.bedrock.aws.timeoutspring-doc.cadn.net.cn

AWS 超时使用。spring-doc.cadn.net.cn

5 分钟spring-doc.cadn.net.cn

spring.ai.bedrock.aws.access-keyspring-doc.cadn.net.cn

AWS 访问密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.基岩.aws.密钥spring-doc.cadn.net.cn

AWS 密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.session-tokenspring-doc.cadn.net.cn

用于临时凭证的 AWS 会话Tokens。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

现在,通过前缀为spring.ai.model.chat.spring-doc.cadn.net.cn

要启用,spring.ai.model.chat=bedrock-converse(默认启用)spring-doc.cadn.net.cn

要禁用,spring.ai.model.chat=none(或任何与 bedrock-converse 不匹配的值)spring-doc.cadn.net.cn

进行此更改是为了允许配置多个模型。spring-doc.cadn.net.cn

前缀spring.ai.bedrock.converse.chat是配置 Converse API 聊天模型实现的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.bedrock.converse.chat.enabled(已删除且不再有效)spring-doc.cadn.net.cn

启用 Bedrock Converse 聊天模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.model.聊天spring-doc.cadn.net.cn

启用 Bedrock Converse 聊天模型。spring-doc.cadn.net.cn

基岩匡威spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.modelspring-doc.cadn.net.cn

要使用的模型 ID。您可以使用支持的模型和模型功能spring-doc.cadn.net.cn

没有。从 AWS 基岩控制台中选择您的 modelIdspring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.temperaturespring-doc.cadn.net.cn

控制输出的随机性。值范围可以超过 [0.0,1.0]spring-doc.cadn.net.cn

0.8spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.top-pspring-doc.cadn.net.cn

采样时要考虑的Tokens的最大累积概率。spring-doc.cadn.net.cn

AWS 基岩默认值spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.top-kspring-doc.cadn.net.cn

用于生成下一个Tokens的Tokens选择数。spring-doc.cadn.net.cn

AWS 基岩默认值spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.maxTokensspring-doc.cadn.net.cn

生成的响应中的最大Tokens数。spring-doc.cadn.net.cn

500spring-doc.cadn.net.cn

运行时选项

使用便携式ChatOptionsBedrockChatOptions可移植构建器来创建模型配置,例如 temperature、maxToken、topP 等。spring-doc.cadn.net.cn

启动时,可以使用BedrockConverseProxyChatModel(api, options)构造函数或spring.ai.bedrock.converse.chat.options.*性能。spring-doc.cadn.net.cn

在运行时,您可以通过向Prompt叫:spring-doc.cadn.net.cn

var options = BedrockChatOptions.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(options)
    .call()
    .content();

工具调用

基岩匡威 API 支持工具调用功能,允许模型在对话中使用工具。 以下是如何定义和使用基于@Tool的工具的示例:spring-doc.cadn.net.cn

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 用作工具:spring-doc.cadn.net.cn

@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?")
        .toolNames("weatherFunction")
        .inputType(Request.class)
        .call()
        .content();

工具文档中查找更多信息。spring-doc.cadn.net.cn

模 态

多模态是指模型同时理解和处理来自各种来源的信息的能力,包括文本、图像、视频、pdf、doc、html、md 和更多数据格式。spring-doc.cadn.net.cn

基岩匡威 API 支持多模态输入,包括文本和图像输入,并且可以根据组合输入生成文本响应。spring-doc.cadn.net.cn

您需要一个支持多模态输入的模型,例如 Anthropic Claude 或 Amazon Nova 模型。spring-doc.cadn.net.cn

图像

对于支持视觉多模态的模型,例如 Amazon Nova、Anthropic Claude、Llama 3.2,Bedrock Converse API Amazon 允许您在有效负载中包含多个图像。这些模型可以分析传递的图像并回答问题,对图像进行分类,以及根据提供的指令对图像进行汇总。spring-doc.cadn.net.cn

目前,Bedrock Converse 支持base64编码图像image/jpeg,image/png,image/gifimage/webpMIME 类型。spring-doc.cadn.net.cn

Spring AI 的Message界面通过引入Media类型。 它包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeTypejava.lang.Object用于原始媒体数据。spring-doc.cadn.net.cn

下面是一个简单的代码示例,演示了用户文本与图像的组合。spring-doc.cadn.net.cn

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图像:spring-doc.cadn.net.cn

多模态测试图像

以及短信“解释一下你在这张照片上看到了什么?”,并生成一个响应,如下所示:spring-doc.cadn.net.cn

The image shows a close-up view of a wire fruit basket containing several pieces of fruit.
...

视频

Amazon Nova 模型允许您在有效负载中包含单个视频,该视频可以以 base64 格式或通过 Amazon S3 URI 提供。spring-doc.cadn.net.cn

目前,基岩新星支持的视频video/x-matroska,video/quicktime,video/mp4,video/webm,video/x-flv,video/mpeg,video/x-ms-wmvvideo/3gppMIME 类型。spring-doc.cadn.net.cn

Spring AI 的Message界面通过引入Media类型。 它包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeTypejava.lang.Object用于原始媒体数据。spring-doc.cadn.net.cn

下面是一个简单的代码示例,演示了用户文本与视频的组合。spring-doc.cadn.net.cn

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图像:spring-doc.cadn.net.cn

多模态测试视频

以及短信“解释一下你在这个视频中看到了什么?”,并生成一个响应,如下所示:spring-doc.cadn.net.cn

The video shows a group of baby chickens, also known as chicks, huddled together on a surface
...

文件

对于某些模型,Bedrock 允许您通过 Converse API 文档支持将文档包含在有效负载中,该支持可以以字节为单位提供。 文档支持有两种不同的变体,如下所述:spring-doc.cadn.net.cn

  • 文本文档类型(txt、csv、html、md 等),重点是文本理解。这些用例包括基于文档的文本元素进行回答。spring-doc.cadn.net.cn

  • 媒体文档类型(pdf、docx、xlsx),重点是基于视觉的理解来回答问题。这些用例包括根据图表、图形等回答问题。spring-doc.cadn.net.cn

目前,Anthropic PDF 支持(测试版)和 Amazon Bedrock Nova 模型支持文档多模态。spring-doc.cadn.net.cn

下面是一个简单的代码示例,演示了用户文本与媒体文档的组合。spring-doc.cadn.net.cn

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公文:spring-doc.cadn.net.cn

多模态测试 PNG

以及短信“您是一位非常专业的文档摘要专家。请总结给定的文档。“,并生成如下所示的响应:spring-doc.cadn.net.cn

**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-starter-model-bedrock-converse到你的依赖关系。spring-doc.cadn.net.cn

添加一个application.propertiesfile 在src/main/resources:spring-doc.cadn.net.cn

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

下面是使用聊天模型的示例控制器:spring-doc.cadn.net.cn

@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();
    }
}