Titan Embeddings

提供 Bedrock Titan 嵌入客户端。` Amazon Titan` 基础模型 (FM) 通过完全托管的 API 为客户提供范围广泛的高性能图像、多模态嵌入和文本模型选择。亚马逊 Titan 模型由 AWS 创建并在大型数据集上预训练,使其成为功能强大的通用模型,旨在支持各种用例,同时还支持负责任地使用 AI。直接使用它们,或使用您自己的数据私下自定义它们。

Provides Bedrock Titan Embedding client. Amazon Titan foundation models (FMs) provide customers with a breadth of high-performing image, multimodal embeddings, and text model choices, via a fully managed API. Amazon Titan models are created by AWS and pretrained on large datasets, making them powerful, general-purpose models built to support a variety of use cases, while also supporting the responsible use of AI. Use them as is or privately customize them with your own data.

Bedrock Titan Embedding 支持文本和图像嵌入。

Bedrock Titan Embedding supports Text and Image embedding.

Bedrock Titan Embedding 并不支持批处理嵌入。

Bedrock Titan Embedding does NOT support batch embedding.

AWS Bedrock Titan Model PageAmazon Bedrock User Guide 包含有关如何使用 AWS 托管模型的详细信息。

The AWS Bedrock Titan Model Page and Amazon Bedrock User Guide contains detailed information on how to use the AWS hosted model.

Prerequisites

请参阅 Spring AI documentation on Amazon Bedrock 以设置 API 访问。

Refer to the Spring AI documentation on Amazon Bedrock for setting up API access.

Add Repositories and BOM

Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。有关将这些存储库添加到你的构建系统的说明,请参阅 Repositories 部分。

Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.

为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单)以确保在整个项目中使用一致版本的 Spring AI。有关将 Spring AI BOM 添加到你的构建系统的说明,请参阅 Dependency Management 部分。

To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.

Auto-configuration

spring-ai-bedrock-ai-spring-boot-starter 依赖项添加到项目 Maven 的 pom.xml 文件:

Add the spring-ai-bedrock-ai-spring-boot-starter dependency to your project’s Maven pom.xml file:

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

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Enable Titan Embedding Support

默认情况下,Titan 嵌入模型处于禁用状态。若要启用,请将 spring.ai.bedrock.titan.embedding.enabled 属性设置为 true。导出环境变量是一种设置此配置属性的方法:

By default the Titan embedding model is disabled. To enable it set the spring.ai.bedrock.titan.embedding.enabled property to true. Exporting environment variable is one way to set this configuration property:

export SPRING_AI_BEDROCK_TITAN_EMBEDDING_ENABLED=true

Embedding Properties

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

The prefix spring.ai.bedrock.aws is the property prefix to configure the connection to AWS Bedrock.

Property Description Default

spring.ai.bedrock.aws.region

AWS region to use.

us-east-1

spring.ai.bedrock.aws.access-key

AWS access key.

-

spring.ai.bedrock.aws.secret-key

AWS secret key.

-

前缀 spring.ai.bedrock.titan.embedding(在 BedrockTitanEmbeddingProperties 中定义)是为 Titan 配置嵌入客户端实现的属性前缀。

The prefix spring.ai.bedrock.titan.embedding (defined in BedrockTitanEmbeddingProperties) is the property prefix that configures the embedding client implementation for Titan.

Property

Description

Default

spring.ai.bedrock.titan.embedding.enabled

Enable or disable support for Titan embedding

false

spring.ai.bedrock.titan.embedding.model

The model id to use. See the TitanEmbeddingModel for the supported models.

amazon.titan-embed-image-v1

支持的值:amazon.titan-embed-image-v1`和 `amazon.titan-embed-text-v1。还可以在 AWS Bedrock documentation for base model IDs中找到模型 ID 值。

Supported values are: amazon.titan-embed-image-v1 and amazon.titan-embed-text-v1. Model ID values can also be found in the AWS Bedrock documentation for base model IDs.

Sample Controller (Auto-configuration)

Create一个新的 Spring Boot 项目,并将 `spring-ai-bedrock-ai-spring-boot-starter`添加到您的 pom(或 gradle)依赖项。

Create a new Spring Boot project and add the spring-ai-bedrock-ai-spring-boot-starter to your pom (or gradle) dependencies.

src/main/resources 目录中添加一个 application.properties 文件,以启用和配置 Titan Embedding 客户端:

Add a application.properties file, under the src/main/resources directory, to enable and configure the Titan Embedding client:

spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}

spring.ai.bedrock.titan.embedding.enabled=true

regionsaccess-keysecret-key 替换为 AWS 凭证。

replace the regions, access-key and secret-key with your AWS credentials.

这将创建您可以注入类中的 EmbeddingController 实现。这是一个使用聊天客户端进行文本生成操作的简单 @Controller 类的示例。

This will create a EmbeddingController implementation that you can inject into your class. Here is an example of a simple @Controller class that uses the chat client for text generations.

@RestController
public class EmbeddingController {

    private final EmbeddingClient embeddingClient;

    @Autowired
    public EmbeddingController(EmbeddingClient embeddingClient) {
        this.embeddingClient = embeddingClient;
    }

    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
}

Manual Configuration

BedrockTitanEmbeddingClient实现`EmbeddingClient`,并且使用Low-level TitanEmbeddingBedrockApi Client连接到Bedrock Titan服务。

The BedrockTitanEmbeddingClient implements the EmbeddingClient and uses the Low-level TitanEmbeddingBedrockApi Client to connect to the Bedrock Titan service.

spring-ai-bedrock 依赖项添加到项目的 Maven pom.xml 文件:

Add the spring-ai-bedrock dependency to your project’s Maven pom.xml file:

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

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

下一步,创建一个 BedrockTitanEmbeddingClient,并将其用于文本嵌入:

Next, create an BedrockTitanEmbeddingClient and use it for text embeddings:

var titanEmbeddingApi = new TitanEmbeddingBedrockApi(
	TitanEmbeddingModel.TITAN_EMBED_IMAGE_V1.id(), Region.US_EAST_1.id());

var embeddingClient  new BedrockTitanEmbeddingClient(titanEmbeddingApi);

EmbeddingResponse embeddingResponse = embeddingClient
	.embedForResponse(List.of("Hello World")); // NOTE titan does not support batch embedding.

Low-level TitanEmbeddingBedrockApi Client

TitanEmbeddingBedrockApi 提供轻量级 Java 客户端,在 AWS Bedrock Titan Embedding models 上。

The TitanEmbeddingBedrockApi provides is lightweight Java client on top of AWS Bedrock Titan Embedding models.

下方的类图阐释了 TitanEmbeddingBedrockApi 接口和构建模块:

Following class diagram illustrates the TitanEmbeddingBedrockApi interface and building blocks:

bedrock titan embedding low level api

TitanEmbeddingBedrockApi 支持 amazon.titan-embed-image-v1amazon.titan-embed-image-v1 模型,用于单一和批量嵌入计算。

The TitanEmbeddingBedrockApi supports the amazon.titan-embed-image-v1 and amazon.titan-embed-image-v1 models for single and batch embedding computation.

下面是一个简单的片段,说明如何以编程方式使用 API:

Here is a simple snippet how to use the api programmatically:

TitanEmbeddingBedrockApi titanEmbedApi = new TitanEmbeddingBedrockApi(
		TitanEmbeddingModel.TITAN_EMBED_TEXT_V1.id(), Region.US_EAST_1.id());

TitanEmbeddingRequest request = TitanEmbeddingRequest.builder()
	.withInputText("I like to eat apples.")
	.build();

TitanEmbeddingResponse response = titanEmbedApi.embedding(request);

要嵌入图像,需要将其转换为 base64 格式:

To embed an image you need to convert it into base64 format:

TitanEmbeddingBedrockApi titanEmbedApi = new TitanEmbeddingBedrockApi(
		TitanEmbeddingModel.TITAN_EMBED_IMAGE_V1.id(), Region.US_EAST_1.id());

byte[] image = new DefaultResourceLoader()
	.getResource("classpath:/spring_framework.png")
	.getContentAsByteArray();


TitanEmbeddingRequest request = TitanEmbeddingRequest.builder()
	.withInputImage(Base64.getEncoder().encodeToString(image))
	.build();

TitanEmbeddingResponse response = titanEmbedApi.embedding(request);