Gemini API は、単語、フレーズ、文、コードのエンベディングを生成するテキスト エンベディング モデルを提供します。これらの基盤となるエンベディングは、セマンティック検索、分類、クラスタリングなどの高度な NLP タスクを強化し、キーワード ベースのアプローチよりも正確でコンテキスト アウェアな結果を提供します。
検索拡張生成(RAG)システムの構築は、エンベディングの一般的なユースケースです。エンベディングは、事実の正確性、一貫性、コンテキストの豊富さを向上させ、モデル出力を大幅に強化するうえで重要な役割を果たします。エンベディングで表される知識ベースから関連情報を効率的に取得し、入力プロンプトの追加コンテキストとして言語モデルに渡すことで、より多くの情報に基づいた正確なレスポンスを生成するように誘導します。
使用可能なエンベディング モデル バリエーションの詳細については、モデルのバージョンをご覧ください。スループットの高いサービングを半額で実現するには、Batch API エンベディングをお試しください。
エンベディングの生成
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:batchEmbedContents" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"requests": [{ "model": "models/gemini-embedding-001", "content": { "parts":[{ "text": "What is the meaning of life?"}]}, }, { "model": "models/gemini-embedding-001", "content": { "parts":[{ "text": "How much wood would a woodchuck chuck?"}]}, }, { "model": "models/gemini-embedding-001", "content": { "parts":[{ "text": "How does the brain work?"}]}, }, ]}' 2> /dev/null | grep -C 5 values ``` パフォーマンスを改善するためにタスクタイプを指定する
エンベディングは、分類からドキュメント検索まで、幅広いタスクに使用できます。適切なタスクタイプを指定すると、意図した関係に合わせてエンベディングを最適化し、精度と効率を最大限に高めることができます。サポートされているタスクタイプの完全なリストについては、サポートされているタスクタイプの表をご覧ください。
次の例は、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 "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent" \ -H "x-goog-api-key: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"task_type": "SEMANTIC_SIMILARITY", "content": { "parts":[{ "text": "What is the meaning of life?"}, {"text": "How much wood would a woodchuck chuck?"}, {"text": "How does the brain work?"}]} }' 次のコード スニペットの出力例を次に示します。
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 サポートされているタスクの種類
| Task type | 説明 | 例 |
|---|---|---|
| SEMANTIC_SIMILARITY | テキストの類似性を評価するために最適化されたエンベディング。 | レコメンデーション システム、重複検出 |
| 分類 | 事前設定されたラベルに従ってテキストを分類するように最適化されたエンベディング。 | 感情分析、スパム検出 |
| クラスタリング | 類似性に基づいてテキストをクラスタ化するように最適化されたエンベディング。 | ドキュメントの整理、市場調査、異常検出 |
| RETRIEVAL_DOCUMENT | ドキュメント検索用に最適化されたエンベディング。 | 検索用に記事、書籍、ウェブページをインデックス登録する。 |
| RETRIEVAL_QUERY | 一般的な検索クエリ用に最適化されたエンベディング。クエリには RETRIEVAL_QUERY を使用し、取得するドキュメントには RETRIEVAL_DOCUMENT を使用します。 | カスタム検索 |
| CODE_RETRIEVAL_QUERY | 自然言語クエリに基づくコードブロックの取得に最適化されたエンベディング。クエリには CODE_RETRIEVAL_QUERY、取得するコードブロックには RETRIEVAL_DOCUMENT を使用します。 | コードの候補と検索 |
| QUESTION_ANSWERING | 質問に回答するドキュメントの検索に最適化された、質問応答システムの質問のエンベディング。質問には QUESTION_ANSWERING を使用し、取得するドキュメントには RETRIEVAL_DOCUMENT を使用します。 | チャットボックス |
| FACT_VERIFICATION | 検証が必要なステートメントのエンベディング。ステートメントを裏付ける証拠または反論する証拠を含むドキュメントの取得に最適化されています。ターゲット テキストには FACT_VERIFICATION を使用し、取得するドキュメントには RETRIEVAL_DOCUMENT を使用します。 | 自動ファクト チェック システム |
埋め込みサイズの制御
Gemini エンベディング モデル gemini-embedding-001 は、Matryoshka Representation Learning(MRL)手法を使用してトレーニングされます。この手法では、同じデータの有用でシンプルなバージョンである初期セグメント(または接頭辞)を持つ高次元エンベディングを学習するようにモデルをトレーニングします。
output_dimensionality パラメータを使用して、出力エンベディング ベクトルのサイズを制御します。出力のディメンションを小さくすると、ストレージ スペースを節約し、ダウンストリーム アプリケーションの計算効率を高めることができます。品質の低下はわずかです。デフォルトでは 3,072 次元のエンベディングが出力されますが、品質を損なうことなくサイズを小さくして、保存容量を節約できます。出力ディメンションには 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: $GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "content": {"parts":[{ "text": "What is the meaning of life?"}]}, "output_dimensionality": 768 }' コード スニペットからの出力例:
Length of embedding: 768 小さい寸法での品質の確保
3,072 次元のエンベディングは正規化されます。正規化されたエンベディングは、ベクトルの大きさではなく方向を比較することで、より正確なセマンティック類似度を生成します。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 ディメンション | 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 月 |
バッチ エンベディング
レイテンシが問題にならない場合は、Batch API で Gemini Embeddings モデルを使用してみてください。これにより、インタラクティブ エンベディングの料金の 50% で、スループットを大幅に向上させることができます。スタートガイドの例については、Batch API クックブックをご覧ください。
責任ある使用に関する通知
新しいコンテンツを作成する生成 AI モデルとは異なり、Gemini エンベディング モデルは、入力データの形式を数値表現に変換することのみを目的としています。Google は、入力データの形式をリクエストされた数値形式に変換するエンベディング モデルを提供する責任を負いますが、ユーザーは入力したデータと結果のエンベディングに対する全責任を負います。Gemini エンベディング モデルを使用することにより、アップロードするコンテンツに対して必要な権利を有することを確認したと見なされます。他者の知的財産やプライバシーの権利を侵害するコンテンツを生成することはできません。このサービスのご利用には、使用禁止に関するポリシーと Google の利用規約が適用されます。
エンベディングを使用して構築を開始する
エンベディングのクイックスタート ノートブックで、モデルの機能を確認し、エンベディングをカスタマイズして可視化する方法を学習します。
以前のモデルの利用非推奨のお知らせ
以下のモデルは 2025 年 10 月に非推奨になります。 - embedding-001 - embedding-gecko-001 - gemini-embedding-exp-03-07(gemini-embedding-exp)