All Products
Search
Document Center

PolarDB:Qwen LLM data inference and interaction

Last Updated:Jun 26, 2026

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

/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_sa, SELECT content FROM textinfo LIMIT 1) WITH ();
-- Content from the database table: This item only looks decent; the actual experience is very poor. I do not recommend purchasing it. 
Output: Negative

Chat model

/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi, SELECT 'Who are you') WITH ();
Output: I am a large language model from Alibaba Cloud. My name is Qwen.

Scenarios

Model

Scenario

Sentiment analysis model

Analyzes sentence sentiment.

Chat model

Generates answers from input content.

Summarization model

Summarizes article content.

Translation model

Translates Chinese to English, or other languages (including English) to Chinese.

Positive/negative comment model

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:

  • Sentiment analysis model: _polar4ai_tongyi_sa

  • Positive/negative comment model:

    • Positive comment model: _polar4ai_tongyi_p_comment

    • Negative comment model: _polar4ai_tongyi_n_comment

  • Summarization model: _polar4ai_tongyi_summarize

  • Translation model:

    • Translate to Chinese: _polar4ai_tongyi_tran_2_zh

    • Translate to English: _polar4ai_tongyi_tran_2_en

  • Chat model: _polar4ai_tongyi

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.

  • Set to async for offline inference.

  • Omit for online inference.

Examples

1. Prepare the environment

  1. 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.
  2. Use the Cluster Endpoint to connect to the PolarDB cluster: Log on to PolarDB for AI

2. Prepare data

  1. Create a table named textinfo.

    CREATE TABLE IF NOT EXISTS textinfo (
        id INT NOT NULL,
        content TEXT NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  2. Add the following data to the textinfo table.

    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        |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------+---------+----------------+----------------+--------+--------------+----------+
Note

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.

  1. Enable cold data archiving.

    Go to the PolarDB console and navigate to the Configuration and Management > Data and Lifecycle > Data Archive (Cold Data) page to enable the cold data archiving feature.

  2. Create a database and grant permissions to the account.

    Create a database named polar4ai with the utf8mb4 character set. The system stores auto-generated AATs in the polar4ai database. Grant read and write permissions to the AI node's database account.

  3. 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 taskStatus shows finish, the task is complete.

    +------------+------------+---------+----------------+----------------+--------+--------------+----------+
    | taskStatus | filePath   | results | startTime      | endTime        | errMsg | successBatch | allBatch |
    +------------+------------+---------+----------------+----------------+--------+--------------+----------+
    | finish     | []         |         | 2025-04-21 xxx | 2025-04-21 xxx |        | 5            | 5        |
    +------------+------------+---------+----------------+----------------+--------+--------------+----------+
  4. View the information about the auto-generated AAT in the polar4ai database.

    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' |
    +--------------------------+-------------------------------------------------------------------------------------------------------------+
  5. 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       |
    +-------------------------------------------------------------+-----------------+