跳到主要内容
版本:2.0.1

Google VertexAI 文本嵌入

QWen 3.8 中英对照 Text Embedding Google VertexAI Text Embeddings

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 仓库。请参考 Artifact Repositories 部分,将这些仓库添加到你的构建系统中。

为了辅助依赖管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致版本的 Spring AI。请参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建系统中。

自动配置​

备注

Spring AI 的自动配置以及 starter 模块的 artifact 名称发生了重大变更。详细信息请参阅升级说明。

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-idGoogle Cloud Platform 项目 ID-
spring.ai.vertex.ai.embedding.location区域-
spring.ai.vertex.ai.embedding.api-endpointVertex 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 Text Embedding 嵌入模型实现的属性前缀。

属性描述默认值
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.model要使用的 Vertex 文本嵌入模型text-embedding-004
spring.ai.vertex.ai.embedding.text.task-type预期的下游应用,帮助模型生成更高质量的嵌入。可用的 任务类型RETRIEVAL_DOCUMENT
spring.ai.vertex.ai.embedding.text.title可选标题,仅在 task_type=RETRIEVAL_DOCUMENT 时有效。-
spring.ai.vertex.ai.embedding.text.dimensions最终输出嵌入应具有的数量。支持模型版本 004 及更高版本。您可以使用此参数来减小嵌入大小,例如用于存储优化。-
spring.ai.vertex.ai.embedding.text.auto-truncate设置为 true 时,输入文本将被截断。设置为 false 时,如果输入文本长度超过模型支持的最大长度,则返回错误。true

示例控制器​

创建 一个新的 Spring Boot 项目,并在 pom(或 gradle)依赖项中添加 spring-ai-starter-model-vertex-ai-embedding。

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