|
如需最新的快照版本,请使用 Spring AI 1.1.3! |
Google VertexAI 文本嵌入
Vertex AI 支持两种类型的嵌入模型:文本和多模态。 本文档介绍如何使用 Vertex AI 文本嵌入 API 创建文本嵌入。
Vertex AI 文本嵌入 API 使用稠密向量表示。 与稀疏向量不同(稀疏向量倾向于直接将单词映射为数字),稠密向量旨在更好地表示一段文本的含义。 在生成式人工智能中使用稠密向量嵌入的好处是,您不再需要搜索直接的单词或语法匹配,而是可以更有效地搜索与查询含义相符的段落,即使这些段落使用的语言不同。
前置条件
-
安装适用于您操作系统的 gcloud CLI。
-
通过运行以下命令进行身份验证。 将
PROJECT_ID替换为您的Google Cloud项目ID,将ACCOUNT替换为您的Google Cloud用户名。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>
Auto-configuration
|
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。
| <property> </property> | <description> </description> | 默认 |
|---|---|---|
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.text=vertexai(默认已启用) 要禁用此功能,请设置 spring.ai.model.embedding.text=none(或任何不与 vertexai 匹配的值) 此更改是为了允许配置多个模型。 |
前缀 spring.ai.vertex.ai.embedding.text 是属性前缀,用于配置 VertexAI 文本嵌入的嵌入模型实现。
| <property> </property> | <description> </description> | 默认 |
|---|---|---|
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.options.model |
这是要使用的 Vertex 文本嵌入模型 |
text-embedding-004 |
spring.ai.vertex.ai.embedding.text.options.task-type |
旨在帮助模型生成更高质量嵌入的下游应用程序。可用的 任务类型 |
|
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项目,并在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.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());