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 text-embeddings 模型

先决条件

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

添加存储库和 BOM

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

为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。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-vertex-ai-embedding-spring-boot-starter</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

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

嵌入属性

前缀spring.ai.vertex.ai.embedding用作属性前缀,用于连接到 VertexAI 嵌入 API。spring-doc.cadn.net.cn

财产 描述 违约

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 Embedding API 端点。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

财产 描述 违约

spring.ai.vertex.ai.embedding.multimodal.enabledspring-doc.cadn.net.cn

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

spring-doc.cadn.net.cn

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

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

multimodalembedding@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(video length, startOffSec + 120) 计算。如果同时指定了 startOffSec 和 endOffSec,则 endOffsetSec 会调整为 min(startOffsetSec+120, endOffsetSec)。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(video length, 120s),则会影响生成的嵌入质量。默认值:16。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

手动配置

添加spring-ai-vertex-ai-embedding依赖项添加到项目的 Mavenpom.xml文件:spring-doc.cadn.net.cn

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

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

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

接下来,创建一个VertexAiMultimodalEmbeddingModel并将其用于 embeddings generations:spring-doc.cadn.net.cn

VertexAiEmbeddingConnectionDetails connectionDetails =
    VertexAiEmbeddingConnectionDetails.builder()
        .withProjectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>))
        .withLocation(System.getenv(<VERTEX_AI_GEMINI_LOCATION>))
        .build();

VertexAiMultimodalEmbeddingOptions options = VertexAiMultimodalEmbeddingOptions.builder()
    .withModel(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);