EMBED
Description
Generates an embedding vector for text or multimodal input. The vector can be used for similarity calculation, retrieval, and other scenarios.
For multimodal embedding, pass a JSON object that describes an image, video, or audio file. Before using a media type, confirm that the configured AI provider and model support it.
Syntax
EMBED([<resource_name>], <input>)
Parameters
| Parameter | Description |
|---|---|
<resource_name> | Optional. The AI resource used to generate the embedding. If omitted, Doris uses the session variable default_ai_resource. |
<input> | The content to embed. It can be a string for text embedding, or a JSON object for multimodal embedding. |
The multimodal JSON object contains the following fields:
| Field | Required | Description |
|---|---|---|
uri | Yes | An http:// or https:// URL, or an S3-compatible URI in the form s3://<bucket>/<key>. |
content_type | Yes | The media MIME type. Values beginning with image/, video/, or audio/ are recognized. The AI provider and model must support that media type. |
endpoint | For S3-compatible URIs | The S3-compatible storage endpoint. |
region | For S3-compatible URIs | The storage region. |
ak | No | Access key for AK/SK authentication. |
sk | No | Secret key for AK/SK authentication. |
role_arn | No | IAM role ARN for role-based authentication. |
external_id | No | External ID used when assuming the IAM role. |
For an HTTP or HTTPS URL, only uri and content_type are required. For an S3-compatible URI, Doris uses the storage credentials to generate a presigned URL. Both AK/SK and IAM role authentication are supported.
Return Value
The return type is ARRAY<FLOAT>, representing the generated vector.
Returns NULL if the input value is NULL.
The vector dimension and values depend on the AI provider and model.
Multimodal Examples
The following example generates an embedding for an image available through an 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 |
+---------------------------+
The following example reads a video from S3-compatible storage by using AK/SK authentication:
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 |
+---------------------------+
IAM role authentication can be used instead of AK/SK. The following example generates an image embedding:
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 |
+---------------------------+
The preceding results are examples. The actual embedding dimension depends on the configured provider and model.
Text Example
The following table simulates a company's code of conduct.
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"));
By vectorizing the text, you can perform operations such as:
- Question answering retrieval (with
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 |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+--------------------+
- Problem analysis matching (with
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 |
+------+-----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
- Text relevance matching and recommendation based on article content (with
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 |
+------+-----------------------------+-----------------------------------------------------------------------------------------------------------------------------------------+---------------------+
- Find content with minimal differences(with
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 |
+------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+