Gemini API 提供文字嵌入模型,可為字詞、片語、句子和程式碼生成嵌入項目。這些基礎嵌入可支援進階 NLP 工作,例如語意搜尋、分類和叢集,與以關鍵字為基礎的方法相比,可提供更準確、符合脈絡的結果。
建構檢索增強生成 (RAG) 系統是嵌入的常見用途。嵌入在大幅提升模型輸出內容方面扮演重要角色,可提高事實準確度、連貫性及情境豐富度。這些模型會從知識庫有效擷取相關資訊 (以嵌入表示),然後在輸入提示中將這些資訊做為額外情境傳遞至語言模型,引導模型生成更準確的回覆。
如要進一步瞭解可用的嵌入模型變體,請參閱「模型版本」一節。對於企業級應用程式和大量工作負載,建議使用 Vertex AI 的嵌入模型。
生成嵌入
使用 embedContent
方法生成文字嵌入:
Python
from google import genai client = genai.Client() result = client.models.embed_content( model="gemini-embedding-001", contents="What is the meaning of life?") print(result.embeddings)
JavaScript
import { GoogleGenAI } from "@google/genai"; async function main() { const ai = new GoogleGenAI({}); const response = await ai.models.embedContent({ model: 'gemini-embedding-001', contents: 'What is the meaning of life?', }); console.log(response.embeddings); } main();
Go
package main import ( "context" "encoding/json" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } contents := []*genai.Content{ genai.NewContentFromText("What is the meaning of life?", genai.RoleUser), } result, err := client.Models.EmbedContent(ctx, "gemini-embedding-001", contents, nil, ) if err != nil { log.Fatal(err) } embeddings, err := json.MarshalIndent(result.Embeddings, "", " ") if err != nil { log.Fatal(err) } fmt.Println(string(embeddings)) }
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"model": "models/gemini-embedding-001", "content": {"parts":[{"text": "What is the meaning of life?"}]} }'
您也可以將多個區塊做為字串清單傳遞,一次產生嵌入。
Python
from google import genai client = genai.Client() result = client.models.embed_content( model="gemini-embedding-001", contents= [ "What is the meaning of life?", "What is the purpose of existence?", "How do I bake a cake?" ]) for embedding in result.embeddings: print(embedding)
JavaScript
import { GoogleGenAI } from "@google/genai"; async function main() { const ai = new GoogleGenAI({}); const response = await ai.models.embedContent({ model: 'gemini-embedding-001', contents: [ 'What is the meaning of life?', 'What is the purpose of existence?', 'How do I bake a cake?' ], }); console.log(response.embeddings); } main();
Go
package main import ( "context" "encoding/json" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } contents := []*genai.Content{ genai.NewContentFromText("What is the meaning of life?"), genai.NewContentFromText("How does photosynthesis work?"), genai.NewContentFromText("Tell me about the history of the internet."), } result, err := client.Models.EmbedContent(ctx, "gemini-embedding-001", contents, nil, ) if err != nil { log.Fatal(err) } embeddings, err := json.MarshalIndent(result.Embeddings, "", " ") if err != nil { log.Fatal(err) } fmt.Println(string(embeddings)) }
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"model": "models/gemini-embedding-001", "content": [ {"parts": [{"text": "What is the meaning of life?"}]}, {"parts": [{"text": "What is the purpose of existence?"}]}, {"parts": [{"text": "How do I bake a cake?"}]} ] }'
指定要提升成效的工作類型
您可以將嵌入項目用於各種工作,從分類到文件搜尋皆可。指定正確的任務類型有助於針對預期關係最佳化嵌入項目,進而提高準確度和效率。如需支援的完整工作類型清單,請參閱「支援的工作類型」表格。
以下範例說明如何使用 SEMANTIC_SIMILARITY
檢查文字字串的意義相似程度。
Python
from google import genai from google.genai import types import numpy as np from sklearn.metrics.pairwise import cosine_similarity client = genai.Client() texts = [ "What is the meaning of life?", "What is the purpose of existence?", "How do I bake a cake?"] result = [ np.array(e.values) for e in client.models.embed_content( model="gemini-embedding-001", contents=texts, config=types.EmbedContentConfig(task_type="SEMANTIC_SIMILARITY")).embeddings ] # Calculate cosine similarity. Higher scores = greater semantic similarity. embeddings_matrix = np.array(result) similarity_matrix = cosine_similarity(embeddings_matrix) for i, text1 in enumerate(texts): for j in range(i + 1, len(texts)): text2 = texts[j] similarity = similarity_matrix[i, j] print(f"Similarity between '{text1}' and '{text2}': {similarity:.4f}")
JavaScript
import { GoogleGenAI } from "@google/genai"; import * as cosineSimilarity from "compute-cosine-similarity"; async function main() { const ai = new GoogleGenAI({}); const texts = [ "What is the meaning of life?", "What is the purpose of existence?", "How do I bake a cake?", ]; const response = await ai.models.embedContent({ model: 'gemini-embedding-001', contents: texts, taskType: 'SEMANTIC_SIMILARITY' }); const embeddings = response.embeddings.map(e => e.values); for (let i = 0; i < texts.length; i++) { for (let j = i + 1; j < texts.length; j++) { const text1 = texts[i]; const text2 = texts[j]; const similarity = cosineSimilarity(embeddings[i], embeddings[j]); console.log(`Similarity between '${text1}' and '${text2}': ${similarity.toFixed(4)}`); } } } main();
Go
package main import ( "context" "fmt" "log" "math" "google.golang.org/genai" ) // cosineSimilarity calculates the similarity between two vectors. func cosineSimilarity(a, b []float32) (float64, error) { if len(a) != len(b) { return 0, fmt.Errorf("vectors must have the same length") } var dotProduct, aMagnitude, bMagnitude float64 for i := 0; i < len(a); i++ { dotProduct += float64(a[i] * b[i]) aMagnitude += float64(a[i] * a[i]) bMagnitude += float64(b[i] * b[i]) } if aMagnitude == 0 || bMagnitude == 0 { return 0, nil } return dotProduct / (math.Sqrt(aMagnitude) * math.Sqrt(bMagnitude)), nil } func main() { ctx := context.Background() client, _ := genai.NewClient(ctx, nil) defer client.Close() texts := []string{ "What is the meaning of life?", "What is the purpose of existence?", "How do I bake a cake?", } var contents []*genai.Content for _, text := range texts { contents = append(contents, genai.NewContentFromText(text, genai.RoleUser)) } result, _ := client.Models.EmbedContent(ctx, "gemini-embedding-001", contents, &genai.EmbedContentRequest{TaskType: genai.TaskTypeSemanticSimilarity}, ) embeddings := result.Embeddings for i := 0; i < len(texts); i++ { for j := i + 1; j < len(texts); j++ { similarity, _ := cosineSimilarity(embeddings[i].Values, embeddings[j].Values) fmt.Printf("Similarity between '%s' and '%s': %.4f\n", texts[i], texts[j], similarity) } } }
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "contents": [ {"parts": [{"text": "What is the meaning of life?"}]}, {"parts": [{"text": "What is the purpose of existence?"}]}, {"parts": [{"text": "How do I bake a cake?"}]} ], "embedding_config": { "task_type": "SEMANTIC_SIMILARITY" } }'
以下是這個程式碼片段的輸出範例:
Similarity between 'What is the meaning of life?' and 'What is the purpose of existence?': 0.9481 Similarity between 'What is the meaning of life?' and 'How do I bake a cake?': 0.7471 Similarity between 'What is the purpose of existence?' and 'How do I bake a cake?': 0.7371
支援的工作類型
工作類型 | 說明 | 範例 |
---|---|---|
SEMANTIC_SIMILARITY | 經過最佳化處理的嵌入,可評估文字相似度。 | 推薦系統、重複內容偵測 |
分類 | 嵌入模型經過最佳化調整,可根據預設標籤分類文字。 | 情緒分析、垃圾訊息偵測 |
分群 | 經過最佳化,可根據相似度將文字分組。 | 文件整理、市場調查、異常偵測 |
RETRIEVAL_DOCUMENT | 專為文件搜尋最佳化的嵌入內容。 | 為文章、書籍或網頁建立索引,方便搜尋。 |
RETRIEVAL_QUERY | 針對一般搜尋查詢最佳化的嵌入內容。 查詢時使用 RETRIEVAL_QUERY ,擷取文件時使用 RETRIEVAL_DOCUMENT 。 | 自訂搜尋 |
CODE_RETRIEVAL_QUERY | 經過最佳化處理的嵌入,可根據自然語言查詢擷取程式碼區塊。 使用 CODE_RETRIEVAL_QUERY 查詢;使用 RETRIEVAL_DOCUMENT 擷取程式碼區塊。 | 程式碼建議和搜尋 |
QUESTION_ANSWERING | 問答系統中的問題嵌入,經過最佳化處理,可找出回答問題的文件。 使用 QUESTION_ANSWERING 提出問題;使用 RETRIEVAL_DOCUMENT 擷取文件。 | Chatbox |
FACT_VERIFICATION | 需要驗證的陳述內容的嵌入項目,經過最佳化處理,可擷取含有佐證或反駁陳述內容的文件。 使用 FACT_VERIFICATION 做為目標文字;使用 RETRIEVAL_DOCUMENT 做為要擷取的檔案 | 自動事實查核系統 |
控制嵌入大小
Gemini 嵌入模型 gemini-embedding-001
是使用 Matryoshka Representation Learning (MRL) 技術訓練而成,這項技術可讓模型學習高維度嵌入,這些嵌入具有初始區段 (或前置字元),也是相同資料的實用簡化版本。
使用 output_dimensionality
參數控制輸出嵌入向量的大小。選取較小的輸出維度可節省儲存空間,並提高下游應用程式的運算效率,同時幾乎不會犧牲品質。根據預設,系統會輸出 3072 維度的嵌入,但您可以將其截斷為較小的大小,以節省儲存空間,且不會降低品質。建議使用 768、1536 或 3072 的輸出尺寸。
Python
from google import genai from google.genai import types client = genai.Client() result = client.models.embed_content( model="gemini-embedding-001", contents="What is the meaning of life?", config=types.EmbedContentConfig(output_dimensionality=768) ) [embedding_obj] = result.embeddings embedding_length = len(embedding_obj.values) print(f"Length of embedding: {embedding_length}")
JavaScript
import { GoogleGenAI } from "@google/genai"; async function main() { const ai = new GoogleGenAI({}); const response = await ai.models.embedContent({ model: 'gemini-embedding-001', content: 'What is the meaning of life?', outputDimensionality: 768, }); const embeddingLength = response.embedding.values.length; console.log(`Length of embedding: ${embeddingLength}`); } main();
Go
package main import ( "context" "fmt" "log" "google.golang.org/genai" ) func main() { ctx := context.Background() // The client uses Application Default Credentials. // Authenticate with 'gcloud auth application-default login'. client, err := genai.NewClient(ctx, nil) if err != nil { log.Fatal(err) } defer client.Close() contents := []*genai.Content{ genai.NewContentFromText("What is the meaning of life?", genai.RoleUser), } result, err := client.Models.EmbedContent(ctx, "gemini-embedding-001", contents, &genai.EmbedContentRequest{OutputDimensionality: 768}, ) if err != nil { log.Fatal(err) } embedding := result.Embeddings[0] embeddingLength := len(embedding.Values) fmt.Printf("Length of embedding: %d\n", embeddingLength) }
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \ -H "x-goog-api-key: YOUR_GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "contents": [ {"parts": [{"text": "What is the meaning of life?"}]} ], "embedding_config": { "output_dimensionality": 768 } }'
程式碼片段的輸出範例如下:
Length of embedding: 768
確保較小尺寸的品質
3072 維度嵌入項目已正規化。標準化嵌入會比較向量方向,而非大小,因此可產生更準確的語意相似度。如要使用其他維度 (包括 768 和 1536),請按照下列方式將嵌入內容標準化:
Python
import numpy as np from numpy.linalg import norm embedding_values_np = np.array(embedding_obj.values) normed_embedding = embedding_values_np / np.linalg.norm(embedding_values_np) print(f"Normed embedding length: {len(normed_embedding)}") print(f"Norm of normed embedding: {np.linalg.norm(normed_embedding):.6f}") # Should be very close to 1
這個程式碼片段的輸出範例如下:
Normed embedding length: 768 Norm of normed embedding: 1.000000
下表顯示不同維度的 MTEB 分數,這是嵌入內容常用的基準。值得注意的是,結果顯示效能並非與嵌入維度大小嚴格相關,較低的維度可達到與較高維度相當的分數。
MRL Dimension | MTEB 分數 |
---|---|
2048 | 68.16 |
1536 | 68.17 |
768 | 67.99 |
512 | 67.55 |
256 | 66.19 |
128 | 63.31 |
用途
文字嵌入是各種常見 AI 用途的關鍵,例如:
- 檢索增強生成 (RAG):嵌入項目可擷取相關資訊並納入模型背景脈絡,提升生成文字的品質。
資訊檢索:根據輸入文字,搜尋語意最相似的文字或文件。
搜尋結果重新排序:根據查詢對初始結果進行語意評分,優先顯示最相關的項目。
異常狀況偵測:比較嵌入群組有助於找出隱藏趨勢或離群值。
分類:根據內容自動分類文字,例如情緒分析或垃圾內容偵測
分群:建立嵌入項目的叢集和視覺化圖表,有效掌握複雜關係。
儲存嵌入
將嵌入投入生產時,通常會使用向量資料庫,有效率地儲存、建立索引及擷取高維度嵌入。Google Cloud 提供可供此用途的代管資料服務,包括 BigQuery、AlloyDB 和 Cloud SQL。
下列教學課程說明如何搭配 Gemini Embedding 使用其他第三方向量資料庫。
模型版本
屬性 | 說明 |
---|---|
模型代碼 | Gemini API
|
支援的資料類型 | 輸入功率 文字 輸出內容 文字嵌入 |
[*] | 代幣限制 輸入權杖限制 2,048 輸出尺寸大小 彈性,支援:128 - 3072,建議:768、1536、3072 |
個版本 |
|
最新更新 | 2025 年 6 月 |
負責任的使用行為通知
與生成新內容的生成式 AI 模型不同,Gemini Embedding 模型只會將輸入資料的格式轉換為數值表示法。Google 負責提供嵌入模型,將輸入資料的格式轉換為要求的數字格式,但使用者仍須全權負責輸入的資料和產生的嵌入內容。使用 Gemini Embedding 模型即代表你確認自己有權使用所上傳一切內容。請勿生成會侵害他人智慧財產或隱私權的內容。使用本服務時,請務必遵守《使用限制政策》和《Google 服務條款》。
開始使用嵌入功能建構內容
請參閱嵌入快速入門筆記本,瞭解模型功能,以及如何自訂和視覺化呈現嵌入。