谷歌 VertexAI 多模态嵌入

实验的。 仅用于实验目的。尚未与向量存储器.

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>

添加仓库和物料清单

Spring AI 产物发布于 Maven Central 和 Spring Snapshot 仓库中。 请参阅神器仓库部分,将这些仓库添加到你的构建系统中。spring-doc.cadn.net.cn

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

自动配置

春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明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>

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

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

嵌入性质

前缀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 项目标识spring-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

顶点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

属性 描述 默认值

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

启用顶点AI嵌入API模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

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

启用顶点AI嵌入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

视频段的结束偏移量以秒为单位。如果未指定,则用最小值(视频长度,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。最大值没有限制该间隔。但如果间隔大于最小值(视频长度,120秒),则会影响生成嵌入的质量。默认值:16。spring-doc.cadn.net.cn

-spring-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>

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

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

接下来,创建一个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);