Gemini API は、テキスト、画像、動画などのコンテンツのエンベディングを生成するエンベディング モデルを提供します。生成されたエンベディングは、セマンティック検索、分類、クラスタリングなどのタスクに使用できます。これにより、キーワード ベースのアプローチよりも正確でコンテキストを認識した結果が得られます。
最新のモデル gemini-embedding-2-preview は、Gemini API の最初のマルチモーダル エンベディング モデルです。テキスト、画像、動画、音声、ドキュメントを統合されたエンベディング空間にマッピングし、100 以上の言語でクロスモーダル検索、分類、クラスタリングを可能にします。詳しくは、マルチモーダル エンベディングのセクションをご覧ください。テキストのみのユースケースでは、gemini-embedding-001 は引き続き使用できます。
検索拡張生成(RAG)システムの構築は、AI プロダクトの一般的なユースケースです。エンベディングは、事実の正確性、一貫性、コンテキストの豊富さを向上させ、モデルの出力を大幅に強化するうえで重要な役割を果たします。マネージド RAG ソリューションを使用する場合は、RAG の管理を容易にし、費用対効果を高める ファイル検索ツールを使用できます。
エンベディングの生成
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 "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-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 "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"content": {
"parts": [
{
"text": "What is the meaning of life?"
},
{
"text": "How much wood would a woodchuck chuck?"
},
{
"text": "How does the brain work?"
}
]
},
"taskType": "SEMANTIC_SIMILARITY"
}'
パフォーマンスを改善するためにタスクタイプを指定する
エンベディングは、分類からドキュメント検索まで、幅広いタスクに使用できます。適切なタスクタイプを指定すると、目的の関係に合わせてエンベディングを最適化し、精度と効率を最大限に高めることができます。サポートされているタスクタイプの完全なリストについては、サポートされているタスクタイプの表をご覧ください。
次の例は、SEMANTIC_SIMILARITY を使用してテキスト文字列の意味がどの程度類似しているかを確認する方法を示しています。
Python
from google import genai
from google.genai import types
import pandas as pd
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 = client.models.embed_content(
model="gemini-embedding-001",
contents=texts,
config=types.EmbedContentConfig(task_type="SEMANTIC_SIMILARITY")
)
# Create a 3x3 table to show the similarity matrix
df = pd.DataFrame(
cosine_similarity([e.values for e in result.embeddings]),
index=texts,
columns=texts,
)
print(df)
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 "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"taskType": "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?"
}
]
}
}'
コード スニペットを実行すると、テキストの各チャンクが互いにどの程度類似しているかがわかります。
サポートされているタスクの種類
| 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-embedding-001 と gemini-embedding-2-preview はどちらも、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 'Content-Type: application/json' \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-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 スコアを示します。MTEB スコアは、エンベディングで一般的に使用されるベンチマークです。特に、結果は、パフォーマンスがエンベディング ディメンションのサイズに厳密に結び付いていないことを示しています。ディメンションが小さい場合でも、ディメンションが大きい場合と同等のスコアを達成しています。
| MRL ディメンション | MTEB スコア |
|---|---|
| 2048 | 68.16 |
| 1536 | 68.17 |
| 768 | 67.99 |
| 512 | 67.55 |
| 256 | 66.19 |
| 128 | 63.31 |
マルチモーダル エンベディング
gemini-embedding-2-preview モデルはマルチモーダル入力をサポートしているため、テキストとともに画像、動画、音声、ドキュメントのコンテンツを埋め込むことができます。すべてのモダリティが同じエンベディング空間にマッピングされるため、クロスモーダル検索と比較が可能になります。
サポートされているモダリティと制限事項
入力トークンの全体的な最大上限は 8,192 トークンです。
| モダリティ | 仕様と制限事項 |
|---|---|
| テキスト | 最大 8,192 個のトークンをサポートします。 |
| 画像 | リクエストごとに最大 6 枚の画像。サポートされている形式: PNG、JPEG。 |
| 音声 | 最大再生時間は 80 秒です。サポートされている形式: MP3、WAV。 |
| 動画 | 最大再生時間は 128 秒です。サポートされている形式: MP4、MOV、サポートされているコーデック: H264、H265、AV1、VP9 |
| ドキュメント(PDF) | 最大 6 ページ。 |
画像を埋め込む
次の例は、gemini-embedding-2-preview を使用して画像を埋め込む方法を示しています。
画像は、インライン データとして、または Files API を介してアップロードされたファイルとして提供できます。
Python
from google import genai
from google.genai import types
with open('example.png', 'rb') as f:
image_bytes = f.read()
client = genai.Client()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/png',
),
]
)
print(result.embeddings)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const imgBase64 = fs.readFileSync("example.png", { encoding: "base64" });
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: [{
inlineData: {
mimeType: 'image/png',
data: imgBase64,
},
}],
});
console.log(response.embeddings);
}
main();
REST
IMG_PATH="/path/to/your/image.png"
IMG_BASE64=$(base64 -w0 "${IMG_PATH}")
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{
"content": {
"parts": [{
"inline_data": {
"mime_type": "image/png",
"data": "'"${IMG_BASE64}"'"
}
}]
}
}'
エンベディングの集計
マルチモーダル コンテンツを扱う場合、入力の構造はエンベディング出力に影響します。
- 単一のコンテンツ エントリ: 単一のコンテンツ エントリ内で複数の部分(テキストや画像など)を送信すると、そのエントリ内のすべてのモダリティに対して 1 つの集約されたエンベディングが生成されます。
- 複数のエントリ:
contents配列で複数のエントリを送信すると、エントリごとに個別のエンベディングが返されます。 - 投稿レベルの表現: 複数のメディア アイテムを含むソーシャル メディア投稿などの複雑なオブジェクトの場合は、個別のエンベディングを(平均化するなどして)集約し、一貫性のある投稿レベルの表現を作成することをおすすめします。
次の例は、テキスト入力と画像入力に対して 1 つの集約エンベディングを作成する方法を示しています。parts フィールドを使用して、複数の入力を結合します。
Python
from google import genai
from google.genai import types
with open('dog.png', 'rb') as f:
image_bytes = f.read()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=[
types.Content(
parts=[
types.Part(text="An image of a dog"),
types.Part.from_bytes(
data=image_bytes,
mime_type='image/png',
)
]
)
]
)
# This produces one embedding
for embedding in result.embeddings:
print(embedding.values)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const imgBase64 = fs.readFileSync("dog.png", { encoding: "base64" });
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: {
parts: [
{ text: 'An image of a dog' },
{ inlineData: { mimeType: 'image/png', data: imgBase64 } },
],
},
});
console.log(response.embeddings);
}
main();
REST
IMG_PATH="/path/to/your/dog.png"
IMG_BASE64=$(base64 -w0 "${IMG_PATH}")
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{
"content": {
"parts": [
{"text": "An image of a dog"},
{
"inline_data": {
"mime_type": "image/png",
"data": "'"${IMG_BASE64}"'"
}
}
]
}
}'
一方、次の例では、1 回のエンベディング呼び出しで複数のエンベディングを作成します。
Python
from google import genai
from google.genai import types
with open('dog.png', 'rb') as f:
image_bytes = f.read()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=[
"The dog is cute",
types.Part.from_bytes(
data=image_bytes,
mime_type='image/png',
),
]
)
# This produces two embeddings
for embedding in result.embeddings:
print(embedding.values)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const imgBase64 = fs.readFileSync("dog.png", { encoding: "base64" });
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: [
'The dog is cute',
{
inlineData: {
mimeType: 'image/png',
data: imgBase64,
},
},
],
});
console.log(response.embeddings);
}
main();
REST
IMG_PATH="/path/to/your/dog.png"
IMG_BASE64=$(base64 -w0 "${IMG_PATH}")
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:batchEmbedContents" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{
"requests": [
{
"model": "models/gemini-embedding-2-preview",
"content": {"parts": [{"text": "The dog is cute"}]}
},
{
"model": "models/gemini-embedding-2-preview",
"content": {"parts": [{"inline_data": {"mime_type": "image/png", "data": "'"${IMG_BASE64}"'"}}]}
}
]
}'
音声を埋め込む
次の例は、gemini-embedding-2-preview を使用して音声ファイルを埋め込む方法を示しています。
音声ファイルは、インライン データとして提供することも、Files API を介してアップロードされたファイルとして提供することもできます。
Python
from google import genai
from google.genai import types
with open('example.mp3', 'rb') as f:
audio_bytes = f.read()
client = genai.Client()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=[
types.Part.from_bytes(
data=audio_bytes,
mime_type='audio/mpeg',
),
]
)
print(result.embeddings)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const audioBase64 = fs.readFileSync("example.mp3", { encoding: "base64" });
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: [{
inlineData: {
mimeType: 'audio/mpeg',
data: audioBase64,
},
}],
});
console.log(response.embeddings);
}
main();
REST
AUDIO_PATH="/path/to/your/example.mp3"
AUDIO_BASE64=$(base64 -w0 "${AUDIO_PATH}")
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{
"content": {
"parts": [{
"inline_data": {
"mime_type": "audio/mpeg",
"data": "'"${AUDIO_BASE64}"'"
}
}]
}
}'
動画の埋め込み
次の例は、gemini-embedding-2-preview を使用して動画を埋め込む方法を示しています。
動画は、インライン データとして提供することも、Files API を介してアップロードされたファイルとして提供することもできます。
Python
from google import genai
from google.genai import types
client = genai.Client()
with open('example.mp4', 'rb') as f:
video_bytes = f.read()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=[
types.Part.from_bytes(
data=video_bytes,
mime_type='video/mp4',
),
]
)
print(result.embeddings[0].values)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const videoBase64 = fs.readFileSync("example.mp4", { encoding: "base64" });
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: [{
inlineData: {
mimeType: 'video/mp4',
data: videoBase64,
},
}],
});
console.log(response.embeddings);
}
main();
REST
VIDEO_PATH="/path/to/your/video.mp4"
VIDEO_BASE64=$(base64 -w0 "${VIDEO_PATH}")
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{
"content": {
"parts": [{
"inline_data": {
"mime_type": "video/mp4",
"data": "'"${VIDEO_BASE64}"'"
}
}]
}
}'
128 秒を超える動画を埋め込む必要がある場合は、動画を重複するセグメントに分割し、それらのチャンクを個別に埋め込むことができます。
ドキュメントのエンベディング
PDF 形式のドキュメントは直接埋め込むことができます。モデルは、各ページのビジュアル コンテンツとテキスト コンテンツを処理します。
PDF は、インライン データとして、または Files API を介してアップロードされたファイルとして提供できます。
Python
from google import genai
from google.genai import types
with open('example.pdf', 'rb') as f:
pdf_bytes = f.read()
client = genai.Client()
result = client.models.embed_content(
model='gemini-embedding-2-preview',
contents=[
types.Part.from_bytes(
data=pdf_bytes,
mime_type='application/pdf',
),
]
)
print(result.embeddings)
JavaScript
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";
async function main() {
const ai = new GoogleGenAI({});
const pdfBase64 = fs.readFileSync("example.pdf", { encoding: "base64" });
const response = await ai.models.embedContent({
model: 'gemini-embedding-2-preview',
contents: [{
inlineData: {
mimeType: 'application/pdf',
data: pdfBase64,
},
}],
});
console.log(response.embeddings);
}
main();
REST
PDF_PATH="/path/to/your/example.pdf"
PDF_BASE64=$(base64 -w0 "${PDF_PATH}")
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2-preview:embedContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${GEMINI_API_KEY}" \
-d '{
"content": {
"parts": [{
"inline_data": {
"mime_type": "application/pdf",
"data": "'"${PDF_BASE64}"'"
}
}]
}
}'
ユースケース
テキスト エンベディングは、次のような一般的な AI のユースケースで重要です。
- 検索拡張生成(RAG): エンベディングは、関連情報を取得してモデルのコンテキストに組み込むことで、生成されたテキストの品質を高めます。
情報検索: 入力テキストが与えられたときに、意味的に最も類似したテキストまたはドキュメントを検索します。
検索結果の再ランキング: クエリに対して初期結果を意味的にスコアリングすることで、最も関連性の高いアイテムを優先します。
異常検出: エンベディングのグループを比較すると、隠れた傾向や外れ値を特定できます。
分類: 感情分析やスパム検出など、コンテンツに基づいてテキストを自動的に分類します。
クラスタリング: エンベディングのクラスタと可視化を作成して、複雑な関係を効果的に把握します。
エンベディングの保存
エンベディングを本番環境に移行する場合は、ベクトル データベースを使用して、高次元エンベディングを効率的に保存、インデックス登録、取得するのが一般的です。Google Cloud は、この目的で使用できるマネージド データサービス(BigQuery、AlloyDB、Cloud SQL など)を提供しています。
次のチュートリアルでは、Gemini Embedding で他のサードパーティのベクトル データベースを使用する方法について説明します。
モデル バージョン
Gemini エンベディング 2 プレビュー
| プロパティ | 説明 |
|---|---|
| モデルコード |
Gemini API
|
| サポートされるデータタイプ |
入力 テキスト、画像、動画、音声、PDF 出力 テキスト エンベディング |
| トークンの上限[*] |
入力トークンの上限 8,192 出力ディメンションのサイズ 柔軟、サポート: 128 ~ 3072、推奨: 768、1536、3072 |
| バージョン |
|
| の最新の更新 | 2025 年 11 月 |
Gemini エンベディング
| プロパティ | 説明 |
|---|---|
| モデルコード |
Gemini API
|
| サポートされるデータタイプ |
入力 テキスト 出力 テキスト エンベディング |
| トークンの上限[*] |
入力トークンの上限 2,048 出力ディメンションのサイズ 柔軟、サポート: 128 ~ 3072、推奨: 768、1536、3072 |
| バージョン |
|
| の最新の更新 | 2025 年 6 月 |
非推奨のエンベディング モデルについては、非推奨のページをご覧ください。
gemini-embedding-001 からの移行
gemini-embedding-001 と gemini-embedding-2-preview の間のエンベディング スペースは互換性がありません。つまり、あるモデルで生成されたエンベディングを、別のモデルで生成されたエンベディングと直接比較することはできません。gemini-embedding-2-preview にアップグレードする場合は、既存のデータをすべて再エンベディングする必要があります。
バッチ エンベディング
レイテンシが問題にならない場合は、Batch API で Gemini Embeddings モデルを使用してみてください。これにより、デフォルトのエンベディング料金の 50% でスループットを大幅に向上させることができます。Batch API クックブックで、開始方法の例をご覧ください。
責任ある使用に関する通知
新しいコンテンツを作成する生成 AI モデルとは異なり、Gemini エンベディング モデルは、入力データの形式を数値表現に変換することのみを目的としています。Google は、入力データの形式をリクエストされた数値形式に変換するエンベディング モデルを提供する責任を負いますが、ユーザーは入力したデータと結果のエンベディングに対する全責任を負います。Gemini エンベディング モデルを使用することにより、アップロードするコンテンツに対して必要な権利を有することを確認したと見なされます。他者の知的財産やプライバシーの権利を侵害するコンテンツを生成することはできません。このサービスの使用には、Google の使用禁止に関するポリシーと利用規約が適用されます。
エンベディングを使用して構築を開始する
エンベディングのクイックスタート ノートブックで、モデルの機能を確認し、エンベディングをカスタマイズして可視化する方法を学習します。