|
此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3! |
谷歌VertexAI多模态嵌入
实验性。仅用于实验目的。尚未与VectorStores兼容。 |
Vertex AI 支持两种类型的嵌入模型,文本和多模态。本文档介绍如何使用 Vertex AI 多模态嵌入API 创建多模态嵌入。
多模态嵌入模型根据您提供的输入生成1408维向量,这些输入可以是图像、文本和视频数据的组合。 生成的嵌入向量可用于后续任务,如图像分类或视频内容审核。
图像嵌入向量和文本嵌入向量位于相同的语义空间中,具有相同的维度。 因此,这些向量可以互换使用于诸如通过文本搜索图像或通过图像搜索视频等应用场景。
| VertexAI多模态API施加了以下限制。 |
| 对于仅文本嵌入的使用场景,我们建议使用Vertex AI 文本嵌入模型代替。 |
前提条件
-
安装适合您操作系统的gcloud命令行工具。
-
通过运行以下命令进行身份验证。 将
PROJECT_ID替换为您的Google Cloud项目ID,将ACCOUNT替换为您的Google Cloud用户名。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>
自动配置
|
There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。 |
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 构建脚本文件中。
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 |
区域 |
- |
spring.ai.vertex.ai.embedding.apiEndpoint |
Vertex AI 嵌入式 API 端点。 |
- |
|
启用和禁用嵌入式自动配置现在通过带有前缀 要启用,请设置 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 模型。 |
顶点AI |
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。间隔的最大值没有限制。 但是,如果间隔大于视频时长和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 构建脚本文件中。
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);