Google GenAI 文字嵌入
Google GenAI 嵌入 API 使用 Gemini Developer API 或 Vertex AI 透過 Google 的嵌入模型提供文字嵌入生成。本文件描述瞭如何使用 Google GenAI 文字嵌入 API 建立文字嵌入。
Google GenAI 文字嵌入 API 使用密集向量表示。與傾向於將單詞直接對映到數字的稀疏向量不同,密集向量旨在更好地表示文字的含義。在生成式 AI 中使用密集向量嵌入的好處是,您不必搜尋直接的單詞或語法匹配,而是可以更好地搜尋與查詢含義對齊的段落,即使這些段落沒有使用相同的語言。
|
目前,Google GenAI SDK 僅支援文字嵌入。多模態嵌入支援正在等待中,將在 SDK 中可用時新增。 |
此實現提供兩種認證模式
-
Gemini Developer API:使用 API 金鑰進行快速原型設計和開發
-
Vertex AI:使用 Google Cloud 憑據進行具有企業功能的生產部署
先決條件
選擇以下任一身份驗證方法
選項 1:Gemini Developer API (API 金鑰)
-
從 Google AI Studio 獲取 API 金鑰
-
將 API 金鑰設定為環境變數或應用程式屬性
選項 2:Vertex AI (Google Cloud)
-
安裝適用於您作業系統的 gcloud CLI。
-
透過執行以下命令進行身份驗證。將
PROJECT_ID替換為您的 Google Cloud 專案 ID,將ACCOUNT替換為您的 Google Cloud 使用者名稱。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
Spring AI 為 Google GenAI 嵌入模型提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到您專案的 Maven pom.xml 檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-google-genai-embedding</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-google-genai-embedding'
}
| 請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
嵌入屬性
連線屬性
字首 spring.ai.google.genai.embedding 用作屬性字首,允許您連線到 Google GenAI 嵌入 API。
|
連線屬性與 Google GenAI Chat 模組共享。如果您同時使用聊天和嵌入,您只需使用 |
| 財產 | 描述 | 預設值 |
|---|---|---|
spring.ai.google.genai.embedding.api-key |
Gemini Developer API 的 API 金鑰。如果提供,客戶端將使用 Gemini Developer API 而不是 Vertex AI。 |
- |
spring.ai.google.genai.embedding.project-id |
Google Cloud Platform 專案 ID(Vertex AI 模式必需) |
- |
spring.ai.google.genai.embedding.location |
Google Cloud 區域(Vertex AI 模式必需) |
- |
spring.ai.google.genai.embedding.credentials-uri |
Google Cloud 憑據的 URI。如果提供,它將用於建立 |
- |
|
嵌入自動配置的啟用和停用現在透過字首為 要啟用,請設定 spring.ai.model.embedding.text=google-genai(預設已啟用) 要停用,請設定 spring.ai.model.embedding.text=none(或任何與 google-genai 不匹配的值) 此更改是為了允許配置多個模型。 |
文字嵌入屬性
字首 spring.ai.google.genai.embedding.text 是屬性字首,允許您為 Google GenAI 文字嵌入配置嵌入模型實現。
| 財產 | 描述 | 預設值 |
|---|---|---|
spring.ai.model.embedding.text |
啟用 Google GenAI 嵌入 API 模型。 |
google-genai |
spring.ai.google.genai.embedding.text.options.model |
要使用的 Google GenAI 文字嵌入模型。支援的模型包括 |
text-embedding-004 |
spring.ai.google.genai.embedding.text.options.task-type |
預期的下游應用程式,以幫助模型生成更高質量的嵌入。可用的任務型別: |
|
spring.ai.google.genai.embedding.text.options.title |
可選標題,僅在 task_type=RETRIEVAL_DOCUMENT 時有效。 |
- |
spring.ai.google.genai.embedding.text.options.dimensions |
結果輸出嵌入應具有的維度數量。模型版本 004 及更高版本支援。您可以使用此引數來減小嵌入大小,例如,用於儲存最佳化。 |
- |
spring.ai.google.genai.embedding.text.options.auto-truncate |
設定為 true 時,輸入文字將被截斷。設定為 false 時,如果輸入文字的長度超過模型支援的最大長度,則會返回錯誤。 |
true |
示例控制器
建立一個新的 Spring Boot 專案,並將 spring-ai-starter-model-google-genai-embedding 新增到您的 pom(或 gradle)依賴項中。
在 src/main/resources 目錄下新增一個 application.properties 檔案,以啟用和配置 Google GenAI 嵌入模型
使用 Gemini Developer 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 類的示例。
@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);
}
}
手動配置
GoogleGenAiTextEmbeddingModel 實現了 EmbeddingModel。
將 spring-ai-google-genai-embedding 依賴項新增到您專案的 Maven pom.xml 檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-google-genai-embedding</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-google-genai-embedding'
}
| 請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
接下來,建立一個 GoogleGenAiTextEmbeddingModel 並將其用於文字嵌入
使用 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 支援不同的任務型別,以針對特定用例最佳化嵌入
-
RETRIEVAL_QUERY:針對檢索系統中的搜尋查詢進行最佳化 -
RETRIEVAL_DOCUMENT:針對檢索系統中的文件進行最佳化 -
SEMANTIC_SIMILARITY:針對測量文字之間的語義相似性進行最佳化 -
CLASSIFICATION:針對文字分類任務進行最佳化 -
CLUSTERING:針對聚類相似文字進行最佳化 -
QUESTION_ANSWERING:針對問答系統進行最佳化 -
FACT_VERIFICATION:針對事實驗證任務進行最佳化
使用不同任務型別的示例
// 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 及更高版本,您可以減小嵌入維度以最佳化儲存
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
主要區別
-
SDK:Google GenAI 使用新的
com.google.genai.Client而不是 Vertex AI SDK -
身份驗證:支援 API 金鑰和 Google Cloud 憑據
-
包名:類位於
org.springframework.ai.google.genai.text而不是org.springframework.ai.vertexai.embedding -
屬性字首:使用
spring.ai.google.genai.embedding而不是spring.ai.vertex.ai.embedding -
連線詳情:使用
GoogleGenAiEmbeddingConnectionDetails而不是VertexAiEmbeddingConnectionDetails