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 嵌入 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 的頂級屬性進行配置。

要啟用,`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 嵌入 API 模型。

true

spring.ai.model.embedding.text

啟用 Vertex AI 嵌入 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

示例控制器

建立一個新的 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());
© . This site is unofficial and not affiliated with VMware.