All Products
Search
Document Center

ApsaraDB for SelectDB:AI_GENERATE

Last Updated:Mar 28, 2026

Passes a prompt to a large language model (LLM) and returns the generated text as a SQL value, letting you run AI inference directly inside a query.

Syntax

AI_GENERATE([<resource_name>], <prompt>)

Parameters

ParameterRequiredDescription
<resource_name>NoThe name of the AI resource to use. If omitted, the session default set by SET default_ai_resource is used.
<prompt>YesThe prompt text that guides the LLM to generate a response.

Return value

  • Returns the text generated by the LLM.

  • Returns NULL if the input value is NULL.

  • Output may vary between calls because the response is generated by an LLM.

Set a default resource

Before calling AI_GENERATE without specifying <resource_name>, set a session-level default:

SET default_ai_resource = '<your_resource_name>';

Replace <your_resource_name> with the name of the AI resource configured in your SelectDB environment.

Examples

Generate a response from a literal prompt

SET default_ai_resource = 'resource_name';
SELECT AI_GENERATE('Describe SelectDB in a few words') AS Result;

Output:

+---------------------------------------------------------+
| Result                                                  |
+---------------------------------------------------------+
| "SelectDB is a fast, real-time analytics database."     |
+---------------------------------------------------------+

Specify a resource per query

Pass <resource_name> directly to override the session default for a single call:

SELECT AI_GENERATE('my_resource', 'Summarize the latest trends in real-time analytics') AS summary;

Run inference on table rows

Use AI_GENERATE in a SELECT statement to process each row in a table:

SELECT
    id,
    review_text,
    AI_GENERATE(
        CONCAT('Classify the sentiment of this review as Positive, Negative, or Neutral: ', review_text)
    ) AS sentiment
FROM customer_reviews
LIMIT 100;

Each row's review_text is appended to the prompt, returning one generated response per row.