AI_SENTIMENT

更新时间:
复制 MD 格式

AI_SENTIMENT is a MaxCompute AI Function that performs sentiment analysis on input text.

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 call. For more information, see SQL AI Function.

  • version_name: Required. STRING. The version of the model. To call the default version, you can specify DEFAULT_VERSION.

  • input: Required. STRING. The input text for sentiment analysis.

  • categories: Optional. ARRAY<STRING>. The categories for sentiment analysis. The default categories are positive, negative, neutral, and mixed. This parameter supports both constants and columns. A constant must contain 1 to 10 categories.

  • model_parameters: Optional. STRING. Specifies model parameters such as max_tokens, temperature, and top_p. The format is a JSON string:

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

    • max_tokens: The maximum number of tokens to generate in a single model call. For MaxCompute public models, the default value is 4,096.

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

    • top_p: A value between 0 and 1 that limits the range of candidate labels the model can choose from. A higher value allows for a broader range and more diversity, while a lower value narrows the range and produces more focused results.

Return value

Returns the sentiment category as a STRING.

  • If the function cannot identify the sentiment category of the input text, it returns NULL.

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

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

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

Examples

Example 1: Sentiment analysis using default categories

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

SET odps.namespace.schema=true;

SELECT AI_SENTIMENT(
    bigdata_public_modelset.default.`qwen3.7-max`,
    DEFAULT_VERSION,
    'The product quality is excellent, and delivery was fast. Highly recommended!'
) AS sentiment;
-- Result
+-----------+
| sentiment |
+-----------+
| positive  |
+-----------+

Example 2: Sentiment analysis using custom categories

This example calls the deepseek-v4-pro MaxCompute public model to analyze the sentiment of multiple customer reviews using custom categories. The categories parameter specifies a custom set of labels (positive, negative, neutral) that are better suited for a customer satisfaction analysis scenario.

-- 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.'),
    ('The package arrived on time. Nothing special.'),
    ('I love the design, but the battery life is too short.');

-- Analyze the sentiment of customer reviews by using custom categories
SET odps.namespace.schema=true;

SELECT
    review,
    AI_SENTIMENT(
        bigdata_public_modelset.default.`deepseek-v4-pro`,
        DEFAULT_VERSION,
        review,
        ARRAY('positive', 'negative', 'neutral')
    ) AS sentiment
FROM customer_feedback;

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

FAQ

Troubleshoot common issues with public models

Symptom: When you call a public model supported by the Model Computing Service, the service returns the following error message.

FAILED: ODPS-0130071:[1,8] Semantic analysis exception - inference quota status check failed, error message: region cn-shanghai not found in inference quota

Cause: The Model Computing Service is not activated in the region where your project resides (for example, cn-shanghai). This prevents the system from calling AI inference resources.

Solution: Go to the Alibaba Cloud console and activate the Model Computing Service for your project's region. For more information, see Purchase and use the MaxCompute Model Computing Service