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自动配置、starter模块的构件名称发生了重大变化。 请参考https://docs.spring.io/spring-ai/reference/upgrade-notes.html[升级说明]获取更多信息。

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

示例控制器

在https://start.spring.io/[创建]一个新的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`。

在项目的Maven `pom.xml`文件中添加`spring-ai-vertex-ai-embedding`依赖:

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