Titan Embeddings

提供 Bedrock Titan 嵌入模型。Amazon Titan 基础模型(FMs)通过完全托管的 API,为客户提供了一系列高性能的图像、多模态嵌入和文本模型选择。 Amazon Titan 模型由 AWS 创建,并在大型数据集上进行了预训练,使其成为强大的通用模型,旨在支持多种使用场景,同时支持 AI 的负责任使用。 可以直接使用它们,或使用自己的数据进行私人定制。

AWS Bedrock Titan 模型页面 和 Amazon Bedrock 用户指南 包含了关于如何使用 AWS 托管模型的详细信息。

前提条件(Prerequisites)

请参考 Spring AI 关于 Amazon Bedrock 的文档 以设置 API 访问权限。

添加存储库和 BOM

Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。请参阅存储库部分将这些存储库添加到您的构建系统中。

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

自动配置(Auto-configuration)

Spring AI 为 Bedrock Titan 嵌入模型提供 Spring Boot 自动配置。要启用它,请在项目的 Maven pom.xml文件中添加以下依赖项:

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

或者,在你的 Gradle 构建文件 build.gradle 中添加:

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

启用 Titan 嵌入支持

默认情况下,Titan 嵌入模型是禁用的。要启用它,请在您的应用程序配置中将 spring.ai.model.embedding 属性设置为 bedrock-titan:

spring.ai.model.embedding=bedrock-titan

或者,您也可以使用 Spring 表达式语言(SpEL)来引用一个环境变量:

# In application.yml
spring:
  ai:
    model:
      embedding: ${AI_MODEL_EMBEDDING}
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-titan

您也可以在启动应用程序时通过 Java 系统属性来设置此属性:

java -Dspring.ai.model.embedding=bedrock-titan -jar your-application.jar

嵌入属性(Embedding Properties)

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

属性 描述 默认值
spring.ai.bedrock.aws.region 使用的AWS区域 us-east-1
spring.ai.bedrock.aws.access-key AWS访问密钥 -
spring.ai.bedrock.aws.secret-key AWS密钥 -

前缀 spring.ai.bedrock.cohere.embedding(定义于 ‘BedrockCohereEmbeddingProperties‘ 中)是用于配置 Cohere 嵌入模型实现的属性前缀。

查看 CohereEmbeddingModel 以获取其他模型 ID。 支持的模型包括:cohere.embed-multilingual-v3 和 cohere.embed-english-v3。 模型 ID 值也可在 AWS Bedrock 基础模型 ID 文档 中找到。

属性 描述 默认值
spring.ai.bedrock.titan.embedding.enabled (已移除且无效) 启用/禁用Titan嵌入支持 false
spring.ai.model.embedding 启用/禁用Titan嵌入支持 bedrock-titan
spring.ai.bedrock.titan.embedding.model 使用的模型ID,支持的模型请参考TitanEmbeddingModel amazon.titan-embed-image-v1

运行时选项(Runtime Options )

BedrockTitanEmbeddingOptions.java 提供模型配置,例如 input-type。

启动时,默认选项可以通过 BedrockTitanEmbeddingModel (api).withInputType (type) 方法或 spring.ai.bedrock.titan.embedding.input-type 属性进行配置。

在运行时,您可以通过向 EmbeddingRequest 调用添加新的、特定于请求的选项来覆盖默认设置。例如,要为特定请求覆盖默认的 input-type :

EmbeddingResponse embeddingResponse = embeddingModel.call(
    new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
        BedrockTitanEmbeddingOptions.builder()
            .withInputType(InputType.TEXT)
        .build()));

示例控制器(Sample Controller)

创建一个新的 Spring Boot 项目,并将 spring-ai-starter-model-bedrock 添加到 pom (或 gradle) 依赖项中。

在 src/main/resources 目录下添加 application.properties 文件,以启用和配置 Cohere Embedding 模型:

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.model.embedding=bedrock-cohere
spring.ai.bedrock.cohere.embedding.options.input-type=search-document

这将创建一个 BedrockCohereEmbeddingModel 的实现,你可以将其注入到你的类中。下面是一个简单的 @Controller 类的示例,它使用聊天模型进行文本生成。

@RestController
public class EmbeddingController {

    private final BedrockTitanEmbeddingModel embeddingModel;

    @Autowired
    public EmbeddingController(BedrockTitanEmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }

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

}

手动配置(Manual Configuration)

BedrockTitanEmbeddingModel 实现了 EmbeddingModel,并利用轻量级 TitanEmbeddingBedrockApi 客户端 连接至 Bedrock Titan 服务。

要启用它,添加 spring-ai-bedrock 依赖到你的项目 Maven pom.xml 文件:

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

或者,在你的 Gradle 构建文件 build.gradle 中添加:

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

接下来,创建一个 BedrockTitanEmbeddingModel 并将其用于文本嵌入:

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

var embeddingModel = new BedrockTitanEmbeddingModel(this.titanEmbeddingApi);

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

轻量级 TitanEmbeddingBedrockApi 客户端

TitanEmbeddingBedrockApi 在 AWS Bedrock 的 Titan Embedding 模型 之上提供了一个轻量级的 Java 客户端。。

以下类图展示了 TitanEmbeddingBedrockApi 接口及其构建模块:

TitanEmbeddingBedrockApi 支持使用 amazon.titan-embed-image-v1 和 amazon.titan-embed-image-v1 模型进行单次及批量嵌入计算。

以下是一个简单的代码片段,展示如何以编程方式使用该 API:

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 = this.titanEmbedApi.embedding(this.request);

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

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(this.image))
    .build();

TitanEmbeddingResponse response = this.titanEmbedApi.embedding(this.request);
作者:Ddd4j  创建时间:2025-08-03 11:55
最后编辑:Ddd4j  更新时间:2026-04-23 14:31
上一篇:
下一篇: