メインコンテンツまでスキップ
に最終更新

EMBED

説明​

テキストまたはマルチモーダル入力の埋め込みベクトルを生成します。このベクトルは、類似度計算、検索、その他のシナリオに使用できます。

注記

マルチモーダル埋め込みは Apache Doris 4.1.4 以降でサポートされます。

マルチモーダル埋め込みでは、画像、動画、音声ファイルを記述する JSON オブジェクトを渡します。メディアタイプを使用する前に、構成済みの AI プロバイダーとモデルがそのタイプをサポートしていることを確認してください。

マルチモーダル埋め込みをサポートするプロバイダー​

プロバイダーサポートされるメディアタイプ
voyageai画像(image/)、動画(video/)
qwen画像(image/)、動画(video/)
jina画像(image/)、動画(video/)
gemini画像(image/)、動画(video/)、音声(audio/)

他のプロバイダーでマルチモーダル埋め込みを呼び出すと、<provider> does not support multimodal Embed feature. が報告されます。プロバイダーがサポートしていないメディアタイプを渡すと、<provider> only supports <types> multimodal embed, got <type> が報告されます。

構文​

バージョン 4.1.5 以降、AI リソースでは、テキスト埋め込みとマルチモーダル埋め込みに対して、それぞれ ai.embed.* と ai.embed.mm.* のモデルグループを構成できます。テキスト入力ではテキスト埋め込みグループ、次に汎用グループが選択されます。JSON マルチモーダル入力では、マルチモーダル埋め込みグループ、テキスト埋め込みグループ、汎用グループの順に選択されます。構成要件については、AI リソースの構成 を参照してください。

EMBED([<resource_name>], <input>)

パラメータ​

パラメータ説明
<resource_name>省略可能。埋め込みの生成に使用する AI リソースです。省略すると、Doris はセッション変数 default_ai_resource を使用します。
<input>埋め込むコンテンツです。テキスト埋め込みの場合は文字列、マルチモーダル埋め込みの場合は JSON オブジェクトを指定できます。

マルチモーダル JSON オブジェクトには、次のフィールドが含まれます。

フィールド必須説明
uriはいhttp:// または https:// URL、あるいは s3://<bucket>/<key> 形式の S3 互換 URI。
content_typeはいメディアの MIME タイプ。image/、video/、audio/ で始まる値が認識されます。AI プロバイダーとモデルは、そのメディアタイプをサポートしている必要があります。
endpointS3 互換 URI の場合S3 互換ストレージのエンドポイント。
regionS3 互換 URI の場合ストレージリージョン。
akいいえAK/SK 認証に使用するアクセスキー。
skいいえAK/SK 認証に使用するシークレットキー。
role_arnいいえロールベース認証に使用する IAM ロール ARN。
external_idいいえIAM ロールを引き受けるときに使用する外部 ID。

HTTP または HTTPS URL の場合、必要なのは uri と content_type だけです。S3 互換 URI の場合、Doris はストレージ認証情報を使用して署名付き URL を生成します。AK/SK 認証と IAM ロール認証の両方がサポートされます。

署名付き URL の有効期間はセッション変数 file_presigned_url_ttl_seconds で秒単位に制御され、デフォルトは 3600(1 時間)です。メディアファイルが大きく、プロバイダーによる取得に時間がかかる場合は、この値を増やしてください。

SET file_presigned_url_ttl_seconds = 7200;

テキスト入力の場合、EMBED は、構成されていればリソースの完全な ai.embed.* 接続グループを使用し、それ以外の場合は汎用の ai.* グループを使用します。JSON マルチモーダル入力の場合、選択順序は ai.embed.mm.*、ai.embed.*、汎用の ai.* です。各グループには endpoint、provider_type、model_name、api_key(LOCAL の場合は省略可能)が含まれます。選択は完全なグループ単位で行われ、選択されたプロバイダーとモデルは入力タイプをサポートしている必要があります。埋め込みプロパティだけを含むリソースは、対応する EMBED 呼び出しに使用できます。専用の埋め込みプロパティ を参照してください。

戻り値​

戻り値の型は ARRAY<FLOAT> で、生成されたベクトルを表します。

入力値が NULL の場合は NULL を返します。

ベクトルの次元数と値は、AI プロバイダーとモデルによって異なります。

マルチモーダルの例​

次の例では、HTTPS URL から利用できる画像の埋め込みを生成します。

SELECT ARRAY_SIZE(
EMBED(
'multimodal_embed_resource',
CAST('{
"uri": "https://example.com/images/product.png",
"content_type": "image/png"
}' AS JSON)
)
) AS image_embedding_dimension;
+---------------------------+
| image_embedding_dimension |
+---------------------------+
| 2560 |
+---------------------------+

次の例では、AK/SK 認証を使用して S3 互換ストレージから動画を読み取ります。

SELECT ARRAY_SIZE(
EMBED(
'multimodal_embed_resource',
CAST('{
"uri": "s3://example-bucket/videos/demo.mp4",
"content_type": "video/mp4",
"endpoint": "s3.us-east-1.amazonaws.com",
"region": "us-east-1",
"ak": "<access_key>",
"sk": "<secret_key>"
}' AS JSON)
)
) AS video_embedding_dimension;
+---------------------------+
| video_embedding_dimension |
+---------------------------+
| 2560 |
+---------------------------+

AK/SK の代わりに IAM ロール認証を使用することもできます。次の例では、画像の埋め込みを生成します。

SELECT ARRAY_SIZE(
EMBED(
'multimodal_embed_resource',
CAST('{
"uri": "s3://example-bucket/images/product.png",
"content_type": "image/png",
"endpoint": "s3.us-east-1.amazonaws.com",
"region": "us-east-1",
"role_arn": "arn:aws:iam::<account_id>:role/<role_name>",
"external_id": "<external_id>"
}' AS JSON)
)
) AS image_embedding_dimension;
+---------------------------+
| image_embedding_dimension |
+---------------------------+
| 3072 |
+---------------------------+

上記の結果は例です。実際の埋め込み次元数は、構成されたプロバイダーとモデルによって異なります。

テキストの例​

次の表は、ある会社の行動規範を模したものです。

CREATE TABLE knowledge_base (
id BIGINT,
title STRING,
content STRING,
embedding ARRAY<FLOAT> COMMENT 'Generated embedding vectors by the EMBED function'
)
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 4
PROPERTIES (
"replication_num" = "1"
);

SET default_ai_resource = 'embed_resource_name';

-- `embedding` is the embedding vector generated by the function EMBED according to the corresponding tag of the content.
INSERT INTO knowledge_base (id, title, content, embedding) VALUES
(1, "Travel Reimbursement Policy",
"Employees must submit a reimbursement request within 7 days after the business trip, with invoices and travel approval attached.",
EMBED("travel reimbursement policy")),
(2, "Leave Policy",
"Employees must apply for leave in the system in advance. If the leave is longer than three days, approval from the direct manager is required.",
EMBED("leave request policy")),
(3, "VPN User Guide",
"To access the internal network, employees must use VPN. For the first login, download and install the client and configure the certificate.",
EMBED("VPN guide intranet access")),
(4, "Meeting Room Reservation",
"Meeting rooms can be reserved in advance through the OA system, with time and number of participants specified.",
EMBED("meeting room booking reservation")),
(5, "Procurement Request Process",
"Departments must fill out a procurement request form for purchasing items. If the amount exceeds $5000, financial approval is required.",
EMBED("procurement request process finance"));

テキストをベクトル化することで、次のような操作を実行できます。

  1. 質疑応答検索(COSINE_DISTANCE を使用)
SELECT
id, title, content,
COSINE_DISTANCE(embedding, EMBED("How to apply for travel reimbursement?")) AS score
FROM knowledge_base
ORDER BY score ASC
LIMIT 2;
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| id | title | content | score |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| 1 | Travel Reimbursement Policy | Employees must submit a reimbursement request within 7 days after the business trip, with invoices and travel approval attached. | 0.4463210454563673 |
| 5 | Procurement Request Process | Departments must fill out a procurement request form for purchasing items. If the amount exceeds $5000, financial approval is required. | 0.5726841578491431 |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+--------------------+
  1. 問題分析のマッチング(L2_DISTANCE を使用)
SELECT
id, title, content,
L2_DISTANCE(embedding, EMBED("How to access the company intranet")) AS distance
FROM knowledge_base
ORDER BY distance ASC
LIMIT 2;
+------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| id | title | content | distance |
+------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| 3 | VPN User Guide | To access the internal network, employees must use VPN. For the first login, download and install the client and configure the certificate. | 0.5838271122253775 |
| 1 | Travel Reimbursement Policy | Employees must submit a reimbursement request within 7 days after the business trip, with invoices and travel approval attached. | 1.272394695975331 |
+------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
  1. 記事の内容に基づくテキスト関連性のマッチングと推薦(INNER PRODUCT を使用)
SELECT
id, title, content,
INNER_PRODUCT(embedding, EMBED("Leave system request leader approval")) AS score
FROM knowledge_base
WHERE id != 2
ORDER BY score DESC
LIMIT 2;
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| id | title | content | score |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| 5 | Procurement Request Process | Departments must fill out a procurement request form for purchasing items. If the amount exceeds $5000, financial approval is required. | 0.33268885332504 |
| 4 | Meeting Room Reservation | Meeting rooms can be reserved in advance through the OA system, with time and number of participants specified. | 0.29224032230852487 |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+---------------------+
  1. 差異が最小のコンテンツを検索(L1_DISTANCE を使用)
SELECT
id, title, content,
L1_DISTANCE(embedding, EMBED("Procurement application process")) AS distance
FROM knowledge_base
ORDER BY distance ASC
LIMIT 3;
+------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| id | title | content | distance |
+------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| 5 | Procurement Request Process | Departments must fill out a procurement request form for purchasing items. If the amount exceeds $5000, financial approval is required. | 18.66882028897362 |
| 4 | Meeting Room Reservation | Meeting rooms can be reserved in advance through the OA system, with time and number of participants specified. | 30.90449328294426 |
| 2 | Leave Policy | Employees must apply for leave in the system in advance. If the leave is longer than three days, approval from the direct manager is required. | 31.060405636536416 |
+------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+