All Products
Search
Document Center

Realtime Compute for Apache Flink:AI_SENTIMENT

Last Updated:Mar 26, 2026

AI_SENTIMENT classifies the sentiment of a text column using a Large Language Model (LLM) and returns a score, label, and confidence value for each row.

Limitations

  • Requires Ververica Runtime (VVR) 11.4 or later.

  • Throughput is bounded by the rate limit of the model platform. When the rate limit is exceeded, the Flink operator experiences backpressure and becomes a bottleneck. In severe cases, this triggers operator timeout errors and causes the job to restart.

Syntax

AI_SENTIMENT(
  MODEL => MODEL <model_name>,
  INPUT => <input_column>
)

Input parameters

ParameterData typeDescription
MODEL <model_name>MODELThe registered model service to use. Register a model service in Model Settings. The model's output type must be VARIANT.
<input_column>STRINGThe text column to analyze.

Outputs

AI_SENTIMENT returns one row per input row with the following columns:

ColumnData typeDescription
scoreDOUBLESentiment score from -1.0 to 1.0. Reference values: -1.0 extremely negative, -0.5 moderately negative, 0.0 neutral, 0.5 moderately positive, 1.0 extremely positive.
labelSTRINGSentiment classification. Valid values: positive, negative, neutral.
confidenceDOUBLEModel confidence in the predicted label, between 0.0 and 1.0.

Example

The following example registers a Qwen-Plus model and uses AI_SENTIMENT to classify the sentiment of movie comments.

Test data

idmovie_namecommentactual_label
1Good StuffI loved the part where the child guessed sounds. It was one of the most romantic narratives I've seen in a movie. Very gentle and full of love.positive
2Dumpling QueenNothing remarkable.negative

SQL statement

-- Register the model service
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 a view with the test data
CREATE TEMPORARY VIEW movie_comment(id, movie_name, user_comment, actual_label)
AS VALUES
  (1, 'Good Stuff',      'I loved the part where the child guessed sounds. It was one of the most romantic narratives I\'ve seen in a movie. Very gentle and full of love.', 'positive'),
  (2, 'Dumpling Queen',  'Nothing remarkable.', 'negative');

-- Call AI_SENTIMENT using positional arguments
SELECT id, movie_name, actual_label, score, label, confidence
FROM movie_comment,
LATERAL TABLE(
  AI_SENTIMENT(MODEL general_model, user_comment));

-- Call AI_SENTIMENT using named arguments
SELECT id, movie_name, actual_label, score, label, confidence
FROM movie_comment,
LATERAL TABLE(
  AI_SENTIMENT(
    MODEL => MODEL general_model,
    INPUT => user_comment));

Replace the following placeholders with your actual values:

PlaceholderDescription
<YOUR ENDPOINT>The endpoint of your model service
<YOUR KEY>The API key for your model service

Output

The predicted label matches the actual_label for both rows.

idmovie_nameactual_labelscorelabelconfidence
1Good Stuffpositive0.8positive0.95
2Dumpling Queennegative-1.0negative0.95