谷歌 VertexAI 文本嵌入
顶点AI支持两种类型的嵌入模型,文本模型和多模模型。 本文档介绍如何使用Vertex AI文本嵌入API创建文本嵌入。
顶点AI文本嵌入API使用密集向量表示。 与稀疏向量倾向于直接将单词映射到数字不同,密集向量的设计是为了更好地表示文本的含义。 生成式AI中使用密集向量嵌入的好处是,你不必直接搜索词语或语法匹配,而是能更好地搜索与查询含义相符的段落,即使这些段落使用不同的语言。
前提条件
-
安装适合你作系统的gcloud光源。
-
通过执行以下命令进行认证。 取代
PROJECT_ID与你的 Google Cloud 项目 ID 以及帐户用你的Google Cloud用户名。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
Spring AI 为 VertexAI 嵌入模型提供 Spring Boot 自动配置。
要启用它,请在项目的Maven中添加以下依赖pom.xml文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-vertex-ai-embedding</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-embedding'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
嵌入性质
前缀spring.ai.vertex.ai.embedding作为属性前缀,允许你连接到VertexAI嵌入API。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.vertex.ai.embedding.project-id |
Google Cloud Platform 项目标识 |
- |
spring.ai.vertex.ai.embedding.location |
地区 |
- |
spring.ai.vertex.ai.embedding.apiEndpoint |
顶点AI嵌入API端点。 |
- |
|
嵌入自动配置的启用和禁用现在通过带有前缀的顶层属性配置 启用时,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(已移除且不再有效) |
启用顶点AI嵌入API模型。 |
true |
spring.ai.model.embedding.text |
启用顶点AI嵌入API模型。 |
顶点 |
spring.ai.vertex.ai.embedding.text.options.model |
这就是顶点文本嵌入模型 |
文本嵌入-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(true)时,输入文本会被截断。当设置为false时,如果输入文本长度超过模型支持的最大长度,则返回错误。 |
true |
采样控制器
创建一个新的 Spring Boot 项目并添加Spring-AI-starter-model-vertex-ai-embedding对你的POM(或Gradle)依赖。
添加一个application.properties文件,在src/主/资源目录,用于启用和配置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 实现了嵌入模型.
添加spring-ai-vertex-ai-embedding对你项目Maven的依赖pom.xml文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-embedding</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
接下来,创建一个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"));
从谷歌服务账户加载凭证
要从服务账户的 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());