Gemini API のエンベディング

Gemini API は、単語、フレーズ、コード、文の最先端のエンベディングを生成するさまざまなエンベディング モデルをサポートしています。生成されたエンベディングは、セマンティック検索、テキスト分類、クラスタリングなどのタスクに使用できます。

エンベディングとは

エンベディングは意味論的意味とコンテキストをキャプチャするため、意味が類似するテキストはエンベディングが「近い」ことになります。たとえば、「犬を動物病院に連れて行った」と「猫を動物病院に連れて行った」という文は、どちらも類似したコンテキストを記述しているため、ベクトル空間内でエンベディングが互いに近くなります。

エンベディングを使用すると、さまざまなテキストを比較して、それらの関連性を把握できます。たとえば、テキスト「cat」と「dog」のエンベディングが近い場合、これらの単語は意味、コンテキスト、またはその両方で類似していると推測できます。これにより、さまざまな一般的な AI ユースケースが可能になります。

エンベディングを生成する

embedContent メソッドを使用してテキスト エンベディングを生成します。

Python

from google import genai

client = genai.Client(api_key="GEMINI_API_KEY")

result = client.models.embed_content(
        model="gemini-embedding-exp-03-07",
        contents="What is the meaning of life?")

print(result.embeddings)

Node.js

const { GoogleGenerativeAI } = require("@google/generative-ai");

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-embedding-exp-03-07"});

async function run() {
    const result = await model.embedContent("What is the meaning of life?");
    console.log(result.embedding.values);
}

run();

curl

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-exp-03-07:embedContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"model": "models/gemini-embedding-exp-03-07",
     "content": {
     "parts":[{
     "text": "What is the meaning of life?"}]}
    }'

Go

ctx := context.Background()

client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
    log.Fatal(err)
}
defer client.Close()

em := client.EmbeddingModel("gemini-embedding-exp-03-07")
res, err := em.EmbedContent(ctx, genai.Text("What is the meaning of life?"))

if err != nil {
    panic(err)
}
fmt.Println(res.Embedding.Values)

ユースケース

テキスト エンベディングは、次のようなさまざまな一般的な AI ユースケースで使用されます。

エンベディング モデル

Gemini API には、テキスト エンベディングを生成する次の 3 つのモデルがあります。

今後数か月以内に、Gemini エンベディング モデルの更新版をリリースする予定です。