This topic describes how to use the AI_SENTIMENT function to perform sentiment analysis using an LLM.
Limitations
This function requires Ververica Runtime (VVR) 11.4+.
The throughput of
AI_SENTIMENToperators is subject to the rate limits of Alibaba Cloud Model Studio. When the rate limits for a model are reached, the Flink job will be backpressured withAI_SENTIMENToperators as the bottleneck. In some cases, timeout errors and job restarts may be triggered.
Syntax
AI_SENTIMENT(
MODEL => MODEL <MODEL NAME>,
INPUT => <INPUT COLUMN NAME>
)Input parameters
Parameter | Data type | Description |
MODEL <MODEL NAME> | MODEL | The name of the registered model. For more information, see Model settings. Note: The output type of the model must be VARIANT. |
<INPUT COLUMN NAME> | STRING | The data to be analyzed by the model. |
Return values
Parameter | Data type | Description |
score | DOUBLE | The sentiment score determined by the model, ranging from -1.0 to 1.0:
|
label | STRING | The sentiment label (positive, negative, or neutral). |
confidence | DOUBLE | The confidence level of the model's output. |
Example
Test data
id | movie_name | user_comment | actual_label |
1 | Her Story | My favourite part was when the kid guessed the sounds. It was romantic, heartwarming, and loving. | POSITIVE |
2 | The Dumpling Queen | Nothing special. | NEGATIVE |
Test statement
The following sample SQL statement uses the Qwen-Plus model and the AI_SENTIMENT function to predict the sentiment category of movie reviews.
CREATE TEMPORARY MODEL general_model
INPUT (`input` STRING)
OUTPUT (`content` VARIANT)
WITH (
'provider' = 'openai-compat',
'endpoint'='<YOUR ENDPOINT>',
'apiKey' = '<YOUR KEY>',
'model' = 'qwen-plus'
);
CREATE TEMPORARY VIEW movie_comment(id, movie_name, user_comment, actual_label)
AS VALUES (1, 'Her Story', 'My favourite part was when the kid guessed the sounds. It was romantic, heartwarming, and loving.', 'positive'), (2, 'The Dumpling Queen', 'Nothing special.', 'negative');
-- Use positional argument to call AI_SENTIMENT
SELECT id, movie_name, actual_label, score, label, confidence FROM movie_comment,
LATERAL TABLE(
AI_SENTIMENT(
MODEL general_model, user_comment));
-- Use named argument to call AI_SENTIMENT
SELECT id, movie_name, actual_label, score, label, confidence FROM movie_comment,
LATERAL TABLE(
AI_SENTIMENT(
MODEL => MODEL general_model,
INPUT => user_comment)); Return values
The predicted result in the label column matches the actual result in the actual_label column.
id | movie_name | actual_label | score | label | confidence |
1 | Her Story | positive | 0.8 | positive | 0.95 |
2 | The Dumpling Queen | negative | -1.0 | negative | 0.95 |