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_typeis an LLM, the function signature is as follows:STRING AI_GENERATE( STRING <model_name> , STRING <version_name>, STRING <prompt> [, STRING <model_parameters>] );When
model_typeis 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_typemust 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
STRINGtype.unstructured_data: Required when
model_typeis 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, andtop_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.
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. |
+-----+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;
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 quotaCause: 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:
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 asgroup.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.
Check
AI_GENERATEfunction parametersFor audio and video data, the
BINARYtype is not supported for unstructured_data.If you use a STRING URL and the function returns an empty result, verify that the
typeparameter is specified correctly. Supported values areIMAGE,AUDIO, andVIDEO. 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;
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.
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.