此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT!spring-doc.cadn.net.cn

基岩版 Converse API

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

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

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。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-bedrock-converse-spring-boot-starter依赖项添加到项目的 Mavenpom.xml或 Gradlebuild.gradlebuild 文件:spring-doc.cadn.net.cn

<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-doc.cadn.net.cn

财产 描述 违约

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

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

us-east-1 (美国东部-1)spring-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.bedrock.aws.secret-keyspring-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 会话令牌。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.enabledspring-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。您可以使用 Supported models (支持的模型) 和 model (模型) 功能spring-doc.cadn.net.cn

没有。从 AWS Bedrock 控制台中选择您的 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

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

AWS Bedrock 默认spring-doc.cadn.net.cn

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

用于生成下一个令牌的令牌选项数。spring-doc.cadn.net.cn

AWS Bedrock 默认spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.max 代币spring-doc.cadn.net.cn

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

500spring-doc.cadn.net.cn

运行时选项

使用便携式ChatOptionsToolCallingChatOptions用于创建模型配置的便携式构建器,例如 temperature、maxToken、topP 等。spring-doc.cadn.net.cn

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

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

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 的工具的示例: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?")
        .tools("weatherFunction")
        .inputType(Request.class)
        .call()
        .content();

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

模 态

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

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

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

图像

对于支持视觉多模态的模型,例如 Amazon Nova、Anthropic Claude、Llama 3.2,Amazon 的 Bedrock Converse API 允许您在负载中包含多个图像。这些模型可以分析传递的图像并回答问题、对图像进行分类,以及根据提供的说明对图像进行汇总。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.MimeType以及java.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

多模态测试图像

以及文本消息“Explain what do you see on this picture?”,并生成如下响应: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

目前,Bedrock Nova 支持video/x-matros,video/quicktime,video/mp4,video/video/webm,video/x-flv,video/mpeg,video/x-ms-wmvimage/3gppMIME 类型。spring-doc.cadn.net.cn

Spring AI 的Message界面通过引入Media`类型。 它包含有关消息中媒体附件的数据和信息,使用 Spring 的org.springframework.util.MimeType以及java.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

多模态测试视频

以及文本消息“Explain what do you see in this video?”,并生成如下响应: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-bedrock-converse-spring-boot-starter添加到您的依赖项中。spring-doc.cadn.net.cn

添加application.properties文件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

下面是一个使用 chat 模型的示例控制器: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();
    }
}