エンベディング

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 を使用します。 チャットボックス
FACT_VERIFICATION 検証が必要なステートメントのエンベディング。ステートメントを裏付ける証拠または反論する証拠を含むドキュメントの取得に最適化されています。ターゲット テキストには FACT_VERIFICATION を使用し、取得するドキュメントには RETRIEVAL_DOCUMENT を使用します。 自動ファクト チェック システム

エンベディング サイズの制御

Gemini エンベディング モデル gemini-embedding-001 は、Matryoshka Representation Learning(MRL)手法を使用してトレーニングされます。この手法では、モデルに、同じデータの有用でシンプルなバージョンである初期セグメント(または接頭辞)を持つ高次元エンベディングを学習させます。3, 072 次元のエンベディング全体を使用することも、品質を損なうことなくより小さなサイズに切り捨ててストレージ容量を節約することもできます。最適な品質を得るには、最初の 768 と 1536 を使用することをおすすめします。

output_dimensionality パラメータを使用すると、出力エンベディング ベクトルのサイズを制御できます。出力のディメンションを小さくすると、品質をほとんど損なうことなく、ストレージ スペースを節約し、ダウンストリーム アプリケーションの計算効率を高めることができます。

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

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

ユースケース

テキスト エンベディングは、次のような一般的な AI のユースケースで重要です。

エンベディングの保存

エンベディングを本番環境に移行する際は、ベクトル データベースを使用して、高次元エンベディングを効率的に保存、インデックス登録、取得するのが一般的です。Google Cloud には、この目的で使用できるマネージド データサービス(BigQueryAlloyDBCloud SQL など)が用意されています。

次のチュートリアルでは、Gemini Embedding で他のサードパーティのベクトル データベースを使用する方法について説明します。

エンベディング モデル

一般提供モデル

以前のモデル

  • embedding-001(2025 年 8 月 14 日に非推奨)
  • text-embedding-004(2026 年 1 月 14 日にサポート終了)

エンベディングの使用

新しいコンテンツを作成する生成 AI モデルとは異なり、Gemini エンベディング モデルは、入力データの形式を数値表現に変換することのみを目的としています。Google は、入力データの形式をリクエストされた数値形式に変換するエンベディング モデルを提供する責任を負いますが、ユーザーは入力したデータと結果のエンベディングに対する全責任を負います。Gemini エンベディング モデルを使用することにより、アップロードするコンテンツに対して必要な権利を有することを確認したと見なされます。他者の知的財産権やプライバシー権を侵害するコンテンツを生成することはできません。このサービスのご利用には、Google の使用禁止に関するポリシー利用規約が適用されます。

エンベディングを使用して構築を開始する

エンベディングのクイックスタート ノートブックで、モデルの機能を確認し、エンベディングをカスタマイズして可視化する方法を学習してください。