This topic describes how to use the Tongyi Qianwen model in PolarDB by executing extended SQL statements and how to use the data generated by the model to interact with data in databases.
Background information
The Tongyi Qianwen model is developed based on the Transformer model and is trained by using ultra-large pre-trained data. The pre-trained data covers a wide range of data types, including a large number of texts, professional books, and code.
For the better usage of features related to PolarDB AIGC, PolarDB for AI deploys the Tongyi Qianwen model developed by Alibaba Cloud on PolarDB. The Tongyi Qianwen model serves as a built-in model in PolarDB. PolarDB for AI extends the syntax of SQL statements to facilitate the calling of the Tongyi Qianwen model in databases. PolarDB pre-configures some application scenarios for the model. You can follow the Usage notes section of this topic to use the configurations out-of-the-box.
Scenarios
Sentiment analytics
Analyze the emotional tendency of a sentence. For example, you can analyze the emotional tendency of e-commerce comments, news reports, and public opinions.
Generation of positive or negative comments
Generate positive or negative comments based on a piece of content.
Summary
Generate the summary of a topic based on a piece of content.
Translation
Translate a piece of content from Chinese into English, or translate the content from other languages (including English) into Chinese.
Dialogue
Generate an answer to a piece of content. The content can include some explicit prompts. The answer to the content is generated based on the prompts.
Usage notes
Syntax
You can use the Tongyi Qianwen model to perform data inference online or offline based on whether the Tongyi Qianwen model is uploaded and deployed on PolarDB for AI. The following statements show the syntax used for online and offline data inference:
-- Online inference
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT col FROM tablename) with ()
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT text) with ()
-- Offline inference
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT col FROM tablename) with (mode='async')
/*polar4ai*/SELECT * FROM PREDICT (MODEL modelname, SELECT text) with (mode='async')
For more information about how to upload and deploy the Tongyi Qianwen model, see Model management.
Parameters
Parameter | Description |
tablename | The name of the table. |
col | The name of the column in the table. The parameter value is used as the input of the Tongyi Qianwen model. |
modelname | The name of the model. The parameter value varies based on the specific scenario. Valid values:
|
text | The plain text. The text specified by this parameter can be directly executed without the need to create tables. The text can be a sentence. Example: SELECT "Tell me the price of a PolarDB cluster". |
mode | The inference mode.
Note For more information about the usage of online inference and offline inference, see Use the model for inference. |
Limits
By default, online inference can process only a single entry to ensure high query performance. If multiple entries are submitted, only the first entry is predicted. Offline inference can process multiple entries.
The Tongyi Qianwen model supports 8,000 tokens. However, if results cannot be generated within 10 seconds, no result are returned due to the limited computing resources of the model. In this case, you can add AI nodes to improve the performance of the model.
Examples
Data preparation
Execute the following SQL statements to create a table named textinfo:
CREATE TABLE IF NOT EXISTS textinfo ( id INT NOT NULL, content TEXT NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Execute the following SQL statements to add the following data to the textinfo table:
INSERT INTO textinfo (id,content) VALUES (1. "This product has a good appearance. However, it does not deliver satisfactory experience. We recommend that you do not buy it.") INSERT INTO textinfo (id,content) VALUES (2. "The latest version of the mobile phone Mate 60 Pro developed by Huawei has experienced a surge in sales after its market launch and is out of stock. Guo Mingji, a well-known analyst, said on Monday that the influence of Huawei on the industry and the stock market cannot be ignored, and this influence is returning. This influence is shown in the significant increase of the shipment expectations of Mate 60 Pro.") INSERT INTO textinfo (id,content) VALUES (3. "The biggest change this summer is the failure of Hollywood blockbusters and the popularity of domestic films that have a realistic theme. In the past, Hollywood blockbusters focused on dazzling visual effects, action, fantasy and adventure theme, and mainly relied on visual bombardment. In the post-epidemic era, audiences are more interested in realistic and personal content. The top 10 movies this year have realistic themes. "Lost in the stars" combines hot topics such as anti-love brain and the wife killing of Thailand. "Never Say Never" deeply digs into the life experience of Wang Baoqiang and focuses on grassroots counter-attack. "No More Bets" focuses on curiosity about social hot spots such as telecom fraud, Myanmar's fraud park, and the setting up of lotus officials. "One and Only" also focuses on the growth of ordinary people. Looking back on the Chinese film market in recent years, we can conclude that the realistic theme has already begun to rise in popularity. In 2018, "Dying to Survive" won the summer championship with 3.1 billion box office. The following films are of the realistic theme: "The White Storm 2: Drug Lords" in 2019, "Raging Fire" in 2021, "Chinese Doctor" in 2021, "Lighting Up The Stars" in 2022, and "Nice View" and "Sister" played on other schedules. INSERT INTO textinfo (id,content) VALUES (4. "Tell me who you are within 30 words")
Inference based on the Tongyi Qianwen model
Scenario 1: Sentiment analytics
Analyze the emotional tendency of a sentence. For example, you can analyze the emotional tendency of e-commerce comments, news reports, and public opinions.
/*polar4ai*/ SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_sa, SELECT content FROM textinfo WHERE id=1) WITH ();
The following result is returned:
Negative sentiment
Scenario 2: Generation of positive or negative comments
Generate positive or negative comments based on a piece of content.
Generate positive comments:
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_p_comment, SELECT content FROM textinfo WHERE id=2) with ()
The following result is returned:
Mate 60 Pro developed by Huawei has witnessed a significant increase in sales volume and popularity immediately after its market launch. This suggests that consumers have shown a positive response to the Huawei brand and have expressed their support by purchasing the product. As a well-known technology company in China, the behavior of Huawei in the mobile phone market has attracted much attention. The post of Guo Mingjī further confirms the influence of Huawei in the industry and the stock market, which indicates that Huawei is gradually returning to its rightful place. The influence of Huawei is reflected not only in its product sales, but also in its role in promoting the entire technology industry and the stock market. We should encourage and support excellent enterprises like Huawei to promote the sustained development of the science and technology industry in China.
Generate negative comments:
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_n_comment, SELECT content FROM textinfo WHERE id=2) with ();
The following result is returned:
The significant increase in the sales volume of Mate 60 Pro developed by Huawei is surprising. This shows that consumers have a strong level of recognition and support for the Huawei brand. However, this may also be related to the current instability in the global technology market. As a Chinese company, Huawei has been under political and economic pressure in the international market, which may affect its sales performance. In addition, the comments of Guo Mingjī, a well-known analyst, may be overly optimistic and ignores the challenges and difficulties that Huawei faces. In summary, although the influence of Huawei is returning, the future market performance of the brand still needs to be treated with caution.
Scenario 3: Summary
Generate the summary of a topic based on a piece of content.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_summarize, SELECT content FROM textinfo WHERE id=3) with ();
The following result is returned:
The biggest change this summer is that films of the realistic theme have higher popularity than Hollywood blockbusters. In the Chinese film market, films of the realistic theme have become mainstream. Many films of this theme such as "The White Storm 2: Drug Lords" and "Raging Fire" gain high box office.
Scenario 4: Translation
Translate a piece of content from Chinese into English, or translate the content from other languages (including English) into Chinese.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi_tran_2_en, SELECT content FROM textinfo WHERE id=1) with ();
The following result is returned:
The item looks okay, but the actual experience is very poor. I don't recommend buying it.
Scenario 5: Dialogue
Generate an answer to a piece of content. The content can include some explicit prompts. The answer to the content is generated based on the prompts.
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi, SELECT content FROM textinfo WHERE id=4) with ();
The following result is returned:
I am an ultra-large-scale language model developed by Alibaba DAMO Academy. My name is Tongyi Qianwen.
You can also directly enter text to obtain results. For example, you can also execute the following statement in the example described in "Scenario 5: Dialogue":
/*polar4ai*/SELECT * FROM PREDICT (MODEL _polar4ai_tongyi, SELECT 'Tell me who you are within 30 words') with ();
The following result is returned:
I am an ultra-large-scale language model developed by Alibaba DAMO Academy. My name is Tongyi Qianwen.