跳到主要内容

Google VertexAI 多模态嵌入

Deepseek 3.2 中英对照 Multimodal Embedding Google VertexAI Multimodal Embeddings

备注

实验性功能。仅用于实验目的。目前尚未与 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 Central 和 Spring Snapshot 仓库。请参阅构件仓库章节,将这些仓库添加至您的构建系统。

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

自动配置

备注

Spring AI 的自动配置和 starter 模块的 artifact 名称发生了重大变化。更多信息请参阅 升级说明

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-idGoogle Cloud Platform 项目 ID-
spring.ai.vertex.ai.embedding.location区域-
spring.ai.vertex.ai.embedding.apiEndpointVertex AI Embedding 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 Embedding API 模型。true
spring.ai.model.embedding.multimodal=vertexai启用 Vertex AI Embedding 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) 计算。如果同时指定了 startOffSec 和 endOffSec,则 endOffsetSec 会调整为 min(startOffsetSec+120, endOffsetSec)。-
spring.ai.vertex.ai.embedding.multimodal.options.video-interval-sec生成嵌入的视频采样间隔(秒)。interval_sec 的最小值为 4。如果间隔小于 4,将返回 InvalidArgumentError。间隔的最大值没有限制。但是,如果间隔大于 min(视频长度, 120秒),则会影响生成的嵌入的质量。默认值:16。-

手动配置

VertexAiMultimodalEmbeddingModel 实现了 DocumentEmbeddingModel 接口。

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

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