最新快照版本请使用Spring AI 1.1.0spring-doc.cadn.net.cn

谷歌 VertexAI 文本嵌入

顶点AI支持两种类型的嵌入模型,文本模型和多模模型。 本文档介绍如何使用Vertex AI文本嵌入API创建文本嵌入。spring-doc.cadn.net.cn

顶点AI文本嵌入API使用密集向量表示。 与稀疏向量倾向于直接将单词映射到数字不同,密集向量的设计是为了更好地表示文本的含义。 生成式AI中使用密集向量嵌入的好处是,你不必直接搜索词语或语法匹配,而是能更好地搜索与查询含义相符的段落,即使这些段落使用不同的语言。spring-doc.cadn.net.cn

前提条件

gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>

添加仓库和物料清单

Spring AI 产物发布于 Maven Central 和 Spring Snapshot 仓库中。 请参阅神器仓库部分,将这些仓库添加到你的构建系统中。spring-doc.cadn.net.cn

为帮助依赖管理,Spring AI 提供了物料清单(BOM),确保整个项目中使用一致版本的 Spring AI。请参考依赖管理部分,将春季AI物料清单添加到你的构建系统中。spring-doc.cadn.net.cn

自动配置

春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明spring-doc.cadn.net.cn

Spring AI 为 VertexAI 嵌入模型提供 Spring Boot 自动配置。 要启用它,请在项目的Maven中添加以下依赖pom.xml文件:spring-doc.cadn.net.cn

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

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-vertex-ai-embedding'
}
请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。

嵌入性质

前缀spring.ai.vertex.ai.embedding作为属性前缀,允许你连接到VertexAI嵌入API。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.vertex.ai.embedding.project-idspring-doc.cadn.net.cn

Google Cloud Platform 项目标识spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.locationspring-doc.cadn.net.cn

地区spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.apiEndpointspring-doc.cadn.net.cn

顶点AI嵌入API端点。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

嵌入自动配置的启用和禁用现在通过带有前缀的顶层属性配置spring.ai.model.embedding.spring-doc.cadn.net.cn

启用时,spring.ai.model.embedding.text=vertexai(默认启用)spring-doc.cadn.net.cn

要禁用,可以选择 spring.ai.model.embedding.text=none(或任何不匹配 vertexai 的值)spring-doc.cadn.net.cn

此改动旨在允许配置多个模型。spring-doc.cadn.net.cn

前缀spring.ai.vertex.ai.embedding.text是允许你配置VertexAI文本嵌入嵌入模型实现的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.vertex.ai.embedding.text.enabled(已移除且不再有效)spring-doc.cadn.net.cn

启用顶点AI嵌入API模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.model.embedding.textspring-doc.cadn.net.cn

启用顶点AI嵌入API模型。spring-doc.cadn.net.cn

顶点spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.text.options.modelspring-doc.cadn.net.cn

这就是顶点文本嵌入模型spring-doc.cadn.net.cn

文本嵌入-004spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.text.options.task-typespring-doc.cadn.net.cn

旨在帮助模型产生更高质量的嵌入。可用的任务类型spring-doc.cadn.net.cn

RETRIEVAL_DOCUMENTspring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.text.options.titlespring-doc.cadn.net.cn

可选标题,仅在task_type=RETRIEVAL_DOCUMENT时有效。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.text.options.dimensionsspring-doc.cadn.net.cn

输出嵌入应有的维数。支持004及以后版本。你可以利用该参数来减少嵌入大小,例如用于存储优化。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vertex.ai.embedding.text.options.auto-truncatespring-doc.cadn.net.cn

当设置为 true(true)时,输入文本会被截断。当设置为false时,如果输入文本长度超过模型支持的最大长度,则返回错误。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

采样控制器

创建一个新的 Spring Boot 项目并添加Spring-AI-starter-model-vertex-ai-embedding对你的POM(或Gradle)依赖。spring-doc.cadn.net.cn

添加一个application.properties文件,在src/主/资源目录,用于启用和配置VertexAi聊天模型:spring-doc.cadn.net.cn

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使用嵌入模型进行嵌入代的类。spring-doc.cadn.net.cn

@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);
    }
}

手动配置

添加spring-ai-vertex-ai-embedding对你项目Maven的依赖pom.xml文件:spring-doc.cadn.net.cn

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

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding'
}
请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。

接下来,创建一个VertexAiTextEmbeddingModel并用于文本生成:spring-doc.cadn.net.cn

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,可以使用以下方法:spring-doc.cadn.net.cn

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