一、Google GenAI 文本嵌入与准备工作
Google GenAI 文本嵌入 API 使用密集向量表示。与稀疏向量(倾向于直接将单词映射到数字)不同,密集向量旨在更好地表示一段文本的含义。在生成式 AI 中使用密集向量嵌入的好处是,您不仅可以搜索直接的单词或语法匹配,还可以更好地搜索与查询含义相符的段落,即使这些段落不使用相同的语言。
目前,Google GenAI SDK 仅支持文本嵌入。多模态嵌入支持正在开发中,将在 SDK 可用时添加。
此实现提供认证模式:
备注(重要):【需要一定的上网技术,甚至运行项目也需要此条件。】
-
Gemini 开发者 API: 使用 API 密钥进行快速原型设计和开发。
-
从 Google AI Studio 获取 API 密钥。(目前适量使用的免费的,见图示)
-
将 API 密钥设置为环境变量或在应用程序属性中设置

-
二、项目依赖:采用最新的 Spring Boot 4.0.5 版本,适配的 AI 为 Spring AI 2.0.0.M4
采用 Gradle项目,因为配置要比Maven简洁的多。配置语法采用kotlin,语法结构上更加规范,有相应错误检查。
具体的 build.gradle.kts 配置如下
plugins {
java
id("org.springframework.boot") version "4.0.5"
id("io.spring.dependency-management") version "1.1.7"
id("io.spring.nullability") version "0.0.12"
}
group = "com.yiyi"
version = "0.0.1-SNAPSHOT"
description = "web"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(25)
}
}
repositories {
mavenCentral()
maven {
name = "Central Portal Snapshots"
url = uri("https://central.sonatype.com/repository/maven-snapshots/")
}
}
dependencies {
implementation(platform("org.springframework.ai:spring-ai-bom:2.0.0-M4"))
implementation("org.springframework.boot:spring-boot-starter-webflux")
// AI
implementation("org.springframework.ai:spring-ai-starter-model-deepseek")
implementation("org.springframework.ai:spring-ai-starter-model-google-genai-embedding")
}
tasks.withType<Test> {
useJUnitPlatform()
}
三、Spring Boot 的配置文件:application.yaml
spring:
application:
name: yiiai # 可访问 yiiai.top
ai:
deepseek:
api-key: [你自己的 DeepSeek api-key]
chat:
options:
model: deepseek-chat
temperature: 0.7
google:
genai:
embedding:
api-key: [你自己的 Gemini api-key]
四、实战项目代码 : EmbeddingController.java
package com.yiyi.web.controller;
import org.jspecify.annotations.NullMarked;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.google.genai.text.GoogleGenAiTextEmbeddingModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@NullMarked
@RestController
public class EmbeddingController {
private final EmbeddingModel embeddingModel;
public EmbeddingController(GoogleGenAiTextEmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/embed")
public float[] embed(@RequestParam String text) {
EmbeddingResponse response = embeddingModel.embedForResponse(List.of(text));
float[] result = response.getResults().getFirst().getOutput();
System.out.println("float[] result length: " + result.length);
return result;
}
}
Spring Boot 启动类,以后的文章中省略此启动类
package com.yiyi.web;
import org.jspecify.annotations.NullMarked;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
@NullMarked
@SpringBootApplication
@EnableR2dbcRepositories
public class WebApplication {
static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
五、测试与检验:对字符串hello spring ai进行向量化
用浏览器访问 : http://localhost:8080/embed?text=hello%20spring%20ai
向量化后是 float[] 类型的数组。数组长度共有3072,表示向量化为 3072个维度,远超一般的 768维度。(这就是Google GenAI 文本嵌入 API 使用密集向量表示。)
生成后的向量化数据如图(部分截图):

1531



被折叠的 条评论
为什么被折叠?



