此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT!spring-doc.cadn.net.cn

Google VertexAI 文本嵌入

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

Vertex AI 文本嵌入 API 使用密集的矢量表示形式。 与倾向于直接将单词映射到数字的稀疏向量不同,密集向量旨在更好地表示一段文本的含义。 在生成式 AI 中使用密集向量嵌入的好处是,您可以更好地搜索与查询含义一致的段落,而不是直接搜索单词或语法匹配,即使这些段落没有使用相同的语言。spring-doc.cadn.net.cn

先决条件

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

添加存储库和 BOM

Spring AI 工件发布在 Maven Central 和 Spring Snapshot 存储库中。 请参阅 Repositories 部分,将这些存储库添加到您的构建系统中。spring-doc.cadn.net.cn

为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。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-vertex-ai-embedding-spring-boot-starter</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

嵌入属性

前缀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 项目 IDspring-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

Vertex AI Embedding API 端点。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.enabledspring-doc.cadn.net.cn

启用 Vertex AI Embedding API 模型。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

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

这是要使用的 Vertex Text Embedding 模型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

可选 title,仅在 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 时,输入文本将被截断。当设置为 false 时,如果输入文本的长度超过模型支持的最大长度,则会返回错误。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

Samples控制器

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

添加application.properties文件中的src/main/resources目录中,以启用和配置 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类,该类使用 embedding 模型进行 embeddings generations。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依赖项添加到项目的 Mavenpom.xml文件:spring-doc.cadn.net.cn

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

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-vertex-ai-embedding'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

接下来,创建一个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"));

从 Google 服务帐户加载凭据

要从服务帐户 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());