AI_EXTRACT

更新时间:
复制 MD 格式

AI_EXTRACT is a MaxCompute AI function that calls a model to extract specified information from text based on a list of labels.

Syntax

STRING AI_EXTRACT(
  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 name of the model version to use. To use the default version, specify DEFAULT_VERSION.

  • input: Required. STRING. The text from which to extract information.

  • labels: Required. Data type: ARRAY<STRING>. A list of the labels that you want to extract. The number of labels can range from 1 to 20.

  • 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 in JSON format that contains all extracted labels and their corresponding values. The function follows these rules:

  • If input is not a STRING or labels is not an ARRAY<STRING>, the function returns an error.

  • If a constant array is provided for labels and it contains more than 20 labels, the function returns an error.

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

  • If the input text lacks information for a label in the labels array, the value for that label is NULL in the output.

Examples

Example 1: Extract with constant labels

Call the MaxCompute public model qwen3.7-max to extract structured information from the given text for the specified labels.

SET odps.namespace.schema=true;

SELECT AI_EXTRACT(
    bigdata_public_modelset.default.`qwen3.7-max`,
    DEFAULT_VERSION,
    'Zhang Wei is a 35-year-old software engineer who works at the Alibaba Cloud office in Hangzhou. He joined the company in 2020 and specializes in distributed computing.',
    ARRAY('Name', 'Age', 'Occupation', 'Company', 'City', 'Year Joined')
) AS extracted_info;

-- Result
+----------------+
| extracted_info |
+----------------+
| {\n  "Name": "Zhang Wei",\n  "Age": 35,\n  "Occupation": "Software Engineer",\n  "Company": "Alibaba Cloud",\n  "City": "Hangzhou",\n  "Year Joined": 2020\n} |
+----------------+

Example 2: Extract from table data

Call the MaxCompute public model deepseek-v4-pro to extract product, issue, and sentiment information from customer reviews stored in a table.

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

INSERT INTO customer_reviews VALUES
    ('The new laptop has great battery life, but the keyboard feels a bit off.'),
    ('Shipping was fast, and the sound quality of the headphones is excellent for the price. Very satisfied.'),
    ('The monitor had dead pixels, so I returned it. The customer service was helpful, though.');

-- Call the model to extract structured information from customer reviews in the table
SET odps.namespace.schema=true;

SELECT
    review,
    AI_EXTRACT(
        bigdata_public_modelset.default.`deepseek-v4-pro`,
        DEFAULT_VERSION,
        review,
        ARRAY('Product', 'Issue', 'Sentiment')
    ) AS extracted_info
FROM customer_reviews;

-- Result
+--------+----------------+
| review | extracted_info |
+--------+----------------+
| The new laptop has great battery life, but the keyboard feels a bit off. | {"Product": "Laptop", "Issue": "Keyboard feels off", "Sentiment": "Negative"} |
| Shipping was fast, and the sound quality of the headphones is excellent for the price. Very satisfied. | {"Product": "Headphones", "Issue": null, "Sentiment": "Positive"} |
| The monitor had dead pixels, so I returned it. The customer service was helpful, though. | {"Product": "Monitor", "Issue": "Dead pixels", "Sentiment": "Negative"} |
+--------+----------------+

FAQ

Troubleshooting public models

Symptom: When you call a public model supported by the Model Computing Service, you receive the following error:

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 has not been activated in your project's region (for example, cn-shanghai). As a result, MaxCompute cannot access the AI inference resources.

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