Google VertexAI Text Embeddings
Vertex AI 支持两种类型的嵌入模型:文本嵌入和多模态嵌入。 本文档描述了如何使用 Vertex AI 的 文本嵌入 API 创建文本嵌入。
Vertex AI 文本嵌入 API 采用密集向量表示法。与稀疏向量不同,后者倾向于直接将单词映射为数字,而密集向量旨在更好地表达文本的含义。在生成式 AI 中使用密集向量嵌入的优势在于,您不再局限于寻找直接的词汇或句法匹配,而是能够更有效地检索与查询意义相符的段落,即便这些段落使用的语言并不相同。
前提条件(Prerequisites)
- 安装适用于您操作系统的 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 添加到您的构建系统。
自动配置(Auto-configuration)
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'
}嵌入属性(Embedding Properties)
前缀 spring.ai.vertex.ai.embedding 用作属性前缀,使您能够连接到 VertexAI 嵌入 API。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.vertex.ai.embedding.project-id |
Google 云平台项目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.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 |
示例控制器(Sample Controller)
创建一个新的 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使用 EmbeddingClient 的简单示例。
@RestController
public class EmbeddingController {
private final VertexAiTextEmbeddingModel embeddingModel;
@Autowired
public EmbeddingController(VertexAiTextEmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/v1/embedding")
public Map<String, Object> embedding(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("embeddings", embeddingModel.embed(message));
}
}手动配置(Manual Configuration)
VertexAiTextEmbeddingModel 实现了 EmbeddingModel , 并使用轻量级 OpenAiApi 客户端连接到 OpenAI 服务。
要启用它,添加 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'
}接下来,创建一个 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());最后编辑:Jeebiz 更新时间:2025-09-28 09:15