此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

Google VertexAI 多模态嵌入

实验性功能。仅用于实验目的。尚未与 VectorStores 兼容。

Vertex AI 支持两种类型的嵌入模型:文本和多模态。 本文档介绍如何使用 Vertex AI 多模态嵌入 API 创建多模态嵌入。spring-doc.cadn.net.cn

多模态嵌入模型根据您提供的输入生成 1408 维向量,这些输入可以包含图像、文本和视频数据的组合。 生成的嵌入向量随后可用于图像分类或视频内容审核等后续任务。spring-doc.cadn.net.cn

图像嵌入向量和文本嵌入向量位于具有相同维度的同一语义空间中。 因此,这些向量可以互换使用,适用于诸如通过文本搜索图像或通过图像搜索视频等用例。spring-doc.cadn.net.cn

VertexAI 多模态 API 施加了以下限制
对于仅文本的嵌入用例,我们建议使用 Vertex AI 文本嵌入模型 代替。

前置条件

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

添加仓库和BOM

Spring AI 构件发布在 Maven 中央仓库和 Spring Snapshot 仓库中。 请参阅 构件仓库 部分,将这些仓库添加到您的构建系统中。spring-doc.cadn.net.cn

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

Auto-configuration

Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。spring-doc.cadn.net.cn

Spring AI 为 VertexAI 嵌入模型提供了 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

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

请将以下内容添加到您的Gradle build.gradle 构建文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-embedding'
}
请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。

嵌入属性

前缀 spring.ai.vertex.ai.embedding 用作属性前缀,让您能够连接到 VertexAI Embedding API。spring-doc.cadn.net.cn

<property> </property> <description> </description> 默认

spring.ai.vertex.ai.embedding.project-idspring-doc.cadn.net.cn

Google Cloud Platform 项目 IDspring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.locationspring-doc.cadn.net.cn

区域spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.apiEndpointspring-doc.cadn.net.cn

Vertex AI 嵌入 API 端点。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

嵌入自动配置的启用和禁用现在通过前缀为 spring.ai.model.embedding 的顶层属性进行配置。spring-doc.cadn.net.cn

要启用,请设置 spring.ai.model.embedding.multimodal=vertexai(默认已启用)spring-doc.cadn.net.cn

要禁用,请设置 spring.ai.model.embedding.multimodal=none(或任何不匹配 vertexai 的值)spring-doc.cadn.net.cn

此更改是为了允许配置多个模型。spring-doc.cadn.net.cn

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

<property> </property> <description> </description> 默认

spring.ai.vertex.ai.embedding.multimodal.enabled(已移除且不再生效)spring-doc.cadn.net.cn

启用 Vertex AI Embedding API 模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.model.embedding.multimodal=vertexaispring-doc.cadn.net.cn

启用 Vertex AI Embedding API 模型。spring-doc.cadn.net.cn

VertexAIspring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.multimodal.options.modelspring-doc.cadn.net.cn

您可以通过使用以下模型获取多模态嵌入:spring-doc.cadn.net.cn

多模态嵌入@001spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.multimodal.options.dimensionsspring-doc.cadn.net.cn

指定低维嵌入。默认情况下,嵌入请求会为数据类型返回一个包含 1408 个浮点数的向量。您也可以为文本和图像数据指定更低维度的嵌入(128、256 或 512 个浮点数的向量)。spring-doc.cadn.net.cn

1408spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.multimodal.options.video-start-offset-secspring-doc.cadn.net.cn

视频片段在秒级的起始偏移量。如果未指定,则通过 max(0, endOffsetSec - 120) 计算得出。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.multimodal.options.video-end-offset-secspring-doc.cadn.net.cn

视频片段的结束偏移量(以秒为单位)。如果未指定,则按 min(视频长度,startOffSec + 120) 计算。如果同时指定了 startOffSec 和 endOffSec,则 endOffsetSec 将调整为 min(startOffsetSec+120, endOffSec)。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.multimodal.options.video-interval-secspring-doc.cadn.net.cn

生成嵌入的视频间隔。interval_sec 的最小值为 4。 如果间隔小于 4,将返回 InvalidArgumentError。间隔的最大值没有限制。 但是,如果间隔大于 min(视频长度,120 秒),会影响生成的嵌入质量。默认值:16。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

手动配置

VertexAiMultimodalEmbeddingModel 实现了 DocumentEmbeddingModelspring-doc.cadn.net.cn

将如下的spring-ai-vertex-ai-embedding依赖添加到项目中Maven的pom.xml文件中:spring-doc.cadn.net.cn

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

请将以下内容添加到您的Gradle build.gradle 构建文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding'
}
请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。

接下来,创建一个 VertexAiMultimodalEmbeddingModel 并用于生成嵌入:spring-doc.cadn.net.cn

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