All Products
Search
Document Center

MaxCompute:AI_SENTIMENT

Last Updated:May 26, 2026

AI_SENTIMENT is a MaxCompute SQL AI function that performs sentiment analysis on text by calling a model.

Syntax

STRING AI_SENTIMENT(
  STRING <model_name>,
  STRING <version_name>,
  STRING <input>
  [, ARRAY<STRING> <categories>]
  [, STRING <model_parameters>]
);

Parameters

  • model_name: Required. STRING. The name of the model to use. For more information, see SQL AI functions.

  • version_name: Required. STRING. The name of the model version to use. Specify DEFAULT_VERSION to use the default version.

  • input: Required. STRING. The text to analyze.

  • categories: Optional. ARRAY<STRING>. The categories for sentiment analysis. The default categories are positive, negative, neutral, and mixed. This parameter accepts a constant array or a column. If you provide a constant array, it must contain 1 to 10 categories.

  • model_parameters: Optional. A string specifying model parameters in JSON format, such as max_tokens, temperature, and top_p. For example:

    '{"max_tokens": 500, "temperature": 0.6, "top_p": 0.95}'.

    • max_tokens: The maximum number of tokens to generate in a single model call. The default for MaxCompute public models is 4096.

    • temperature: A value between 0 and 1 that controls the randomness of the model's output. A higher value results in more creative and diverse output, while a lower value makes the output more deterministic and conservative.

    • top_p: A value between 0 and 1 that limits the range of candidate labels the model considers. A higher value results in a wider range and more diversity, while a lower value results in a narrower range and more focused output.

Return value

Returns a STRING value indicating the sentiment category. The value is determined as follows:

  • If the sentiment of the input text cannot be determined, the function returns NULL.

  • If input is NULL or an empty string (""), or if the categories array contains an empty string (""), the function returns NULL.

  • If the categories parameter is NULL, the function uses the default categories: positive, negative, neutral, and mixed.

  • If categories is a constant array that contains more than 10 categories, the function returns an error.

Examples

Example 1: Sentiment analysis with default categories

This example uses the qwen3-max public model in MaxCompute to analyze the sentiment of a product review. Because no custom categories are specified, the function uses the default categories: positive, negative, neutral, and mixed.

-- Use the model computing service in SQL jobs
SET odps.task.major.version=sql_modelstudio;
SET odps.namespace.schema=true;

SELECT AI_SENTIMENT(
    bigdata_public_modelset.default.`qwen3-max`,
    DEFAULT_VERSION,
    'The product quality is excellent, and the shipping was fast. Highly recommended!'
) AS sentiment;

-- Result
+-----------+
| sentiment |
+-----------+
| positive  |
+-----------+

Example 2: Sentiment analysis with custom categories

This example uses the Qwen3-4B-GGUF public model in MaxCompute to classify multiple customer reviews with custom categories. The categories parameter specifies a custom set of labels (satisfied, unsatisfied, neutral) for customer satisfaction analysis.

-- Sample data
CREATE TABLE customer_feedback (
    review STRING
);

INSERT INTO customer_feedback VALUES
    ('The service exceeded my expectations. I am very satisfied!'),
    ('The experience was terrible. The product broke after just one day of use.'),
    ('The package arrived on time. Nothing special.'),
    ('I love the design, but the battery life is too short.');

-- Analyze customer feedback sentiment using custom categories
SET odps.sql.ai.treat.as.common.model=true;
SET odps.namespace.schema=true;

SELECT
    review,
    AI_SENTIMENT(
        bigdata_public_modelset.default.`Qwen3-4B-GGUF`,
        DEFAULT_VERSION,
        review,
        ARRAY('satisfied', 'unsatisfied', 'neutral')
    ) AS sentiment
FROM customer_feedback;

-- Result
+---------------------------------------------------------------------------+-------------+
| review                                                                    | sentiment   |
+---------------------------------------------------------------------------+-------------+
| The service exceeded my expectations. I am very satisfied!                | satisfied   |
| The experience was terrible. The product broke after just one day of use. | unsatisfied |
| The package arrived on time. Nothing special.                             | neutral     |
| I love the design, but the battery life is too short.                     | neutral     |
+---------------------------------------------------------------------------+-------------+