Google VertexAI 文字嵌入

Vertex AI 支援兩種型別的嵌入模型:文字和多模態。本文件介紹瞭如何使用 Vertex AI 文字嵌入 API 建立文字嵌入。

Vertex AI 文字嵌入 API 使用密集向量表示。與通常將單詞直接對映到數字的稀疏向量不同,密集向量旨在更好地表示文字的含義。在生成式 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 工件釋出在 Maven Central 和 Spring Snapshot 倉庫中。請參閱倉庫部分,將這些倉庫新增到您的構建系統中。

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

自動配置

Spring AI 自動配置、啟動器模組的工件名稱發生了重大變化。有關詳細資訊,請參閱升級注意事項

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 build.gradle 構建檔案中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-embedding'
}
請參閱依賴管理部分,將 Spring AI BOM 新增到您的構建檔案中。

嵌入屬性

字首 spring.ai.vertex.ai.embedding 用作屬性字首,允許您連線到 VertexAI Embedding 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 Embedding API 端點。

-

嵌入自動配置的啟用和停用現在透過帶有字首 spring.ai.model.embedding 的頂級屬性進行配置。

要啟用,請設定 spring.ai.model.embedding.text=vertexai(預設已啟用)

要停用,請設定 spring.ai.model.embedding.text=none(或任何不匹配 vertexai 的值)

此更改是為了允許配置多個模型。

字首 spring.ai.vertex.ai.embedding.text 是屬性字首,允許您配置 VertexAI 文字嵌入的嵌入模型實現。

屬性 描述 預設值

spring.ai.vertex.ai.embedding.text.enabled (已移除,不再有效)

啟用 Vertex AI Embedding API 模型。

true

spring.ai.model.embedding.text

啟用 Vertex AI Embedding API 模型。

vertexai

spring.ai.vertex.ai.embedding.text.options.model

這是要使用的 Vertex 文字嵌入模型

text-embedding-004

spring.ai.vertex.ai.embedding.text.options.task-type

預期的下游應用程式,有助於模型生成更高質量的嵌入。可用的 任務型別

RETRIEVAL_DOCUMENT

spring.ai.vertex.ai.embedding.text.options.title

可選的標題,僅在 task_type=RETRIEVAL_DOCUMENT 時有效。

-

spring.ai.vertex.ai.embedding.text.options.dimensions

生成的輸出嵌入應具有的維度數量。支援 004 及更高版本的模型。您可以使用此引數減小嵌入大小,例如用於儲存最佳化。

-

spring.ai.vertex.ai.embedding.text.options.auto-truncate

設定為 true 時,輸入文字將被截斷。設定為 false 時,如果輸入文字長於模型支援的最大長度,則返回錯誤。

true

示例 Controller

建立一個新的 Spring Boot 專案,並將 spring-ai-starter-model-vertex-ai-embedding 新增到您的 pom(或 gradle)依賴項中。

src/main/resources 目錄下新增一個 application.properties 檔案,以啟用和配置 VertexAi 聊天模型

spring.ai.vertex.ai.embedding.project-id=<YOUR_PROJECT_ID>
spring.ai.vertex.ai.embedding.location=<YOUR_PROJECT_LOCATION>
spring.ai.vertex.ai.embedding.text.options.model=text-embedding-004

這將建立一個 VertexAiTextEmbeddingModel 實現,您可以將其注入到您的類中。這是一個簡單的 @Controller 類示例,它使用嵌入模型生成嵌入。

@RestController
public class EmbeddingController {

    private final EmbeddingModel embeddingModel;

    @Autowired
    public EmbeddingController(EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }

    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
}

手動配置

VertexAiTextEmbeddingModel 實現了 EmbeddingModel 介面。

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 新增到您的構建檔案中。

接下來,建立一個 VertexAiTextEmbeddingModel 並使用它進行文字生成

VertexAiEmbeddingConnectionDetails connectionDetails =
    VertexAiEmbeddingConnectionDetails.builder()
        .projectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>))
        .location(System.getenv(<VERTEX_AI_GEMINI_LOCATION>))
        .build();

VertexAiTextEmbeddingOptions options = VertexAiTextEmbeddingOptions.builder()
    .model(VertexAiTextEmbeddingOptions.DEFAULT_MODEL_NAME)
    .build();

var embeddingModel = new VertexAiTextEmbeddingModel(this.connectionDetails, this.options);

EmbeddingResponse embeddingResponse = this.embeddingModel
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

從 Google 服務賬號載入憑據

要以程式設計方式從服務賬號 json 檔案載入 GoogleCredentials,可以使用以下方法

GoogleCredentials credentials = GoogleCredentials.fromStream(<INPUT_STREAM_TO_CREDENTIALS_JSON>)
        .createScoped("https://www.googleapis.com/auth/cloud-platform");
credentials.refreshIfExpired();

VertexAiEmbeddingConnectionDetails connectionDetails =
    VertexAiEmbeddingConnectionDetails.builder()
        .projectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>))
        .location(System.getenv(<VERTEX_AI_GEMINI_LOCATION>))
        .apiEndpoint(endpoint)
        .predictionServiceSettings(
            PredictionServiceSettings.newBuilder()
                .setEndpoint(endpoint)
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                .build());