嵌入

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)

JavaScript

import { GoogleGenAI } from "@google/genai";

async function main() {

    const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

    const response = await ai.models.embedContent({
        model: 'gemini-embedding-exp-03-07',
        contents: 'What is the meaning of life?',
    });

    console.log(response.embeddings);
}

main();

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)

REST

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?"}]}
    }'

您也可以將多個區塊傳入做為字串清單,一次產生多個區塊的嵌入資料。

工作類型

建構檢索增強生成 (RAG) 系統時,常見的設計是使用文字嵌入來執行相似搜尋。在某些情況下,這可能會導致品質下降,因為問題和答案在語意上並不相似。舉例來說,「為什麼天空是藍色的?」這類問題和「陽光散射造成天空呈現藍色」這類答案,在陳述句中具有截然不同的意義,因此 RAG 系統不會自動辨識兩者之間的關聯。

任務類型可讓您為特定任務產生最佳化嵌入資料,節省時間和成本,並提升效能。

Python

from google import genai
from google.genai import types

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?",
        config=types.EmbedContentConfig(task_type="SEMANTIC_SIMILARITY")
)
print(result.embeddings)

JavaScript

import { GoogleGenAI } from "@google/genai";

async function main() {

    const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

    const response = await ai.models.embedContent({
        model: 'gemini-embedding-exp-03-07',
        contents: 'What is the meaning of life?',
        config: {
            taskType: "SEMANTIC_SIMILARITY",
        }
    });

    console.log(response.embeddings);
}

main();

REST

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?"}]},
     "taskType": "SEMANTIC_SIMILARITY"
    }'

支援的工作類型

工作類型 說明
SEMANTIC_SIMILARITY 用於產生經過最佳化處理的嵌入值,以便評估文字相似度。
CLASSIFICATION 用於產生經過最佳化的嵌入資料,以便根據預先設定的標籤分類文字。
CLUSTERING 用於產生經過最佳化的嵌入資料,以便根據相似性將文字分組。
RETRIEVAL_DOCUMENTRETRIEVAL_QUERYQUESTION_ANSWERINGFACT_VERIFICATION 用於產生專為文件搜尋或資訊擷取最佳化的嵌入項目。
CODE_RETRIEVAL_QUERY 用於根據自然語言查詢語法擷取程式碼區塊,例如排序陣列或反轉連結清單。系統會使用 RETRIEVAL_DOCUMENT 計算程式碼區塊的嵌入內容。

用途

文字嵌入可用於各種常見的 AI 用途,例如:

嵌入模型

Gemini API 提供三種生成文字嵌入資料的模型:

後續步驟

請查看嵌入式資料快速入門筆記本