This topic describes how to use Row-oriented AI to import a model.
Step 1: Enable the PolarDB for AI and cold data archiving features
Before you import models, you must enable the PolarDB for AI and cold data archiving features for your cluster. For more information, see Enable the PolarDB for AI feature and Enable cold data archiving.
If the model that you want to import is a large language model (LLM), you can skip Step 2 and perform operations described in Step 3, Step 4, and Step 5 after you perform operations described in Step 1.
Step 2: Upload the model
Connect to a database and then execute the following statement to upload the created model to the AI node in the cluster:
/*polar4ai*/ UPLOAD MODEL model_name WITH (model_location = 'https://mybucket.oss-cn-hangzhou.aliyuncs.com/logisitic_regression.tgz');After the model is uploaded, you can use the SHOW MODEL script to view the model status. The model is in the saved_oss state after the model is uploaded.
/*polar4ai*/ SHOW MODEL model_name;The following table describes the parameters that you can configure in the preceding statement.
Parameter | Description |
model_name | The name of the pre-created model that you want to upload. The model name cannot contain hyphens (-) and must start with a letter. |
model_location | The public URL used to download the model. |
Step 3: Deploy the model
Execute the following statement to deploy the model:
/*polar4ai*/ DEPLOY MODEL model_name WITH (mode = 'in_db');After executing the statement, you can use the SHOW MODEL script to view the model status. If the model status is serving, the model has been deployed.
/*polar4ai*/ SHOW MODEL model_name;The value of the
model_nameparameter must be consistent with the model name described in Step 2.The
modeparameter in the statement used to deploy the model must be set toin_db.It may take a while to deploy the model. You need to wait until the model status changes to serving before you perform subsequent operations.
Step 4: Create a function
To create a function, you need to use a privileged account or grant a standard account permissions on the mysql.func system table. You can execute the following statements in your MySQL client by using the privileged account to grant a standard account permissions on the system table:
-- root login:
mysql> grant insert on mysql.* to 'normal'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql> grant delete on mysql.* to 'normal'@'localhost';
Query OK, 0 rows affected (0.02 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.04 sec)
mysql> select * from mysql.func;
Empty set (0.00 sec)
-- normal login:
mysql> select myfunc_int(1);
ERROR 1046 (3D000): No database selected
mysql> select * from mysql.func;
ERROR 1142 (42000): SELECT command denied to user 'normal'@'localhost' for table 'func'
mysql> create function myfunc_int returns int soname 'udf_example.so';
Query OK, 0 rows affected (0.01 sec)
mysql> select myfunc_int(1);
+---------------+
| myfunc_int(1) |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> drop function myfunc_int;
Query OK, 0 rows affected (0.00 sec)You can use the user-defined functions (the .so file) generated in Step 3 to create a function.
CREATE FUNCTION function_name RETURNS return_value SONAME "soname";The following table describes the parameters that you can configure in the preceding statement.
Parameter | Description |
function_name | The function name. Note The function name must be consistent with the model name specified in Step 2. |
return_value | The type of the return value. The REAL, STRING, and INTEGER types are supported. Note The type of the returned value must be consistent with the actual input type of the model. Otherwise, The output may not match expected results. |
soname | The name of the .so file. The file is named in the following format: # ailib#_ModelName.so |
After the function is created, you can execute the following statement to query the system table to check whether the function has been created:
SELECT * FROM mysql.func;Step 5: Call the function to perform model inference
You can execute the following statements to call the function for model inference:
SELECT function_name(feature1, feature2) from predict_table;
SELECT function_name("content");The following table describes the parameters that you can configure in the preceding statements.
Parameter | Description |
function_name | The function name. |
feature1 | The name of the column used for model inference. |
feature2 | The name of the column used for model inference. |
predict_table | The name of the table used for model inference. |
content | The input for model inference. |
(Optional) Step 6: Delete the function and model
If you no longer require the function and model, you need to delete the function and then delete the model that is created on the AI node. The following scripts are used to delete a function and model:
Delete a function
DROP FUNCTION function_name;When you execute the script, replace the
function_nameparameter with the name of the function that you want to delete.Delete a model
/*polar4ai*/ DROP MODEL model_name;When you execute the script, replace the
model_nameparameter with the name of the model that you want to delete.