AI_GENERATE is an AI function in MaxCompute that calls a model to run an inference task based on a prompt. This function supports tasks such as natural language generation, complex logic analysis, sentiment analysis, and multimodal understanding. You can use SQL to process unstructured data without requiring external services.
Command format
The AI_GENERATE function has different signatures for calling a large language model (LLM) and a multimodal large language model (MLLM).
When
model_typeis LLM, the function signature is as follows:STRING AI_GENERATE( STRING <model_name> , STRING <version_name>, STRING <prompt> [, STRING <model_parameters>] );When
model_typeis MLLM, the function signature is as follows:STRING AI_GENERATE( STRING <model_name>, STRING <version_name>, STRING | BINARY <unstructured_data> , STRING <prompt> [, STRING <model_parameters>] );
Parameters
model_name: Required. A
STRINGthat specifies the name of the model to use. The model can be an LLM or an MLLM.version_name: Required. A
STRINGthat specifies the name of the model version to use. To use the default version, set this parameter toDEFAULT_VERSION.prompt: Required. A
STRINGthat specifies the prompt to send to the model. The value can be aSTRINGconstant, a column name, or an expression.unstructured_data: Required when
model_typeis MLLM. ASTRINGorBINARYvalue that specifies the multimodal data to process. You can specify an image URL as aSTRINGor the image's binary data asBINARY. If you use theBINARYdata type, aBINARYinput parameter must also be specified when the model is created.model_parameters: Optional. A
STRING. You can use this parameter to specify parameters for the model call, such as max_tokens, temperature, and top_p, in the following format:'{"max_tokens": 500, "temperature": 0.6, "top_p": 0.95}'. The parameters are described as follows:max_tokens: The maximum number of tokens in the output from a single model call. For public models in MaxCompute, the default value is 4096.
temperature: A value between 0 and 1 that controls the randomness of the model's output. A higher value makes the output more diverse and random.
top_p: A value between 0 and 1 that controls the randomness and diversity of the model's output. A higher value makes the output more random.
To use an AI function to call a public model for inference, you must first run the SET odps.sql.using.public.model=true; command to enable the use of public models.
Return value
Returns a STRING that contains the content generated by the model.
Examples
Example 1: Call a model for content generation.
Call the
Qwen3-0.6B-GGUFpublic model in MaxCompute to generate content.SET odps.sql.using.public.model=true; -- The following result is returned. -- "The capital of China is **Beijing**." SELECT AI_GENERATE(bigdata_public_modelset.default.Qwen3-0.6B-GGUF,DEFAULT_VERSION,'what is the capital of China');Example 2: Call a model for sentiment analysis.
Call the
Qwen3-1.7B-GGUFpublic model in MaxCompute to perform sentiment analysis on user comments.SET odps.sql.using.public.model=true; SELECT prompt, AI_GENERATE( bigdata_public_modelset.default.Qwen3-1.7B-GGUF, DEFAULT_VERSION, concat('Perform sentiment analysis and classification on the following comment. The output must be one of these three options: Positive, Negative, or Neutral. Comment to analyze:', prompt) ) AS generated_text FROM ( VALUES ('The weather is great today, and I''m in a good mood! It''s sunny and perfect for a walk. /no_think'), ('The weather is great today, and I''m in a good mood! It''s sunny. /no_think'), ('Technology is advancing rapidly, and artificial intelligence is changing lives. /no_think'), ('The control measures are excellent. Thumbs up to the medical staff! /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'm in a good mood! It's sunny and perfect for a walk. /no_think | "Positive" | | The weather is great today, and I'm in a good mood! It's sunny. /no_think | "Positive" | | Technology is advancing rapidly, and artificial intelligence is changing lives. /no_think | "Positive" | | The control measures are excellent. Thumbs up to the medical staff! /no_think | "Positive" | | The quality of this product is very poor. /no_think | "Negative" | +------------------------------------------------------------------------------------------------+----------------+Example 3: Call a model for multimodal data processing.
This example calls a model named
PAI_EAS_Qwen25_Omni_3B, which is a PAI-EAS remote model, to tag product categories based on an image URL and a prompt. You must also create an Object Table namedimage_demo. For more information, see Automatically generate E-commerce product descriptions using a MaxCompute remote model.NoteMaxCompute remote models currently support only PAI-EAS Internet addresses. When you use an AI function to call a model, you must add the PAI-EAS Internet address as an available external network address in MaxCompute. For more information, see Network enablement process.
SELECT key, AI_GENERATE( PAI_EAS_Qwen25_Omni_3B, v1, image_url, "Recognize and extract the product category from the e-commerce product sales poster. The result must be one of the following six options: Cosmetics, Apparel, Daily Necessities, Food, Other, Electronic Products. Do not include any other text or information." ) AS item_catagory FROM ( SELECT GET_SIGNED_URL_FROM_OSS( 'pd_test_model.default.image_demo', key, 604800 ) AS image_url, key AS key FROM pd_test_model.default.image_demo ) Limit 10; -- The following result is returned: +--------------------+---------------------+ | key | item_catagory | +--------------------+---------------------+ | alimamazszw-1.jpg | Food | | alimamazszw-10.jpg | Electronic Products | | alimamazszw-11.jpg | Electronic Products | | alimamazszw-12.jpg | Cosmetics | | alimamazszw-13.jpg | Electronic Products | | alimamazszw-14.jpg | Daily Necessities | | alimamazszw-15.jpg | Cosmetics | | alimamazszw-16.jpg | Cosmetics | | alimamazszw-18.jpg | Daily Necessities | +--------------------+---------------------+