AI_GENERATE

更新时间:
复制 MD 格式

AI_GENERATE is a MaxCompute AI function that invokes a model with a prompt to perform an inference task. It supports various scenarios, such as natural language generation, complex logical analysis, sentiment analysis, and multi-modal understanding. This function enables you to use SQL to process unstructured data directly, eliminating the need for external services.

Syntax

The AI_GENERATE function has different signatures for large language models (LLMs) and multi-modal large language models (MLLMs).

  • When model_type is an LLM, the function signature is as follows:

    STRING AI_GENERATE(
      STRING <model_name> , 
      STRING <version_name>, 
      STRING <prompt> 
      [, STRING <model_parameters>]
    );
  • When model_type is an MLLM, the function signature is as follows:

    STRING AI_GENERATE(
      STRING <model_name>, 
      STRING <version_name>, 
      STRING | BINARY <unstructured_data> ,
      STRING <prompt> 
      STRING <type>
      [, STRING <model_parameters>]
    );

Parameters

  • model_name: Required. A STRING value that specifies the name of the model. The model's model_type must be either LLM or MLLM.

  • version_name: Required. A STRING value that specifies the version of the model. To invoke the default version, specify DEFAULT_VERSION.

  • prompt: Required. The prompt for the model. It must be a constant, a column name, or an expression of the STRING type.

  • unstructured_data: Required when model_type is MLLM. A STRING or BINARY value.

    Specifies the multi-modal data to process, which can be a STRING URL for an image, audio, or video, or the BINARY representation of an image. If you specify the BINARY type, you must also specify a BINARY input parameter when creating the model.

  • type: When unstructured_data is a STRING URL for an image, audio, or video, type is required. Supported values are IMAGE, AUDIO, and VIDEO. For example:

    • Audio: AI_GENERATE(model, version,audio_url, prompt, 'AUDIO');

    • Video: AI_GENERATE(model, version, video_url, prompt, 'VIDEO');

    • Image: AI_GENERATE(model, version, image_url, prompt, 'IMAGE');

  • 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.

Note

To use an AI function for inference with a public model, you must first enable the use of public models by running the command SET odps.sql.using.public.model=true;.

Return value

Returns a STRING containing the model-generated content.

Examples

Example 1: Perform sentiment analysis

Use the public model qwen3.7-max provided by MaxCompute to perform sentiment analysis on online user comment data.

SET odps.namespace.schema=true;

SELECT 
    prompt,
    AI_GENERATE(
        bigdata_public_modelset.default.`qwen3.7-max`,
        DEFAULT_VERSION,
        concat('Please classify the sentiment of the following comment. The output must be exactly one of the following three options: Positive, Negative, Neutral. Comment to analyze: ', prompt)
    ) AS generated_text
FROM (
    VALUES 
        ('The weather is great today, and I am in a good mood! The sun is shining, perfect for a walk. /no_think'),
        ('The weather is great today, and I am in a good mood! The sun is shining /no_think'),
        ('Technology is advancing rapidly, and artificial intelligence is changing lives. /no_think'),
        ('The control measures are excellent, kudos to the healthcare workers! /no_think'),
        ('The quality of this product is very poor /no_think')
) t (prompt);

-- The following result is returned:
+--------+----------------+
| prompt | generated_text |
+--------+----------------+
| The weather is great today, and I am in a good mood! The sun is shining, perfect for a walk. /no_think | Positive         |
| The weather is great today, and I am in a good mood! The sun is shining /no_think | Positive         |
| Technology is advancing rapidly, and artificial intelligence is changing lives. /no_think | Neutral          |
| The control measures are excellent, kudos to the healthcare workers! /no_think | Positive         |
| The quality of this product is very poor /no_think | Negative         |
+--------+----------------+

Example 2: Generate a text summary

This example generates a text summary for a news article by invoking the MaxCompute public model deepseek-v4-flash.

SET odps.namespace.schema=true;

SELECT AI_GENERATE(bigdata_public_modelset.default.`deepseek-v4-flash`,DEFAULT_VERSION,
  "Summarize the following news in one sentence: Many people know that eating too much salt can lead to high blood pressure.
   But have you ever noticed that your blood pressure remains high, or even quietly rises, even when you reduce your salt intake?
   As medical research advances, more and more hidden causes of high blood pressure are being discovered.
   Recently, a new study identified three deeply hidden culprits. They may seem ordinary,
   but they can cause blood pressure to spiral out of control. It's not just about eating too much salt; the study reveals three hidden drivers of high blood pressure.
   In November 2025, a study published in the International Journal of Education and Health Promotion found that sleep quality, 
   amount of exercise, and healthy weight are three interconnected factors that collectively affect our blood pressure and are even the hidden drivers behind high blood pressure.");

-- The following result is returned:
+-----+
| _c0 |
+-----+
| A new study shows that beyond high salt intake, poor sleep quality, lack of exercise, and unhealthy body weight are three key hidden drivers of hypertension. |
+-----+
Note

Public models such as deepseek-v4-flash incur MaxCompute AI inference costs. For more information, see Model compute costs (token costs).

Example 3: Generate content

Call the public model deepseek-v4-pro provided by MaxCompute to directly perform model inference and content generation.

SET odps.namespace.schema=true;

SELECT AI_GENERATE(bigdata_public_modelset.default.`deepseek-v4-pro`,DEFAULT_VERSION,'what is the capital of China');

-- The following result is returned:
+-----+
| _c0 |
+-----+
| The capital of China is **Beijing**. |
+-----+

Example 4: Perform multi-modal data processing

  • Image processing

    Call the public model qwen3.7-plus to tag product categories based on an image URL and a prompt. In this example, the Object Table is named image_demo.

    -- Enable the three-tier model.
    SET odps.namespace.schema=true;
    
    -- Create an Object Table.
    CREATE OBJECT TABLE IF NOT EXISTS image_demo
    WITH SERDEPROPERTIES ('odps.properties.rolearn'='acs:ram::xxxxxxxxxxxxxxxx:role/aliyunodpsdefaultrole')
    LOCATION 'oss://oss-cn-hangzhou-internal.aliyuncs.com/muze-demo/poster_11/';
    
    ALTER TABLE image_demo REFRESH METADATA;
    
    SELECT COUNT(*) AS ROW_COUNT FROM image_demo; 
    +------------+
    | row_count  |
    +------------+
    | 50         |
    +------------+
    
    -- Invoke the public model qwen3.7-plus to label product categories based on an image URL and a prompt.
    SELECT
      key,
      AI_GENERATE(
        bigdata_public_modelset.default.`qwen3.7-plus`,  DEFAULT_VERSION, image_url,
        'Identify and extract the product category from the e-commerce product poster. The output must be exactly one of the following six options: Beauty, Apparel, Daily necessities, Food, Other, Electronics. Do not include any other text or information.','IMAGE'
      ) AS item_catagory
      FROM (
        SELECT GET_SIGNED_URL_FROM_OSS(
          'muze_project.default.image_demo', key, 604800
        ) AS image_url, key AS key
        FROM muze_project.default.image_demo
    ) Limit 10;
    
    -- The following result is returned:
    +------+---------------+
    | key  | item_catagory |
    +------+---------------+
    | O1CN01085bQu2GXxbXWRZdR_!!190979026-0-alimamazszw.jpg | Electronics     |
    | O1CN01090E2K2IpTwrsnKig_!!2200646859335-0-alimamazszw.jpg | Daily necessities |
    | O1CN01104c5a1V8Ve9lucF0_!!2212022232608-0-alimamazszw.jpg | Food            |
    | O1CN01113ir71oWIvnjQ5VI_!!2207697805232-0-alimamazszw.jpg | Beauty          |
    | O1CN01174q2u1ZlDaCqi6bG_!!2212097133234-0-alimamazszw.jpg | Other           |
    | O1CN01361qwo2D9HOv5WB4p_!!826038566-0-alimamazszw.jpg | Other           |
    | O1CN01413KK82ANWsnkpDh4_!!802948191-0-alimamazszw.jpg | Apparel         |
    | O1CN01446lv42D12HPYUDpl_!!1573418548-0-alimamazszw.jpg | Apparel         |
    | O1CN01447sWy2E4zJ1YgpM0_!!1676208692-0-alimamazszw.jpg | Beauty          |
    | O1CN01526tSJ1QY5hF6zdE7_!!2867731987-0-alimamazszw.jpg | Food            |
    +------+---------------+
  • Audio processing

    After you create a PAI-EAS remote model named PAI_EAS_Qwen25_Omni_3B and an Object Table, you can invoke the AI function. For detailed steps, see Use a MaxCompute remote model to automatically generate e-commerce product descriptions.

    Note You can use a VPC endpoint (recommended) or a PAI-EAS public network endpoint.

    • If you use a PAI-EAS VPC endpoint, you must enable a leased line connection and specify the configured network connection name when you invoke the AI function. For configuration details, see VPC access solution (leased line connection).

    • If you use a PAI-EAS public network endpoint, you must add the endpoint to the MaxCompute external network allowlist before invoking the AI function. For configuration instructions, see Network Access Procedure.

    Call the model with an audio URL and a prompt to tag the audio category. In this example, the Object Table is named music_demo. You can download the audio dataset from the following download link.

    SELECT
      key,
      AI_GENERATE(
        PAI_EAS1_Qwen25_Omni_3B, v1, audio_url,
        "Accurately analyze the music genre of the audio. The result must be one of the following seven options, with no additional information: Classical, Country, Hip-hop, Metal, Pop, Reggae, Rock","AUDIO"
      ) as item_catagory
      from (
        select GET_SIGNED_URL_FROM_OSS(
          'project_test_model.default.music_demo', key, 604800
        ) as audio_url, key as key
        from project_test_model.default.music_demo
    ) Limit 42;

    Click to expand and view results

    +------+---------------+
    | key  | item_catagory |
    +------+---------------+
    | classical.00000.wav | Classical     |
    | classical.00001.wav | Classical     |
    | classical.00002.wav | Classical     |
    | classical.00003.wav | Classical     |
    | classical.00004.wav | Classical     |
    | classical.00005.wav | Classical     |
    | country.00000.wav | Country       |
    | country.00001.wav | Country       |
    | country.00002.wav | Country       |
    | country.00003.wav | Country       |
    | country.00004.wav | Country       |
    | country.00005.wav | Rock          |
    | hiphop.00000.wav | Hip-hop       |
    | hiphop.00001.wav | Hip-hop       |
    | hiphop.00002.wav | Hip-hop       |
    | hiphop.00003.wav | Hip-hop       |
    | hiphop.00004.wav | Hip-hop       |
    | hiphop.00005.wav | Hip-hop       |
    | metal.00000.wav | Metal         |
    | metal.00001.wav | Metal         |
    | metal.00002.wav | Rock          |
    | metal.00003.wav | Metal         |
    | metal.00004.wav | Metal         |
    | metal.00005.wav | Metal         |
    | pop.00000.wav | Pop           |
    | pop.00001.wav | Pop           |
    | pop.00002.wav | Pop           |
    | pop.00003.wav | Pop           |
    | pop.00004.wav | Rock          |
    | pop.00005.wav | Pop           |
    | reggae.00000.wav | Reggae        |
    | reggae.00001.wav | Reggae        |
    | reggae.00002.wav | Reggae        |
    | reggae.00003.wav | Reggae        |
    | reggae.00004.wav | Reggae        |
    | reggae.00005.wav | Reggae        |
    | rock.00000.wav | Rock          |
    | rock.00001.wav | Rock          |
    | rock.00002.wav | Rock          |
    | rock.00003.wav | Rock          |
    | rock.00004.wav | Rock          |
    | rock.00005.wav | Rock          |
    +------+---------------+

FAQ

Troubleshooting public models

Symptom: Calling a public model through the model computing service returns 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 is not activated for your project's region (for example, cn-shanghai), which prevents the invocation of AI inference resources.

Solution: Activate the model computing service for your project's region in the Alibaba Cloud console. For detailed instructions, see Purchase and use the MaxCompute model computing service.

Troubleshooting PAI-EAS remote models

If a call to a MaxCompute remote model using the AI_GENERATE function returns an empty result, follow these steps to troubleshoot the issue:

  1. Check Create remote model parameters

    • PAI_EAS_SERVICE_NAME:

      Check if the EAS service has been added to a service group.

      • If so, you must specify the name in the format group_name.service_name, such as group.service_name.

      • If not, specify only the EAS service name.

    • Endpoint:

      When you set the model ENDPOINT parameter, do not provide the information that follows .com in the address, regardless of whether you use the EAS public network address or the VPC address (recommended). An example of a correct address format is: http://1*************70.cn-shanghai.pai-eas.aliyuncs.com.

    • APIKEY:

      Verify that the PAI-EAS service token is correct.

    To obtain the parameter values, see 6.1 Obtain an endpoint and token.

  2. Check AI_GENERATE function parameters

    • For audio and video data, the BINARY type is not supported for unstructured_data.

    • If you use a STRING URL and the function returns an empty result, verify that the type parameter is specified correctly. Supported values are IMAGE, AUDIO, and VIDEO. For example:

      SELECT
        key,
        AI_GENERATE(
          PAI_EAS1_Qwen25_Omni_3B, v1, audio_url,
          "Accurately analyze the music genre of the audio. The result must be one of the following seven options, with no additional information: Classical, Country, Hip-hop, Metal, Pop, Reggae, Rock","AUDIO"
        ) as item_catagory
        from (
          select GET_SIGNED_URL_FROM_OSS(
            'project_test_model.default.music_demo', key, 604800
          ) as audio_url, key as key
          from project_test_model.default.music_demo
      ) Limit 42;
  3. Check the runtime status

    Manually disable the query acceleration task to eliminate the impact of the SQLRT task on model calls. Before the AI_GENERATE function call, add the following: set odps.mcqa.disable=true;

    In some scenarios, query acceleration is enabled by default when you submit a job. This feature can interfere with remote model invocation and must be manually disabled.

  4. Check PAI-EAS resources

    Ensure that the PAI-EAS resources for the remote model are sufficient. During job execution, check for resource-related issues, such as out-of-memory (OOM) errors. If such issues occur, scale up the resources and retry the job.