Use the AI_EMBED function
This topic describes how to use the AI_EMBED function in AI Function Service to generate vector embeddings from text in SeekDB.
Overview
The AI_EMBED function is used to generate vector embeddings for text data.
Hybrid search depends on the AI model management feature and the AI_EMBED function. Before you delete an AI model, check whether it is referenced by hybrid search. Otherwise, potential issues will be caused.
seekdb allows you to use the AI_EMBED function in AI Function Service to generate vector embeddings. You do not need to install any dependencies. Simply register the model information, and then use the AI Function Service in seekdb to generate vector embeddings.
The AI_EMBED function converts user-provided text data into vector data using a specified embedding model, as identified by the model_key parameter. If the model supports multiple dimensions, you can specify the output dimensions using the dim parameter.
Prerequisites
Granting Privileges
Before you can use AI function services such as the AI_EMBED function, make sure that you have the required permissions. For more information, see AI function call permissions.
Register a model
Before using the AI_EMBED function, you must register model information by calling subprograms in the DBMS_AI_SERVICE package. Example:
CALL DBMS_AI_SERVICE.DROP_AI_MODEL ('ob_embed');
CALL DBMS_AI_SERVICE.DROP_AI_MODEL_ENDPOINT ('ob_embed_endpoint');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL(
'ob_embed', '{
"type": "dense_embedding",
"model_name": "BAAI/bge-m3"
}');
CALL DBMS_AI_SERVICE.CREATE_AI_MODEL_ENDPOINT (
'ob_embed_endpoint', '{
"ai_model_name": "ob_embed",
"url": "https://api.siliconflow.cn/v1/embeddings",
-- Replace this with the actual AccessKey of an Alibaba Cloud account that has the appropriate permissions.
"access_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"provider": "siliconflow"
}');
For information about supported model providers and corresponding URLs, see CREATE_AI_MODEL_ENDPOINT.
Syntax
AI_EMBED(model_key, input, [dim])
Parameters:
| Parameter | Description | Type | Null? |
|---|---|---|---|
| model_key | Model registered in the database. | VARCHAR(128) | No |
| input | The text data to convert. | VARCHAR | No |
| dim | Specifies the vector containing the dimension related to the output. Some large model vendors support configuring multiple dimensions in their API. | INT64 | Yes |
If the model_key or input parameter is specified, and one of the parameters is specified as NULL, an error is returned.
The return values are described as follows:
- A string of vector format. The embedding model converts the text into a vector.
Example
Embed single-row data
SELECT AI_EMBED("ob_embed","Hello world") AS embedding;
The returned result is as follows:
+----------------+
| embedding |
+----------------+
| [0.1, 0.2, 0.3]|
+----------------+
Embed columns into a table
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
content TEXT
);
INSERT INTO comments (content) VALUES ('hello world!');
SELECT AI_EMBED("ob_embed",content) AS embedding FROM comments;
The returned result is as follows:
+----------------+
| embedding |
+----------------+
| [0.1, 0.2, 0.3]|
+----------------+