EMBED
説明
入力テキストに基づいてセマンティック埋め込みベクトルを生成し、テキストのセマンティック情報を表現します。類似度計算、検索、その他のシナリオで使用できます。
構文
EMBED([<resource_name>], <text>)
パラメータ
| Parameter | デスクリプション |
|---|---|
<resource_name> | 指定されたリソース名 |
<text> | 埋め込みベクトルを生成するテキスト |
戻り値
戻り値の型はARRAY
入力値がNULLの場合、NULLを返します。
結果は大規模言語モデルによって生成されるため、返されるコンテンツは固定ではありません。
例
以下の表は、会社の行動規範をシミュレートしています。
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"));
テキストをベクトル化することで、以下のような操作を実行できます:
- 質問応答検索(
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 |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+--------------------+
- 問題解析マッチング(
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 |
+------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
- 記事コンテンツに基づくテキスト関連性マッチングと推薦(
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 |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+---------------------+
- 最小差分でコンテンツを検索する(
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 |
+------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+