Asynchronous vectorization

Updated at:

Use a Lindorm stream engine extract, transform, and load (ETL) task to call the AI engine automatically when you insert or update text in a wide table. The task generates embedding vectors and writes them back to the table, so your application does not need to call the model API directly.

Prerequisites

  • LindormTable is activated and runs version 2.9.2.1 or later. For more information about the VECTOR data type, see Vector type.

  • The stream engine is activated and runs version 3.4.2 or later. The instance must support bailian_embedding and writing results back to a VECTOR column.

  • You have the AI engine endpoint and API key that the stream engine can use to call text-embedding-v4.

  • Contact Lindorm technical support to confirm the required AI embedding model and quota. For the contact details, see Limits.

Limits

  • Supported input — Only text embeddings are supported. Image embeddings are not supported.

  • Model and quota — Before you use the automatic embedding feature, contact Lindorm technical support on DingTalk (ID: s0s3eg3) to confirm the required AI embedding model and quota. Otherwise, the task may fail or fall behind because of throttling.

  • Throughput — Vectorization is asynchronous relative to writes in the wide table. Within the stream engine operator, bailian_embedding calls the model synchronously, one record at a time. Throughput depends on the model response time, the quota, and the operator parallelism that the parallelism parameter sets.

Example

This example shows the complete process: create a wide table with a vector column, and then use a stream engine ETL task to detect text changes, generate vectors, and write them back to the table.

Step 1: Create a wide table

Connect to LindormTable SQL and run the following statements to create the table in the default database. For the syntax, see Use a MySQL client to connect to and use LindormTable.

USE `default`;

CREATE TABLE product_embedding_demo (
  id VARCHAR,
  title VARCHAR,
  embedding VECTOR(768),
  PRIMARY KEY (id)
);

Step 2: Create an ETL task

Connect to stream engine SQL. To obtain the connection endpoint, see Connect to the streaming engine using the MySQL command line.

Replace <AI_ENGINE_HOST>:<PORT> and <AI_API_KEY> with the AI engine endpoint and API key, and then run the following statement:

CREATE ETL product_title_embedding
WITH (
  `source.parallelism` = '4',
  `parallelism` = '8',
  `ai.engine.endpoint` = 'https://<AI_ENGINE_HOST>:<PORT>',
  `ai.engine.apikey` = '<AI_API_KEY>',
  `ai.bailian.embedding` = 'text-embedding-v4',
  `incremental.startup.mode` = 'latest-offset',
  `changelogMode` = 'true',
  `changelog.watch-columns` = 'title',
  `source.ignore-delete` = 'true',
  `source.ignore-update-before` = 'true'
)
AS
INSERT INTO `lindorm_table`.`default`.product_embedding_demo (id, embedding)
SELECT id, bailian_embedding(title, 768)
FROM `lindorm_table`.`default`.product_embedding_demo
WHERE title IS NOT NULL AND CHAR_LENGTH(TRIM(title)) > 0;

The WHERE clause skips empty or whitespace-only text, so no vector is generated for those rows. The following table describes the parameters in the WITH clause and the function in the SELECT clause:

Parameter

Description

source.parallelism

The read parallelism of the source wide table. As a general guideline, set it higher than the number of nodes in the source LindormTable cluster.

parallelism

The compute parallelism of the task. bailian_embedding calls the model one record at a time, so this parameter controls the embedding concurrency in this example. Set it based on the model quota.

ai.engine.endpoint

The base URL of the AI engine, including the scheme, host, and port.

ai.engine.apikey

The API key of the AI engine.

ai.bailian.embedding

The embedding model name. This example uses text-embedding-v4.

incremental.startup.mode

Valid values: latest-offset (reads only changes made after the task starts), initial (scans existing data first, and then continues reading changes), and snapshot (scans existing data once, and then stops). This example uses latest-offset. To generate vectors for text that already exists in the table, use initial.

changelogMode

Required for this example. The default value is false. Set it to true to enable incremental change detection.

changelog.watch-columns

Required for this example. The default value is *. Set it to title so that only title changes trigger vectorization. This prevents the vector writeback from triggering the computation again.

source.ignore-delete

Required for this example. The default value is false. Set it to true to skip delete events. This avoids computing the old title or deleting the source row again.

source.ignore-update-before

Required for this example. The default value is false. Set it to true to skip the pre-update record and avoid computing the vector for the old title again.

bailian_embedding(title, 768)

The function called in the SELECT clause, not a property in the WITH clause. title is the text to vectorize. 768 is the output dimension and must match the target vector column.

Step 3: Write text and query the result

In stream engine SQL, run SHOW ETLS; and verify that the task is RUNNING.

Return to LindormTable SQL to write text to the wide table and query the result:

UPSERT INTO product_embedding_demo (id, title)
VALUES ('p001', 'Lightweight waterproof outdoor backpack');

SELECT id, title, embedding
FROM product_embedding_demo
WHERE id = 'p001';

Vectorization is asynchronous, so there is a delay before the vector column is written back. After the writeback completes, the embedding column in the query result contains the generated vector. When you upsert a new title value for an existing primary key, the task generates a new vector. To monitor the task latency, see Stream Engine Monitoring and Alerting.

Step 4: Delete the ETL task

(Optional) To stop vectorization, delete the ETL task in stream engine SQL. Vectors that have already been written remain in the table and are not cleared. For the detailed syntax, see ETL management.

DROP ETL IF EXISTS product_title_embedding;