一、Google VertexAI 文本嵌入模型
Vertex AI 支持两种类型的嵌入模型:文本嵌入和多模态嵌入。本文档介绍如何使用 Vertex AI 文本嵌入 API 创建文本嵌入。
Vertex AI 文本嵌入 API 使用稠密向量表示。与直接映射单词到数字的稀疏向量不同,稠密向量旨在更好地表示文本的含义。在生成式 AI 中使用稠密向量嵌入的优点是,您可以更好地搜索与查询含义相符的段落,而不仅仅是搜索直接的单词或语法匹配,即使这些段落使用的语言不同。
二、先决条件
安装适用于您操作系统的 gcloud CLI。
通过运行以下命令进行身份验证。请将 <PROJECT_ID> 替换为您的 Google Cloud 项目 ID,将 替换为您的 Google Cloud 用户名。
gcloud configsetproject<PROJECT_ID>&&gcloud auth application-default login<ACCOUNT>添加仓库和 BOM
Spring AI 的构件发布在 Maven Central 和 Spring Snapshot 仓库中。请参阅 构件仓库 部分,将这些仓库添加到您的构建系统中。
为便于依赖管理,Spring AI 提供了 BOM(物料清单),以确保在整个项目中使用统一版本的 Spring AI。请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建系统中。
三、自动配置
Spring 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>或在您的 Gradle build.gradle 构建文件中添加:
dependencies{implementation'org.springframework.ai:spring-ai-starter-model-vertex-ai-embedding'}请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建文件中。
嵌入属性
前缀 spring.ai.vertex.ai.embedding 用作属性前缀,用于连接到 VertexAI 嵌入 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 文本嵌入的嵌入模型实现的属性前缀。
四、示例控制器
创建一个新的 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 类示例。
@RestControllerpublicclassEmbeddingController{privatefinalEmbeddingModelembeddingModel;@AutowiredpublicEmbeddingController(EmbeddingModelembeddingModel){this.embeddingModel=embeddingModel;}@GetMapping("/ai/embedding")publicMapembed(@RequestParam(value="message",defaultValue="Tell me a joke")Stringmessage){EmbeddingResponseembeddingResponse=this.embeddingModel.embedForResponse(List.of(message));returnMap.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 并使用它进行文本生成:
VertexAiEmbeddingConnectionDetailsconnectionDetails=VertexAiEmbeddingConnectionDetails.builder().projectId(System.getenv(<VERTEX_AI_GEMINI_PROJECT_ID>)).location(System.getenv(<VERTEX_AI_GEMINI_LOCATION>)).build();VertexAiTextEmbeddingOptionsoptions=VertexAiTextEmbeddingOptions.builder().model(VertexAiTextEmbeddingOptions.DEFAULT_MODEL_NAME).build();varembeddingModel=newVertexAiTextEmbeddingModel(this.connectionDetails,this.options);EmbeddingResponseembeddingResponse=this.embeddingModel.embedForResponse(List.of("Hello World","World is big and salvation is near"));从 Google 服务账户加载凭据
要以编程方式从服务账户 JSON 文件加载 GoogleCredentials,您可以使用以下代码:
GoogleCredentialscredentials=GoogleCredentials.fromStream(<INPUT_STREAM_TO_CREDENTIALS_JSON>).createScoped("https://www.googleapis.com/auth/cloud-platform");credentials.refreshIfExpired();VertexAiEmbeddingConnectionDetailsconnectionDetails=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());