AI_CLASSIFY

Updated at:

AI_CLASSIFY is an AI function in MaxCompute that calls a model to return the label from a given set that best matches the input.

Syntax

STRING AI_CLASSIFY(
  STRING <model_name>,
  STRING <version_name>,
  STRING <input>,
  ARRAY<STRING> <labels>
  [, STRING <model_parameters>]
);

Parameters

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

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

  • input: Required. STRING. The text that you want to classify.

  • labels: Required. ARRAY<STRING>. A list of candidate labels for classification. This parameter accepts constants and column input. If a constant is used, the number of labels must be between 2 and 20, inclusive.

  • 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 a STRING value that represents the label best matching the input.

  • Returns an error if input is not a STRING or labels is not an ARRAY<STRING>.

  • Returns an error if labels is a constant and the number of labels is 1 or greater than 20.

  • Returns NULL if input or labels is NULL or an empty string ("").

Examples

Example 1: Classify constant text

Calls the public model qwen3.7-max provided by MaxCompute to classify the input text and returns the best-matching result from the given labels.

SET odps.namespace.schema=true;

SELECT AI_CLASSIFY(
    bigdata_public_modelset.default.`qwen3.7-max`,
    DEFAULT_VERSION,
    'MaxCompute is a fully managed, high-performance big data computing platform that provides fast and scalable data warehousing and analysis capabilities.',
    ARRAY('Technology', 'Sports', 'Finance', 'Healthcare', 'Education')
) AS classified_label;
-- Result
+------------------+
| classified_label |
+------------------+
| Technology       |
+------------------+

Example 2: Classify table data

You can call the MaxCompute public model deepseek-v4-pro to batch classify multiple text data entries in a table.

-- Sample data
CREATE TABLE news_articles (
    content STRING
);

INSERT INTO news_articles VALUES
    ('Artificial intelligence is changing the healthcare industry with new diagnostic tools.'),
    ('Driven by a rally in the tech sector, the stock market hit a record high today.'),
    ('The team won the championship after a thrilling overtime.'),
    ('Cloud computing enables businesses to scale their infrastructure on demand.');

-- Use the model to classify the text in the table
SET odps.namespace.schema=true;

SELECT
    content,
    AI_CLASSIFY(
        bigdata_public_modelset.default.`deepseek-v4-pro`,
        DEFAULT_VERSION,
        content,
        ARRAY('Technology', 'Sports', 'Finance', 'Healthcare')
    ) AS category
FROM news_articles;

-- Result
+------------------------------------------------------------------------------------------+------------+
| content                                                                                  | category   |
+------------------------------------------------------------------------------------------+------------+
| Artificial intelligence is changing the healthcare industry with new diagnostic tools.   | Healthcare |
| Driven by a rally in the tech sector, the stock market hit a record high today.          | Finance    |
| The team won the championship after a thrilling overtime.                                | Sports     |
| Cloud computing enables businesses to scale their infrastructure on demand.              | Technology |
+------------------------------------------------------------------------------------------+------------+

FAQ

Troubleshooting public model issues

Symptom: When you call a public model supported by the MaxCompute 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 MaxCompute Model Computing Service is not enabled for your project's region (for example, cn-shanghai). As a result, you cannot call the AI inference resources.

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