侧边栏壁纸
博主头像
AI中文站

开拓MCP洪荒时代

  • 累计撰写 28 篇文章
  • 累计创建 3 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

SpringAI-Bedrock 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 实现所有聊天对话功能。虽然现有 InvokeModel API 支持对话应用,但我们强烈建议所有聊天对话模型采用 Converse API。

Converse API 不支持嵌入操作,因此这些功能将保留在当前 API 中,现有 InvokeModel API 的嵌入模型功能将保持不变

先决条件

参考 Getting started with Amazon Bedrock 设置 API 访问权限

  • 获取 AWS 凭证:如果您尚未配置 AWS 账户和 AWS CLI,此视频指南可帮助配置:AWS CLI & SDK Setup in Less Than 4 Minutes!。您应能获取访问密钥和安全密钥。

  • 启用要使用的模型:访问 Amazon Bedrock,在左侧 Model Access 菜单中配置对要使用模型的访问权限。

自动配置

Spring AI 自动配置和 starter 模块的构件名称已发生重大变更。请参考升级说明获取更多信息。

将 spring-ai-starter-model-bedrock-converse 依赖项添加到项目的 Maven pom.xml 或 Gradle build.gradle 构建文件中:

Maven

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-bedrock-converse</artifactId>
</dependency>

Gradle

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-bedrock-converse'
}

参考 Dependency Management 部分将 Spring AI BOM 添加到构建文件。


聊天属性

前缀 spring.ai.bedrock.aws 是配置 AWS Bedrock 连接的属性前缀。

Property

Description

Default

spring.ai.bedrock.aws.region

AWS region to use.

us-east-1

spring.ai.bedrock.aws.timeout

AWS timeout to use.

5m

spring.ai.bedrock.aws.access-key

AWS access key.

-

spring.ai.bedrock.aws.secret-key

AWS secret key.

-

spring.ai.bedrock.aws.session-token

AWS session token for temporary credentials.

-

聊天自动配置的启用和禁用现在通过顶级属性配置,前缀为 spring.ai.model.chat。

启用:spring.ai.model.chat=bedrock-converse(默认启用)

禁用:spring.ai.model.chat=none(或任何不匹配 bedrock-converse 的值)

此变更允许配置多个模型。

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

Property

Description

Default

spring.ai.bedrock.converse.chat.enabled (Removed and no longer valid)

Enable Bedrock Converse chat model.

true

spring.ai.model.chat

Enable Bedrock Converse chat model.

bedrock-converse

spring.ai.bedrock.converse.chat.options.model

The model ID to use. You can use the Supported models and model features

None. Select your modelId from the AWS Bedrock console.

spring.ai.bedrock.converse.chat.options.temperature

Controls the randomness of the output. Values can range over [0.0,1.0]

0.8

spring.ai.bedrock.converse.chat.options.top-p

The maximum cumulative probability of tokens to consider when sampling.

AWS Bedrock default

spring.ai.bedrock.converse.chat.options.top-k

Number of token choices for generating the next token.

AWS Bedrock default

spring.ai.bedrock.converse.chat.options.max-tokens

Maximum number of tokens in the generated response.

500

运行时选项

使用可移植的 ChatOptions 或 ToolCallingChatOptions 构建器创建模型配置,如 temperature、maxToken、topP 等。

启动时,可通过 BedrockConverseProxyChatModel(api, options) 构造函数或 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(options)
    .call()
    .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),Bedrock Converse API 允许在有效负载中包含多个图像。这些模型可分析传递的图像并回答问题、对图像分类以及根据提供的指令总结图像。

当前,Bedrock Converse 支持 image/jpeg、image/png、image/gif 和 image/webp MIME 类型的 base64 编码图像。

Spring AI 的 Message 接口通过引入 Media 类型支持多模态 AI 模型。它包含消息中媒体附件的数据和信息,使用 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 图像作为输入:

多模态测试图像
附带文本消息"解释你在这张图片中看到了什么?",并生成类似响应:

图像展示了一个装有多个水果的金属丝水果篮特写视图。
...

视频

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/3gpp MIME 类型的视频。

Spring AI 的 Message 接口通过引入 Media 类型支持多模态 AI 模型。它包含消息中媒体附件的数据和信息,使用 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 视频作为输入:


附带文本消息"解释你在这段视频中看到了什么?",并生成类似响应:

视频展示了一群小鸡(也称为雏鸡)挤在表面上
...

文档

对于某些模型,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 文档作为输入:


附带文本消息"您是非常专业的文档摘要专家。请总结给定文档。",并生成类似响应:

Spring AI 旨在简化具有人工智能(AI)功能的应用程序开发,力求避免不必要的复杂性。
...


示例控制器

创建新的 Spring Boot 项目并将 spring-ai-starter-model-bedrock-converse 添加到依赖项。

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

以下是使用聊天模型的控制器示例:

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

0

评论区