Google VertexAI 多模态嵌入模型

本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springdoc.cn 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。
实验性功能。仅用于实验目的,目前与 VectorStores 不兼容。

Vertex AI 支持两种嵌入模型:文本嵌入与多模态嵌入。本文档介绍如何使用 Vertex AI 多模态嵌入 API 创建多模态嵌入。

多模态嵌入模型基于输入的图像、文本或视频数据生成 1408 维向量,生成的嵌入向量可用于后续任务(如图像分类或视频内容审核)。

图像嵌入向量与文本嵌入向量位于同一语义空间且维度相同,因此可互换使用(例如:以文搜图、以图搜视频等场景)。

VertexAI 多模态 API 存在 这些限制
纯文本嵌入场景建议改用 Vertex AI 文本嵌入模型

先决条件

  • 安装适用于你操作系统的 gcloud CLI。

  • 运行以下命令进行认证(将 PROJECT_ID 替换为你的 Google Cloud 项目 ID,ACCOUNT 替换为你的 Google Cloud 用户名):

gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>

添加仓库和 BOM

Spring AI 构件已发布至 Maven 中央仓库和 Spring 快照仓库。请参阅 “构建仓库” 章节将这些仓库添加到你的构建系统中。

为简化依赖管理,Spring AI 提供 BOM 以确保项目统一使用相同版本。请参考 “依赖管理” 章节将 Spring AI BOM 添加到你的构建系统。

自动配置

Spring AI 自动配置及 starter 模块的构件命名已发生重大变更,详情请参阅 升级说明文档

Spring AI 为 VertexAI 嵌入模型提供 Spring Boot 自动配置。启用该功能需在项目的 Maven pom.xml 文件中添加以下依赖:

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

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

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-embedding'
}
请参考 “依赖管理” 章节将 Spring AI BOM 添加到你的构建文件。

嵌入配置

前缀 spring.ai.vertex.ai.embedding 是用于连接 VertexAI 嵌入 API 的属性前缀。

属性 说明 默认值

spring.ai.vertex.ai.embedding.project-id

Google Cloud Platform 项目 ID。

-

spring.ai.vertex.ai.embedding.location

Region

-

spring.ai.vertex.ai.embedding.apiEndpoint

Vertex AI 嵌入模型 API 端点

-

嵌入自动配置的启用与禁用现在通过顶级属性配置,前缀为 spring.ai.model.embedding

  • 启用多模态嵌入:spring.ai.model.embedding.multimodal=vertexai(默认启用)

  • 禁用多模态嵌入:spring.ai.model.embedding.multimodal=none(或任何不匹配 vertexai 的值)

此变更为支持多模型配置。

前缀 spring.ai.vertex.ai.embedding.multimodal 是用于配置 VertexAI 多模态嵌入模型实现的属性前缀。

属性 说明 默认值

spring.ai.vertex.ai.embedding.multimodal.enabled (已移除且不再生效)

启用 Vertex AI 嵌入 API 模型。

true

spring.ai.model.embedding.multimodal=vertexai

启用 Vertex AI 嵌入 API 模型。

vertexai

spring.ai.vertex.ai.embedding.multimodal.options.model

可通过以下模型获取多模态嵌入:

multimodalembedding@001

spring.ai.vertex.ai.embedding.multimodal.options.dimensions

指定低维嵌入向量。默认返回 1408 维浮点向量,也可为文本和图像数据指定低维嵌入(128、256 或 512 维浮点向量)。

1408

spring.ai.vertex.ai.embedding.multimodal.options.video-start-offset-sec

视频片段的起始偏移时间(秒)。未指定时默认值为 max(0, endOffsetSec - 120)

-

spring.ai.vertex.ai.embedding.multimodal.options.video-end-offset-sec

视频片段的结束偏移时间(秒)。未指定时默认值为 min(视频总时长, startOffSec + 120)。若同时指定了 startOffSecendOffSec,则 endOffsetSec 会自动调整为 min(startOffsetSec+120, endOffsetSec)

-

spring.ai.vertex.ai.embedding.multimodal.options.video-interval-sec

视频嵌入生成的采样间隔(秒)。最小间隔为 4 秒,若低于此值将返回 InvalidArgumentError。最大间隔无硬性限制,但若超过 min(视频总时长, 120秒) 将影响嵌入向量质量。默认值:16 秒。

-

手动配置

VertexAiMultimodalEmbeddingModel 实现了 DocumentEmbeddingModel 接口。

在项目的 Maven pom.xml 文件中添加 spring-ai-vertex-ai-embedding 依赖:

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

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

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding'
}
请参考 “依赖管理” 章节将 Spring AI BOM 添加到你的构建文件。

接下来,创建 VertexAiMultimodalEmbeddingModel 实例并用于生成嵌入向量:

VertexAiEmbeddingConnectionDetails connectionDetails =
    VertexAiEmbeddingConnectionDetails.builder()
        .projectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>))
        .location(System.getenv(<VERTEX_AI_GEMINI_LOCATION>))
        .build();

VertexAiMultimodalEmbeddingOptions options = VertexAiMultimodalEmbeddingOptions.builder()
    .model(VertexAiMultimodalEmbeddingOptions.DEFAULT_MODEL_NAME)
    .build();

var embeddingModel = new VertexAiMultimodalEmbeddingModel(this.connectionDetails, this.options);

Media imageMedial = new Media(MimeTypeUtils.IMAGE_PNG, new ClassPathResource("/test.image.png"));
Media videoMedial = new Media(new MimeType("video", "mp4"), new ClassPathResource("/test.video.mp4"));

var document = new Document("Explain what do you see on this video?", List.of(this.imageMedial, this.videoMedial), Map.of());

EmbeddingResponse embeddingResponse = this.embeddingModel
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

DocumentEmbeddingRequest embeddingRequest = new DocumentEmbeddingRequest(List.of(this.document),
        EmbeddingOptions.EMPTY);

EmbeddingResponse embeddingResponse = multiModelEmbeddingModel.call(this.embeddingRequest);

assertThat(embeddingResponse.getResults()).hasSize(3);