All Products
Search
Document Center

ApsaraDB RDS:AI (rds_ai)

Last Updated:Aug 25, 2026

RDS for PostgreSQL offers the rds_ai extension, which integrates advanced models from Alibaba Cloud Model Studio, including Qwen, text embedding. This extension enables a wide range of applications, such as LLM-based Q&A, text-to-vector conversion, top-N similar vector retrieval, and Retrieval-Augmented Generation (RAG). In addition, rds_ai supports custom models, allowing you to add models to build diverse AI applications.

Prerequisites

  • Ensure your RDS for PostgreSQL instance meets the version requirements in the following table:

    Major version

    Minor version

    PostgreSQL 16

    20241230 or later

    PostgreSQL 14, PostgreSQL 15, and PostgreSQL 17

    20250430 or later

    PostgreSQL 18

    20251130 or later

    To update the minor engine version, see Update the minor engine version.

  • You must have a privileged account for your RDS for PostgreSQL instance. For more information, see Create an account.

  • The model used in this document is provided by Alibaba Cloud Model Studio. You must first activate the service and obtain an API key. For more information, see Get an API key.

Network configuration

By default, an RDS for PostgreSQL instance cannot access external networks. To enable the instance to access external models, you must configure a NAT gateway for its VPC. For more information about NAT gateways, see Use the SNAT feature of an Internet NAT gateway to access the Internet.

Create and drop extensions

Important

Before you install an extension, ensure that your RDS for PostgreSQL instance is running a major and minor version that supports it. For more details, see prerequisites in this topic.

  • Install an extension on the Plug-ins page.

    1. Go to the Instances page, select a region at the top, and then click the ID of the target instance.

    2. In the navigation pane on the left, click Plug-ins.

    3. On the Extension Marketplace tab, find the rds_ai extension and click Install.

    4. In the dialog box, select the target database and account, and then click Install.

    (Optional) On the Extension Management page, you can uninstall extensions from the Installed Extensions tab.

  • Manage extensions with SQL statements

    Create an extension

    CREATE EXTENSION IF NOT EXISTS rds_ai CASCADE;
    Note
    • Only a privileged account can execute this statement. To create a privileged account, see Create an account.

    • Creating the rds_ai extension also creates the pgvector and pgsql-http extensions.

    • To view all installed extensions, execute the SELECT * FROM pg_extension; statement.

    Drop an extension

    DROP EXTENSION rds_ai;

Default models

The rds_ai extension supports the default models listed below. You can also create custom models.

API

Parameter

Type

Default

prompt API

rds_ai.default_prompt_model

enum

  • qwen-plus (default)

  • qwen-max

  • qwen-turbo

embedding API

rds_ai.default_embed_model

enum

text-embedding-v3

Note

To change a default model, you must add rds_ai to the Running Value of the shared_preload_libraries instance parameter. For more information about how to configure parameters, see Set instance parameters. For example, change the Running Value to 'pg_stat_statements,auto_explain,rds_ai'.

The default models in the rds_ai extension provide the following capabilities:

Basic settings

  1. To call a large language model using rds_ai, you must first configure the API key.

    -- Set the API key for the target model.
    SELECT rds_ai.update_model('qwen-plus', 'token', 'sk-****'); 
    
    -- Set the API key for all models in rds_ai.model_list.
    SELECT rds_ai.update_model(model_name,'token','sk-****') FROM rds_ai.model_list;
  2. Run the following statements to configure the URL for each default model.

    --qwen-plus model
    SELECT rds_ai.update_model('qwen-plus', 'uri', 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation'); 
    
    --qwen-max model
    SELECT rds_ai.update_model('qwen-max', 'uri', 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation'); 
    
    --qwen-turbo model
    SELECT rds_ai.update_model('qwen-turbo', 'uri', 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation'); 
    
    --text-embedding-v3 model
    SELECT rds_ai.update_model('text-embedding-v3', 'uri', 'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding'); 
  3. The rds_ai extension uses the pgsql-http extension to remotely call models. Configure the following timeout settings to interrupt long-running requests.

    Note

    These timeout settings are session-level and must be reconfigured for each new connection.

    -- Set the request timeout period in milliseconds.
    SET http.timeout_msec TO 200000;
    
    SELECT http.http_set_curlopt('CURLOPT_TIMEOUT', '200000');
    
    -- Set the connection timeout period.
    SELECT http.http_set_curlopt('CURLOPT_CONNECTTIMEOUT_MS', '200000');

LLM-based Q&A

By default, the rds_ai extension uses the Qwen text generation model for LLM-based Q&A.

  1. (Optional) Set the default model for LLM-based Q&A. If you do not set this parameter, the system uses the qwen-plus model by default.

    Note

    To change a default model, you must add rds_ai to the Running Value of the shared_preload_libraries instance parameter. For more information about how to configure parameters, see Set instance parameters. For example, change the Running Value to 'pg_stat_statements,auto_explain,rds_ai'.

    SET rds_ai.default_prompt_model TO 'qwen-max';
  2. Use rds_ai.prompt to call the default large language model for Q&A. For example:

    SELECT rds_ai.prompt('Give me a recipe for a dish made with carrots, potatoes, and eggplants.');

    Full API call:

    SELECT rds_ai.prompt(
      model_name=>'qwen-plus', -- The model name.
      content=>'Give me a recipe for a dish made with carrots, potatoes, and eggplants.',  -- The question.
      args=>'{"top_p": 0.7}'::jsonb -- The parameters for calling the large language model.
    );

Text-to-vector conversion

By default, the rds_ai extension uses the text-embedding-v3 model for text-to-vector conversion.

  • By default, the output is a dense vector.

    SELECT rds_ai.embed(
        'On the wind-swept plain, gibbons cry; the islet is clear, the sand is white, birds wheel back. Leaves fall without end; the Yangtze rolls on, never ceasing.' -- The text to convert to a vector.
    );
  • Specify the output as a sparse vector.

    -- Convert text to a sparse vector, which has 250002 dimensions by default.
    SELECT rds_ai.embed(
        content=>'On the wind-swept plain, gibbons cry; the islet is clear, the sand is white, birds wheel back. Leaves fall without end; the Yangtze rolls on, never ceasing.',
        args=>'{"output_type": "sparse"}'::jsonb -- The parameter list.
    );

Store the resulting vectors in a destination table.

  1. Create the test_embed table.

    CREATE TABLE test_embed(a text, b vector(1024), c sparsevec(250002));
  2. Insert the text that you want to convert.

    INSERT INTO test_embed (a) values ('hello world');
  3. Use rds_ai.embed to call the text-embedding-v3 model and write the results to the test_embed table.

    • Write a dense vector.

      UPDATE test_embed 
      SET b = rds_ai.embed(a, '{"output_type": "dense"}'::jsonb)::vector;
    • Write a sparse vector.

      UPDATE test_embed 
      SET c = rds_ai.embed(a, '{"output_type": "sparse"}'::jsonb)::sparsevec;

Top-N similar vector retrieval

By default, the rds_ai extension uses the text-embedding-v3 model for similar vector retrieval. For example:

  1. Create a test table.

    -- Create a test table named test_rag.
    CREATE TABLE test_rag (
        id SERIAL PRIMARY KEY,
        chunk TEXT
    );
    
    -- Add an embedding column to store the vector converted from the chunk.
    ALTER TABLE test_rag ADD COLUMN embedding VECTOR(1024);
    
    -- Populate the embedding column.
    UPDATE test_rag SET embedding=rds_ai.embed(chunk)::vector;
  2. Create a vector index.

    Note

    When you create a vector index, you must use the vector_cosine_ops operator class.

    -- Create an HNSW index.
    CREATE INDEX ON test_rag USING hnsw (embedding vector_cosine_ops);
    -- Create an IVFFlat index.
    CREATE INDEX ON test_rag USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
  3. Use rds_ai.retrieve, which calls the text-embedding-v3 model, to perform vector retrieval in the test table.

    SELECT * FROM rds_ai.retrieve
    ('Why is PostgreSQL considered the most advanced open source database',
    'public', 'test_rag', 'chunk', 'embedding');

    Full API call:

    SELECT * FROM rds_ai.retrieve
    (
      embed_model=>'text-embedding-v3', -- The embedding model to use.
      question=>'Why is PostgreSQL considered the most advanced open source database',
      source_schema=>'public', -- The schema of the table for the similarity search.
      source_table=>'test_rag', -- The table for the similarity search.
      chunk_col=>'chunk', -- The column that contains text chunks.
      vector_col=>'embedding', -- The column that contains vectors.
      -- The following are optional parameters.
      topn=>10, -- The number of top results to return.
      embed_args=>'{}'::jsonb, -- The parameters for the embedding model.
      distance_type=>'cosine' -- The distance algorithm. Valid values: L1, L2, cosine, and inner product.
    );

RAG-based Q&A

By default, the rds_ai extension uses the text-embedding-v3 model and the Qwen text generation model for RAG-based Q&A.

-- Embedding model: text-embedding-v3. Text generation model: specified by the rds_ai.default_prompt_model parameter.
SELECT * FROM rds_ai.rag
('Why is PostgreSQL considered the most advanced open source database', 
'public', 'test_rag', 'chunk', 'embedding');

Full API call:

SELECT * FROM rds_ai.rag
(
  embed_model=>'text-embedding-v3', -- The embedding model to use.
  prompt_model=>'qwen-plus', -- The prompt model to use.
  question=>'Why is PostgreSQL considered the most advanced open source database',
  source_schema=>'public', -- The schema of the table for the similarity search.
  source_table=>'test_rag', -- The table for the similarity search.
  chunk_col=>'chunk', -- The column that contains text chunks.
  vector_col=>'embedding', -- The column that contains vectors.
  -- The following are optional parameters.
  topn=>10,
  embed_args=>'{}'::jsonb,  -- The parameters for the embedding model.
  prompt_args=>'{}'::jsonb, -- The parameters for the prompt model.
  distance_type=>'L2' -- The distance algorithm. Valid values: L1, L2, cosine, and inner product.
);

Custom models

Important

If you have any issues adding a custom model, please contact us.

You can add a custom model using the rds_ai.model_list metadata table. The following table describes the fields in rds_ai.model_list.

Parameter

Type

Description

model_name

name

The name of the model. This field is the primary key.

request_type

text

The HTTP request method, such as POST.

request_header

http.http_header[]

The request header contains information such as authentication credentials. This data type is provided by the pgsql-http extension.

uri

text

The endpoint URL of the model.

content_type

text

The content type of the request, such as application/json.

content_template

text

The template for the request body. It can contain placeholders that are populated when you invoke the model.

json_path

text

An SQL statement used to parse the HTTP response and extract the required content.

token

text

The authentication token included in the request header.

Add a custom model

SELECT rds_ai.add_model(
    'test-model',                   -- Model name
    'POST',                         -- HTTP request method
    ARRAY[('Authorization', 'Bearer %s')]::http.http_header[], -- Request header
    'https://****.com',           -- Request URL
    'application/json',            -- Content type
    '{"key":"%s"}',               -- Request body template
    'SELECT %L'          -- SQL statement to parse the response
);

Use a custom model

  • Use the rds_ai.raw_invoke_model function to send a request based on the model's configuration. The function populates the request template with an ARRAY and returns the complete HTTP response.

    SELECT * FROM
    rds_ai.raw_invoke_model('qwen-plus', ARRAY['who are you']);
  • Use the rds_ai.invoke_model function to send a request based on the model's configuration and extract specific fields from the response using the json_path setting.

    SELECT * FROM
    rds_ai.invoke_model('qwen-plus', ARRAY['who are you']);

Delete a custom model

SELECT rds_ai.del_model('test-model');

Add a PAI-RAG model

We recommend deploying PAI-EAS and your RDS for PostgreSQL instance in the same VPC. When you configure the model in the rds_ai extension, use the VPC endpoint of the PAI service. This keeps all communication within your VPC for enhanced security.

  1. Deploy a RAG service in PAI-EAS. For more information, see Deploy an LLM-based RAG chatbot by using PAI-EAS and RDS for PostgreSQL.

  2. Obtain the invocation information for the RAG service.

    1. Click the name of the RAG service to open the Service Details page.

    2. In the Basic Information section, click View Invocation Information.

    3. In the Invocation Information dialog box, obtain the service endpoint and token.

  3. Add the PAI-RAG model.

    SELECT rds_ai.add_model(
        'pai-rag',      -- Model name
        'POST',         -- Request method
        ARRAY[('Authorization','%s')]::http.http_header[],  -- Request header,
        'http://rds-pai-rag-demo.****.cn-hangzhou.pai-eas.aliyuncs.com/service/query',  -- Request URL
        'application/json',  -- Content type
        '{
            "question": "%s"
        }',  -- Request body
        'SELECT (%L::jsonb->''answer'')::text'  -- Parse path
    );
    Note
    • Replace the model URL with your actual service endpoint and append /service/query.

    • For more information about invoking PAI-RAG models, see Call PAI models by using an API.

  4. Configure the token for the PAI-RAG model.

    SELECT rds_ai.update_model('pai-rag', 'token','MTFkYjMwZjgzYzA1YmE2N2YyNWMxM2NkNDVjMjEzNjYxMDAzMzE5****');

Verify the new PAI-RAG model. For example:

SELECT rds_ai.invoke_model('pai-rag', ARRAY['Which parameters are related to WAL log accumulation?']);

Add a Function Compute RAG model

  1. Deploy an AgentCraft application in Function Compute. For more information, see Deploy AgentCraft in the cloud.

  2. Create an agent and configure its client access.

    For example, as described in Connect an agent to a DingTalk chatbot, obtain and save the service endpoint and token when you configure client access.

  3. Add the Function Compute RAG model.

    SELECT rds_ai.add_model(
        'fc-rag',      -- Model name
        'POST',         -- Request method
        ARRAY[('Authorization','Bearer %s')]::http.http_header[],  -- Request header,
        'https://agentcrckend-de-obnhjsknam.cn-hangzhou.fcapp.run/v1/chat/completions',  -- Request URL
        'application/json',  -- Content type
        '{ 
            "messages":[ { "role": "user", "content": "%s" } ], 
            "stream": false, 
            "max_tokens": 1024 
         }', 
        'SELECT (%L::jsonb->''choices''->0->''message''->>''content'')::text'  -- Parse path
    );
    Note

    Replace the model URL with the actual URL and append /completions.

  4. Configure the token for the Function Compute RAG model.

    SELECT rds_ai.update_model('fc-rag', 'token','8UiGAziWgYGPxM3qR5sAChBfDJRt****');

Verify the new Function Compute RAG model. For example:

SELECT rds_ai.invoke_model('fc-rag', ARRAY['Which parameters are related to WAL log accumulation?']);

Functions

rds-ai.raw_invoke_model: Invokes a custom model and returns the complete HTTP response (http.http_response).

rds-ai.raw_invoke_model(
    model_name TEXT, 
    params_list TEXT[]
) 
RETURNS http.http_response

Parameter

Type

Description

Example

model_name

text

The name of the model to invoke.

'qwen-plus'

params_list

text[]

Sequentially fill in the content of the model's content_template field.

ARRAY['who are you', '{}']

rds-ai.invoke_model: Invokes a custom model, parses the HTTP response using the json_path field from the metadata table, and returns the result.

rds-ai.invoke_model(
    model_name TEXT, 
    params_list TEXT[]
) 
RETURNS http.http_response

Parameter

Type

Description

Example

model_name

text

The name of the model to invoke.

'qwen-plus'

params_list

text[]

Values that sequentially populate the placeholders in the model's content_template field.

ARRAY['who are you', '{}']

rds_ai.embed: Generates a text embedding using a specified model.

rds_ai.embed(
    model_name TEXT, 
    content TEXT, 
    args jsonb DEFAULT '{}' 
) RETURNS text

Parameter

Type

Description

Example

model_name

text

The name of the model to invoke.

Note

If unspecified, the default model (text-embedding-v3) is used.

'text-embedding-v3'

content

text

The input text to embed.

'who are you'

args

jsonb

Additional parameters to pass to the model.

'{"output_type": "dense"}'

rds_ai.prompt: Runs a prompt on a specified model.

rds_ai.prompt(
    model_name TEXT, 
    content TEXT, 
    args jsonb DEFAULT '{}'
) 
RETURNS text

Parameter

Type

Description

Example

model_name

text

The name of the model to invoke.

Note

If omitted, the default model, qwen-plus, is invoked.

'qwen-plus'

content

text

The prompt to send to the model.

'who are you'

args

jsonb

Additional parameters for the model.

'{"top_p": 0.2}'

rds_ai.retrieve: Finds vectors similar to an input text by using a specified model.

rds_ai.retrieve(
    model_name TEXT, 
    question TEXT, 
    source_schema TEXT, 
    source_table TEXT, 
    chunk_col TEXT, 
    vector_col TEXT, 
    topn INT DEFAULT 10, 
    embed_args jsonb DEFAULT '{}', 
    distance_type TEXT DEFAULT 'L2'
) 
RETURNS TABLE(chunk TEXT, distance float);

Parameter

Type

Description

Example

model_name

text

The name of the embedding model to use.

Note

If you do not specify this parameter, the system uses the default model, text-embedding-v3.

'text-embedding-v3'

question

text

The input text for the similarity search.

'who are you'

source_schema

text

The schema of the table to search.

'public'

source_table

text

The name of the table to search.

'vec_tbl'

chunk_col

text

The name of the column that contains the original text chunks.

'chunk'

vector_col

text

The name of the vector column to search.

'vec'

topn

int

The number of nearest neighbors to return. The default is 10.

20

embed_args

jsonb

Specifies parameters for the text-to-vector conversion. These parameters are identical to the args parameter of the rds_ai.embed function.

'{"output_type": "dense"}'

distance_type

text

The distance metric for calculating vector similarity. The default is L2.

'L1'

rds_ai.rag: Executes a prompt on a specified model using results from a vector retrieval.

rds_ai.rag(
    embed_model TEXT,
    prompt_model TEXT,
    question TEXT,
    source_schema TEXT,
    source_table TEXT,
    chunk_col TEXT,
    vector_col TEXT,
    topn INT DEFAULT 10,
    embed_args jsonb DEFAULT '{}',
    prompt_args jsonb DEFAULT '{}',
    distance_type TEXT DEFAULT 'L2'
) 
returns text

Parameter

Type

Description

Example

embed_model

text

The model used for text-to-vector conversion of the question.

Note

You should use a model designed for creating text embeddings.

'qwen-plus'

question

text

The input question for vector retrieval.

'who are you'

source_schema

text

The schema containing the source table.

'public'

source_table

text

The name of the source table.

'vec_tbl'

chunk_col

text

The column containing the original text chunks.

'chunk'

vector_col

text

The name of the vector column.

'vec'

topn

int

The number of most relevant vectors to retrieve (Top N). Default: 10.

20

embed_args

jsonb

Parameters for text-to-vector conversion, which correspond to the args parameter of the rds_ai.embed function.

'{"output_type": "dense"}'

prompt_args

jsonb

Parameters for prompt execution, which correspond to the args parameter of the rds_ai.prompt function.

'{"top_p": 0.2}'

distance_type

text

The method for vector distance calculation. Default: L2.

'L1'

rds_ai.show_models: Displays the metadata for configured models.

rds_ai.show_models() 
RETURNS setof rds_ai.model_list

rds_ai.del_model: Deletes a specified model.

rds_ai.del_model (model_name text) 
RETURNS void

Parameter

Type

Description

Example

model_name

text

The name of the model to delete.

'qwen-plus'

rds_ai.add_model: Adds a model configuration.

rds_ai.add_model(
    model_name TEXT,
    request_type TEXT,
    request_header http.http_header[],
    uri TEXT,
    content_type TEXT,
    content_template TEXT,
    json_path TEXT
) 
RETURNS TEXT

Parameter

Type

Description

Example

model_name

TEXT

The name of the model.

'text-embedding-v3'

request_type,

TEXT

The request method.

'POST'

request_header

http.http_header[]

The headers for the HTTP request. The value of the token field replaces the %s placeholder during invocation.

ARRAY[('Authorization', 'Bearer %s')]

uri

TEXT

The URL of the model.

'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding'

content_type

TEXT

The content type of the request.

'application/json'

content_template

TEXT

The request body template. When you call the rds_ai.invoke_model function, the param_list parameter populates the placeholders in this template. The value for the parameters key must be a JSON object.

'{

"model": "text-embedding-v3",

"input": {"texts": ["%s"]},

"parameters": %s

}'

json_path

TEXT

A JSONPath expression to parse the JSON response from the model. When you call the rds_ai.invoke_model function, this expression extracts data from the response.

If you are unsure of the specific path, set this parameter to the special value 'SELECT %L' to return the entire response body. Otherwise, provide a standard JSONPath expression to extract the desired data.

'SELECT %L'

rds_ai.update_model: Updates a configured model's information.

rds_ai.update_model(
    model_name TEXT,
    config_name TEXT, 
    value TEXT
) 
RETURNS void

Parameter

Type

Description

Example

model_name

TEXT

The name of the model to update.

'text-embedding-v3'

config_name

TEXT

The field to update. For valid values, see the rds_ai.add_model function.

'uri'

value

TEXT

The new value for the specified field.

'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding'