谷歌生成式AI文本嵌入

Google GenAI Embeddings API 通过 Gemini Developer API 或 Vertex AI 提供利用谷歌嵌入模型生成文本。 本文档介绍了如何使用 Google GenAI 文本嵌入 API 创建文本嵌入。spring-doc.cadn.net.cn

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

目前,Google GenAI SDK 仅支持文本嵌入。多模态嵌入支持正在等待中,预计将在SDK中提供时添加。spring-doc.cadn.net.cn

该实现提供了两种认证模式:spring-doc.cadn.net.cn

前提条件

请选择以下认证方法之一:spring-doc.cadn.net.cn

选项1:Gemini开发者API(API密钥)

选项二:Vertex AI(谷歌云)

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

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

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

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

嵌入性质

连接性质

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

连接属性与 Google GenAI 聊天模块共享。如果你同时使用聊天和嵌入,只需用其中一个配置一次连接spring.ai.google.genai前缀(用于聊天)或spring.ai.google.genai.embedding前缀(用于嵌入)。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.google.genai.embedding.api-keyspring-doc.cadn.net.cn

Gemini 开发者 API 的 API 密钥。客户端提供时,使用Gemini开发者API而非Vertex AI。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.google.genai.embedding.project-idspring-doc.cadn.net.cn

Google Cloud Platform 项目 ID(顶点 AI 模式必备)spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.google.genai.embedding.locationspring-doc.cadn.net.cn

Google Cloud 区域(Vertex AI 模式必备)spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.google.genai.embedding.credentials-urispring-doc.cadn.net.cn

URI到Google Cloud凭证。当它被提供时,用于创建谷歌凭证用于认证实例。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

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

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

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

文本嵌入属性

前缀spring.ai.google.genai.embedding.text是允许您配置 Google GenAI 文本嵌入嵌入模型实现的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认值

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

启用谷歌生成式AI嵌入API模型。spring-doc.cadn.net.cn

谷歌生成AI(Google Genai)spring-doc.cadn.net.cn

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

谷歌生成人工智能文本嵌入模型。支持的型号包括文本嵌入-004文本-多语言-嵌入-002spring-doc.cadn.net.cn

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

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

旨在帮助模型产生更高质量的嵌入。可用任务类型RETRIEVAL_QUERY,RETRIEVAL_DOCUMENT,SEMANTIC_SIMILARITY,分类,聚类,QUESTION_ANSWERING,FACT_VERIFICATIONspring-doc.cadn.net.cn

RETRIEVAL_DOCUMENTspring-doc.cadn.net.cn

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

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

-spring-doc.cadn.net.cn

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

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

-spring-doc.cadn.net.cn

spring.ai.google.genai.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-google-genai-embedding对你的POM(或Gradle)依赖。spring-doc.cadn.net.cn

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

使用 Gemini 开发者 API(API 密钥)

spring.ai.google.genai.embedding.api-key=YOUR_API_KEY
spring.ai.google.genai.embedding.text.options.model=text-embedding-004

使用 Vertex AI

spring.ai.google.genai.embedding.project-id=YOUR_PROJECT_ID
spring.ai.google.genai.embedding.location=YOUR_PROJECT_LOCATION
spring.ai.google.genai.embedding.text.options.model=text-embedding-004

这将产生GoogleGenAiTextEmbeddingModel你可以把这些实现注入到你的类里。 这里有一个简单的例子@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-google-genai-embedding对你项目Maven的依赖pom.xml文件:spring-doc.cadn.net.cn

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

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

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

接下来,创建一个GoogleGenAiTextEmbeddingModel并用于文本嵌入:spring-doc.cadn.net.cn

使用 API 密钥

GoogleGenAiEmbeddingConnectionDetails connectionDetails =
    GoogleGenAiEmbeddingConnectionDetails.builder()
        .apiKey(System.getenv("GOOGLE_API_KEY"))
        .build();

GoogleGenAiTextEmbeddingOptions options = GoogleGenAiTextEmbeddingOptions.builder()
    .model(GoogleGenAiTextEmbeddingOptions.DEFAULT_MODEL_NAME)
    .taskType(TaskType.RETRIEVAL_DOCUMENT)
    .build();

var embeddingModel = new GoogleGenAiTextEmbeddingModel(connectionDetails, options);

EmbeddingResponse embeddingResponse = embeddingModel
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

使用 Vertex AI

GoogleGenAiEmbeddingConnectionDetails connectionDetails =
    GoogleGenAiEmbeddingConnectionDetails.builder()
        .projectId(System.getenv("GOOGLE_CLOUD_PROJECT"))
        .location(System.getenv("GOOGLE_CLOUD_LOCATION"))
        .build();

GoogleGenAiTextEmbeddingOptions options = GoogleGenAiTextEmbeddingOptions.builder()
    .model(GoogleGenAiTextEmbeddingOptions.DEFAULT_MODEL_NAME)
    .taskType(TaskType.RETRIEVAL_DOCUMENT)
    .build();

var embeddingModel = new GoogleGenAiTextEmbeddingModel(connectionDetails, options);

EmbeddingResponse embeddingResponse = embeddingModel
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

任务类型

Google GenAI 嵌入 API 支持不同的任务类型,以优化嵌入以适应特定用例:spring-doc.cadn.net.cn

使用不同任务类型的示例:spring-doc.cadn.net.cn

// For indexing documents
GoogleGenAiTextEmbeddingOptions docOptions = GoogleGenAiTextEmbeddingOptions.builder()
    .model("text-embedding-004")
    .taskType(TaskType.RETRIEVAL_DOCUMENT)
    .title("Product Documentation")  // Optional title for documents
    .build();

// For search queries
GoogleGenAiTextEmbeddingOptions queryOptions = GoogleGenAiTextEmbeddingOptions.builder()
    .model("text-embedding-004")
    .taskType(TaskType.RETRIEVAL_QUERY)
    .build();

维度约简

对于型号004及以后版本,你可以减少嵌入维数以实现存储优化:spring-doc.cadn.net.cn

GoogleGenAiTextEmbeddingOptions options = GoogleGenAiTextEmbeddingOptions.builder()
    .model("text-embedding-004")
    .dimensions(256)  // Reduce from default 768 to 256 dimensions
    .build();

从 Vertex AI 文本嵌入迁移

如果你目前正在使用 Vertex AI 文本嵌入实现(spring-ai-vertex-ai-embedding),你可以以最小的改动迁移到 Google GenAI:spring-doc.cadn.net.cn

主要区别

  1. SDK:谷歌生成式人工智能使用新的com.google.genai.客户端而不是用 Vertex AI SDKspring-doc.cadn.net.cn

  2. 认证:支持API密钥和Google Cloud凭证spring-doc.cadn.net.cn

  3. 软件包名称:类别已入org.springframework.ai.google.genai.text而不是org.springframework.ai.vertexai.embeddingspring-doc.cadn.net.cn

  4. 属性前缀:用途spring.ai.google.genai.embedding而不是spring.ai.vertex.ai.embeddingspring-doc.cadn.net.cn

  5. 连接详情:用途谷歌GenAiEmbeddingConnectionDetails。而不是VertexAiEmbeddingConnection详情spring-doc.cadn.net.cn

何时使用Google GenAI与顶点AI文本嵌入

在以下情况下使用 Google GenAI 嵌入:- 你想用 API 密钥快速做原型 - 你需要开发者API中最新的嵌入功能 - 你需要在API密钥和顶点AI模式之间灵活切换 - 你已经在用谷歌生成式AI聊天了spring-doc.cadn.net.cn

在以下情况下使用 Vertex AI 文本嵌入: - 你已有 Vertex AI 基础设施 - 你需要多模态嵌入(目前仅在VertexAI中提供) - 您的组织需要仅部署Google Cloud系统spring-doc.cadn.net.cn