跳到主要内容

Google VertexAI 文本嵌入

DeepSeek V3 中英对照 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>
none

添加仓库和 BOM

Spring AI 构件已发布在 Maven Central 和 Spring Snapshot 仓库中。请参考 Repositories 部分,将这些仓库添加到您的构建系统中。

为了帮助进行依赖管理,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>
xml

或者到你的 Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding-spring-boot-starter'
}
groovy

:::提示
请参考依赖管理部分,将 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.apiEndpointVertex AI Embedding API 端点。-

前缀 spring.ai.vertex.ai.embedding.text 是用于配置 VertexAI 文本嵌入模型实现的属性前缀。

属性描述默认值
spring.ai.vertex.ai.embedding.text.enabled启用 Vertex AI Embedding 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 文件,以启用和配置 Vertex AI 聊天模型:

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
application.properties

这将创建一个 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);
}
}
java

手动配置

VertexAiTextEmbeddingModel 实现了 EmbeddingModel

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

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-embedding</artifactId>
</dependency>
xml

或到你的 Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding'
}
groovy
提示

请参考 依赖管理 部分,将 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"));
java

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