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 構件發佈在 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.text 是屬性字首,讓您可以設定 VertexAI 文字嵌入的嵌入模型實作。

屬性 描述 預設值

spring.ai.vertex.ai.embedding.text.enabled

啟用 Vertex AI 嵌入 API 模型。

true

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

範例控制器

建立新的 Spring Boot 專案,並將 spring-ai-vertex-ai-embedding-spring-boot-starter 新增至您的 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()
        .withProjectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>))
        .withLocation(System.getenv(<VERTEX_AI_GEMINI_LOCATION>))
        .build();

VertexAiTextEmbeddingOptions options = VertexAiTextEmbeddingOptions.builder()
    .withModel(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()
        .withProjectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>))
        .withLocation(System.getenv(<VERTEX_AI_GEMINI_LOCATION>))
        .withApiEndpoint(endpoint)
        .withPredictionServiceSettings(
            PredictionServiceSettings.newBuilder()
                .setEndpoint(endpoint)
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                .build());