PolarDB for AI integrates the Alibaba Cloud Qwen LLM into PolarDB clusters. Call the built-in Qwen LLM through SQL to perform inference and interaction on your PolarDB data.
For example, use the sentiment analysis model (_polar4ai_tongyi_sa) to analyze sentiment in table data, or the chat model (_polar4ai_tongyi) to generate answers from specific input.
|
Sentiment analysis model
Output: Negative |
|
Chat model
Output: I am a large language model from Alibaba Cloud. My name is Qwen. |
Scenarios
|
Model |
Scenario |
|
Analyzes sentence sentiment. |
|
|
Generates answers from input content. |
|
|
Summarizes article content. |
|
|
Translates Chinese to English, or other languages (including English) to Chinese. |
|
|
Generates positive or negative comments from input content. |
Limitations
-
Online inference processes one record at a time. Offline inference supports batch processing.
-
The Qwen LLM supports a maximum context length of 8,000 tokens. Requests time out after 10 seconds due to computing resource limits. Add AI nodes to improve performance.
Usage
Syntax
Call the built-in Qwen LLM for online or offline inference using the following syntax. Complete syntax details are in Run inference.
--Online inference
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT columnname FROM tablename) with ()
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT text) with ()
--Offline inference
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT columnname FROM tablename) with (mode='async')
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT text) with (mode='async')
Parameters
|
Parameter |
Description |
|
modelname |
The model name. Specify a name based on the Scenarios. Valid values:
|
|
tablename |
The table name. |
|
columnname |
The table column name, used as model input. |
|
text |
Plain text, used as model input. Runs directly without a data table. Suitable for API call scenarios. |
|
mode |
The inference mode.
|
Examples
1. Prepare the environment
-
Add an AI node and configure its database account: Enable the PolarDB for AI feature
If you added an AI node during cluster purchase, configure the database account directly.
-
Use the Cluster Endpoint to connect to the PolarDB cluster: Log on to PolarDB for AI
2. Prepare data
-
Create a table named
textinfo.CREATE TABLE IF NOT EXISTS textinfo ( id INT NOT NULL, content TEXT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -
Add the following data to the
textinfotable.INSERT INTO textinfo (id,content) VALUES (1,"这个东西只是看着还行,实际体验上非常的不好,不推荐大家购买。"); INSERT INTO textinfo (id,content) VALUES (2,"华为最新手机Mate 60 Pro自开卖后销售火爆,目前已缺货。知名分析师郭明錤周一发文称,大幅提升Mate 60 Pro的出货量预期,华为对于产业和股市的影响力不可忽视,这种影响力正在回归中。"); INSERT INTO textinfo (id,content) VALUES (3,"近年来,中国电影市场逐渐倾向于现实题材。2018年的《我不是药神》以31亿票房成为暑期档冠军。随后,《扫毒2》、《怒火·重案》和《中国医生》等影片也取得了成功。今年暑期档,现实题材如《消失的她》、《八角笼中》和《孤注一掷》等电影大受欢迎,反映了观众对贴近生活内容的兴趣。"); INSERT INTO textinfo (id,content) VALUES (4,"你是谁"); INSERT INTO textinfo (id,content) VALUES (5,"This item only looks decent; the actual experience is very poor. I do not recommend purchasing it.");
3. Call the Qwen LLM
Online inference
Online inference processes only a single record at a time.
Sentiment analysis model
Analyzes sentence sentiment. Common use cases: e-commerce review analysis, news bias analysis, and public opinion monitoring.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_sa, SELECT content FROM textinfo WHERE id = 1) WITH ();
Returned result:
Negative
Positive and negative comment model
Generates a positive or negative comment based on a piece of content.
-
Generate a positive comment:
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_p_comment, SELECT content FROM textinfo WHERE id = 2) WITH ();Returned result:
The Huawei Mate 60 Pro has been selling extremely well since its launch, which demonstrates strong consumer recognition and support for the Huawei brand. As a well-known technology company in China, Huawei has always attracted significant attention in the smartphone market. The post by Ming-Chi Kuo further confirms Huawei's influence in the industry and stock market, indicating that Huawei is gradually regaining its rightful position. Huawei's influence extends beyond product sales to its role in driving the entire tech industry and stock market. We should encourage and support excellent companies like Huawei to promote the continuous development of China's technology industry. -
Generate a negative comment:
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_n_comment, SELECT content FROM textinfo WHERE id = 2) WITH ();Returned result:
While the sales of the Huawei Mate 60 Pro may seem optimistic, from the perspective of market diversity and competition, the popularity of a single product might obscure consumer demand for other options. Over-concentration on one product could limit the market's drive for innovation, causing other potentially excellent products to not receive enough attention and development opportunities.
Summarization model
Summarizes the provided text.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_summarize, SELECT content FROM textinfo WHERE id = 3) WITH ();
Returned result:
In recent years, the Chinese film market has increasingly favored realistic themes. In 2018, "Dying to Survive" became the summer box office champion with a revenue of 3.1 billion. Later, films like "The White Storm 2: Drug Lords," "Raging Fire," and "Chinese Doctors" also achieved great success. This summer, realistic films such as "Lost in the Stars," "Octagonal," and "No More Bets" have been very popular with audiences, reflecting a strong interest in content that reflects real life.
Translation model
-
Translate a piece of Chinese content into English.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_tran_2_en, SELECT content FROM textinfo WHERE id = 1) WITH ();Returned result:
This item only looks decent; the actual experience is very poor. I do not recommend purchasing it. -
Translate other languages (including English) into Chinese.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_tran_2_zh, SELECT content FROM textinfo WHERE id = 5) WITH ();Returned result:
这件商品看起来还不错,实际体验却很差。我不推荐购买。
Chat model
Generates answers from input content, including explicit prompts.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi, SELECT content FROM textinfo WHERE id = 4) WITH ();
Returned result:
I am a large language model from Alibaba Cloud. My name is Qwen.
You can also input plain text directly.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi, SELECT 'Who are you') WITH ();
Returned result:
I am a large language model from Alibaba Cloud. My name is Qwen.
Offline inference
Offline inference supports batch processing of multiple records. This example uses the sentiment analysis model:
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_sa, SELECT content FROM textinfo) WITH (mode='async');
The following result is returned:
+--------------------------------------+
| task_id |
+--------------------------------------+
| 6227479c-1e82-11f0-b7cb-xxxxxxxxxxxx |
+--------------------------------------+
The query returns a task ID. Use this ID to check the task status and download results.
/*polar4ai*/SHOW TASK `6227479c-1e82-11f0-b7cb-xxxxxxxxxxxx`;
When taskStatus shows finish, the task is complete.
+------------+---------------------------------------------------------------------------------------------------------------------------------------------+---------+----------------+----------------+--------+--------------+----------+
| taskStatus | filePath | results | startTime | endTime | errMsg | successBatch | allBatch |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------+---------+----------------+----------------+--------+--------------+----------+
| finish | ["http://db4ai-xxx.aliyuncs.com/final_results/_polar4ai_tongyi_sa/xxx.csv?security-token=xxx&OSSAccessKeyId=xxx&Expires=xxx&Signature=xxx"] | | 2025-04-21 xxx | 2025-04-21 xxx | | 5 | 5 |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------+---------+----------------+----------------+--------+--------------+----------+
The filePath expires after 100 minutes. Check the Expires parameter in the filePath to verify validity. Download results before expiration.
4. (Optional) Write inference results to the database
To reduce database writes, PolarDB stores AI inference results as PolarDB for AI Augmented Table (AAT) external tables in OSS. You must enable cold data archiving first.
-
Go to the PolarDB console and navigate to the page to enable the cold data archiving feature.
-
Create a database and grant permissions to the account.
Create a database named
polar4aiwith the utf8mb4 character set. The system stores auto-generated AATs in thepolar4aidatabase. Grant read and write permissions to the AI node's database account. -
Use the sentiment analysis model to perform offline inference and automatically generate an AAT.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_sa, SELECT content FROM textinfo) WITH (x_cols='content', mode='async', into_type='db') INTO tongyi_sa_predict_in_db;The following result is returned:
+--------------------------------------+ | task_id | +--------------------------------------+ | e61aefa8-42af-11f0-92d0-xxxxxxxxxxxx | +--------------------------------------+The query returns a
task ID. Use this ID to check the task status./*polar4ai*/SHOW TASK `e61aefa8-42af-11f0-92d0-xxxxxxxxxxxx`;When
taskStatusshowsfinish, the task is complete.+------------+------------+---------+----------------+----------------+--------+--------------+----------+ | taskStatus | filePath | results | startTime | endTime | errMsg | successBatch | allBatch | +------------+------------+---------+----------------+----------------+--------+--------------+----------+ | finish | [] | | 2025-04-21 xxx | 2025-04-21 xxx | | 5 | 5 | +------------+------------+---------+----------------+----------------+--------+--------------+----------+ -
View the information about the auto-generated AAT in the
polar4aidatabase.SHOW CREATE TABLE polar4ai.tongyi_sa_predict_in_db;The following result is returned:
+--------------------------+-------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------------------+-------------------------------------------------------------------------------------------------------------+ | tongyi_sa_predict_in_db | CREATE TABLE `tongyi_sa_predict_in_db` ( `content` text, `content_result` text ) ENGINE=CSV DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci /*!99990 800010128 NULL_MARKER='NULL' */ CONNECTION='default_oss_server' | +--------------------------+-------------------------------------------------------------------------------------------------------------+ -
View the data in the AAT.
SELECT * FROM polar4ai.tongyi_sa_predict_in_db;The following result is returned:
+-------------------------------------------------------------+----------------+ | content | content_result | +-------------------------------------------------------------+----------------+ | 这个东西只是看着还行,实际体验上非常的不好,不推荐大家购买。 | Negative | | 华为最新手机Mate 60 Pro自开卖后销售火爆,目前已缺货。知名分析师郭... | Positive | | 近年来,中国电影市场逐渐倾向于现实题材。2018年的《我不是药神》以31... | Neutral | | 你是谁 | Neutral | | This item only looks decent; the actual experience is ve... | Negative | +-------------------------------------------------------------+-----------------+