This topic provides the use cases of a large language model (LLM) to describe how to use the Row-oriented AI feature In the cases provided in this topic, although the given LLM does not run on an AI node, the principle and usage are the same as those when the LLM run on the AI node. When an LLM is not run on an AI node, the backend system automatically generates user-defined functions (UDFs) to call the LLM.
An online inference service is provided for LLMs free of charge. You can directly deploy models in PolarDB clusters and create functions that are used to call LLMs, and use the created functions to perform online inference or interactive Q&A.
In the use cases described in this topic, the built-in qwen function is used to show how to use the Row-oriented AI feature.
Assume that a movie review table named movie_comment exists and contains the following content:
mysql> SELECT * FROM movie_comment;
+----+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | name | review |
+----+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | movie1 | The work itself is quite good, five-star thumbs up |
| 2 | movie1 | Full marks! Nolans performance this time is indeed remarkable, showcasing how mature his level and technique are from narrative techniques to plot reasoning |
| 3 | movie1 | At the beginning of the explosion, it was silent, realizing the destruction afterwards. So shocking!! |
| 4 | movie1 | It is very boring. A movie thats over three hours long, I couldnot endure watching it till the end. |
| 5 | movie2 | They portrayed so many characters and plot twists very well, really impressive. |
| 6 | movie2 | Every detail is meticulous, the true nature of humanity is most touching, and the ending is excellent. |
| 7 | movie2 | Iranian movie, didnot expect it to be so good. |
| 8 | movie3 | The pinnacle of domestic films. |
| 9 | movie3 | Domestic films can also be so thrilling and satisfying. |
| 10 | movie3 | The plot is som ewhat stiff, and the initial immersion is quite ordinary. |
+----+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)Enable the PolarDB for AI and cold data archiving features for the cluster. For more information, see Enable the PolarDB for AI feature and Enable cold data archiving.
Connect to a database and deploy the built-in function model.
/*polar4ai*/ DEPLOY MODEL builtin_qwen WITH (mode = 'in_db');After the model is deployed, you can use the
SHOW MODELscript to view the model status. If the model is in the serving state after deployment, the deployment is completed./*polar4ai*/ SHOW MODEL builtin_qwen;Create an LLM-based function named
qwen.CREATE FUNCTION qwen RETURNS STRING SONAME "#ailib#_builtin_qwen.so";Call the
qwenfunction to perform online inference.Example 1: Call the
qwenfunction to directly use SQL statements to classify movie reviews into three categories: "good", "fair", and "bad". The following SQL statement is used:SELECT qwen(CONCAT("Classify the following text into three categories by emotional color: [good, general, bad], and output the name of one of the three categories. For example, input [This movie is great] and output "good". For another example, input [This movie is too boring] and output "bad".", review)) as emotional color classification FROM movie_comment;The following result is returned:
+----------------------+ | sentiment | +----------------------+ | good | | good | | good | | bad | | good | | good | | good | | good | | good | | neural | +----------------------+ 10 rows in set (5.36 sec)Example 2: Call the
qwenfunction to perform interactive Q&A. The following SQL statement is used:SELECT qwen("Hello, when will the Hangzhou Asian Games be held");The following result is returned:
+------------------------------------------------------------------------------------+ | qwen("Hello, when will the Hangzhou Asian Games be held") | +------------------------------------------------------------------------------------+ | The Hangzhou Asian Games will be held from September 23rd to October 8th, 2023. | +------------------------------------------------------------------------------------+ 1 row in set (0.94 sec)