介紹 Spring AI Amazon Bedrock Nova 透過 Converse API 整合

工程 | Christian Tzolov | 2024 年 12 月 10 日 | ...

Amazon Bedrock Nova 模型代表著新一代的基礎模型,支援從文字和影像理解到影片轉文字分析等廣泛的用例。

透過 Spring AI Bedrock Converse API 整合,開發人員可以輕鬆連線到這些先進的 Nova 模型,並以最少的精力構建複雜的對話式應用程式。

這篇博文介紹了 Amazon Nova 模型的主要特性,演示了它們與 Spring AI 的 Bedrock Converse API 的整合,並提供了文字、影像、影片、文件處理和函式呼叫的實際示例。

什麼是 Amazon Nova 模型?

Amazon Nova 提供三個層級的模型——Nova Pro、Nova Lite 和 Nova Micro——以滿足不同的效能和成本需求。

規格 Nova Pro Nova Lite Nova Micro
模態 文字、影像、影片轉文字 文字、影像、影片轉文字 文字
模型 ID amazon.nova-pro-v1:0 amazon.nova-lite-v1:0 amazon.nova-micro-v1:0
最大 token 數 300K 300K 128K

Nova Pro 和 Lite 支援多模態功能,包括文字、影像和影片輸入,而 Nova Micro 針對純文字互動進行了最佳化,成本更低。

設定整合

前提條件

  1. AWS 配置: 您需要

    • 具有訪問 Amazon Bedrock 許可權的 AWS 憑據
    • 使用 Nova 模型所需的許可權
    • Amazon Bedrock 控制檯 中啟用的模型
  2. Spring AI 依賴: 將 Spring AI Bedrock Converse starter 新增到您的 Spring Boot 專案中

    Maven:

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

    Gradle:

    dependencies {
        implementation 'org.springframework.ai:spring-ai-bedrock-converse-spring-boot-starter'
    }
    
  3. 應用程式配置: 為 Amazon Bedrock 配置 application.properties

    spring.ai.bedrock.aws.region=us-east-1
    spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
    spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
    spring.ai.bedrock.aws.session-token=${AWS_SESSION_TOKEN}
    
    spring.ai.bedrock.converse.chat.options.model=amazon.nova-pro-v1:0
    
    spring.ai.bedrock.converse.chat.options.temperature=0.8
    spring.ai.bedrock.converse.chat.options.max-tokens=1000
    

    有關更多詳細資訊,請參考聊天屬性文件。

Bedrock Nova 整合的關鍵特性

1. 文字補全

基於文字的聊天補全非常簡單

String response = ChatClient.create(chatModel)
    .prompt("Tell me a joke about AI.")
    .call()
    .content();

2. 多模態輸入

Nova Pro 和 Lite 支援多模態輸入,支援處理文字和視覺資料。Spring AI 提供了一個可移植的 多模態 API,支援 Bedrock Nova 模型。

文字 + 影像

Nova Pro 和 Lite 支援多種影像模態。這些模型可以分析影像、回答關於影像的問題、對影像進行分類,並根據提供的說明生成摘要。它們支援 image/jpegimage/pngimage/gifimage/webp 格式的 base64 編碼影像。

使用者文字與影像結合的示例

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

此程式碼處理 test.png 影像:,以及文字訊息 "Explain what do you see on this picture?",並生成如下響應:

影像顯示了裝有幾塊水果的金屬水果籃的特寫檢視...

文字 + 影片

Amazon Nova Pro/Lite 模型支援負載中的單個影片模態,可以 base64 格式或透過 Amazon S3 URI 提供。

支援的影片格式包括 video/x-matrosvideo/quicktimevideo/mp4video/webmvideo/x-flvvideo/mpegvideo/x-ms-wmvimage/3gpp

使用者文字與影片結合的示例

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

此程式碼處理 test.video.mp4 影片 ,以及文字訊息 "Explain what do you see in this video?",並生成如下響應:

影片顯示了一群小雞(也稱為雛雞)擠在一起,在某個表面上...

文字 + 文件

Nova Pro/Lite 支援兩種文件模態變體

  • 文字文件型別 (txt, csv, html, md 等) 用於文字理解和基於文字元素的問答
  • 媒體文件型別 (pdf, docx, xlsx) 用於基於視覺的理解,例如分析圖表

使用者文字與媒體文件結合的示例

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

此程式碼處理 spring-ai-reference-overview.pdf 文件:,以及文字訊息,並生成如下響應:

簡介

  • Spring AI 旨在簡化開發具有人工智慧 (AI) 功能的應用程式,目標是避免不必要的複雜性...

3. 函式呼叫

Nova 模型支援工具/函式呼叫,用於與外部工具整合。

定義函式

@Bean
@Description("Get the weather in a location. Return temperature in Celsius or Fahrenheit.")
public Function<WeatherRequest, WeatherResponse> weatherFunction() {
    return new MockWeatherService();
}

在聊天提示中使用函式

String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .function("weatherFunction") // bean name
        .inputType(WeatherRequest.class)
        .call()
        .content();

資源

入門

Amazon Bedrock 資源

程式碼示例

Tanzu AI Server

VMware Tanzu Platform 10 透過 VMware Tanzu AI Server(由 Spring AI 提供支援)集成了 Amazon Bedrock Nova 模型。此整合提供

  • 企業級 AI 部署: 在您的 VMware Tanzu 環境中部署 AI 應用程式的生產就緒解決方案
  • 簡化的模型訪問: 透過統一介面簡化對 Amazon Bedrock Nova 模型的訪問
  • 安全與治理: 企業級安全控制和治理功能
  • 可擴充套件的基礎設施: 基於 Spring AI 構建,此整合支援可擴充套件地部署 AI 應用程式,同時保持高效能

有關使用 Tanzu AI Server 部署 AI 應用程式的更多資訊,請訪問 VMware Tanzu AI 文件

結論

透過 Converse API 將 Spring AI 與 Amazon Bedrock Nova 模型整合,為構建先進的對話式應用程式提供了強大的功能。Nova Pro 和 Lite 為開發跨文字、影像、影片和文件的多模態體驗提供了全面的工具。函式呼叫透過支援與外部工具和服務互動,進一步擴充套件了這些功能。

立即開始使用 Nova 模型和 Spring AI 構建先進的 AI 應用程式!

訂閱 Spring 新聞通訊

透過 Spring 新聞通訊保持聯絡

訂閱

搶佔先機

VMware 提供培訓和認證,助力您的發展。

瞭解更多

獲取支援

Tanzu Spring 透過一個簡單的訂閱提供對 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案。

瞭解更多

即將到來的活動

檢視 Spring 社群的所有即將到來的活動。

檢視全部