Google VertexAI 多模態嵌入

實驗性功能。僅用於實驗目的。目前與 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 artifacts 發佈在 Spring Milestone 和 Snapshot 儲存庫中。請參閱儲存庫章節,將這些儲存庫新增至您的建置系統。

為了幫助進行相依性管理,Spring AI 提供了 BOM(物料清單),以確保在整個專案中使用一致版本的 Spring AI。請參閱相依性管理章節,將 Spring AI BOM 新增至您的建置系統。

自動組態

Spring AI 為 VertexAI 嵌入模型提供 Spring Boot 自動組態。若要啟用它,請將以下相依性新增至您專案的 Maven pom.xml 檔案

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

或您的 Gradle build.gradle 建置檔案。

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding-spring-boot-starter'
}
請參閱相依性管理章節,將 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.vertex.ai.embedding.multimodal 是屬性字首,讓您可以設定 VertexAI 多模態嵌入的嵌入模型實作。

屬性 描述 預設值

spring.ai.vertex.ai.embedding.multimodal.enabled

啟用 Vertex AI 嵌入 API 模型。

true

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