All Products
Search
Document Center

PolarDB:Getting started

Last Updated:May 16, 2026

Polar_AI is an AI extension for the cloud-native database PolarDB. It integrates advanced AI models and algorithms to bridge the gap between databases and modern artificial intelligence, enabling databases to perform tasks such as machine learning and natural language processing (NLP). This topic describes the basic features of the Polar_AI engine, including how to call large AI models to perform text-to-embedding conversion, text sentiment classification, and extend SQL to define custom models that interact with other AI model services.

Key concepts

  • NLP: Natural language processing is a field of AI that focuses on enabling computers to understand and generate human language. This field includes technologies such as text classification, sentiment analysis, machine translation, and dialogue systems.

  • Embedding: A core concept in machine learning and natural language processing (NLP), embedding converts high-dimensional and sparse feature vectors, such as words in a dictionary or image pixels, into low-dimensional, dense, and continuous vector representations.

Benefits

You can use standard SQL to easily call and manage AI models directly within your database. This method provides the following key benefits:

  • Ease of use: Use basic SQL to perform end-to-end AI operations, from model training to inference. You do not need deep AI expertise or complex programming skills. This significantly lowers the barrier to entry and allows more users to build AI applications.

  • Flexibility and customization: Beyond a set of pre-configured AI algorithms, you can add new models to meet your business needs and extend functionality with a few simple SQL statements. This allows you to efficiently handle various tasks such as text classification, image recognition, and time-series forecasting within a unified framework.

  • Seamless data integration: Polar_AI saves AI-generated results directly to the database, eliminating the extra steps typically required to integrate model outputs into existing systems. You can easily join AI results with other structured or unstructured data for comprehensive analysis and better-informed decision-making.

  • Data security: Your data remains within the secure and reliable database environment throughout the computation process, preventing information leakage during data transfers. Enterprise-grade features such as fine-grained access control, access auditing, and encryption further enhance system security.

  • Excellent performance: All computation runs within the database, reducing overhead from data migration and resulting in excellent response times and throughput for applications that require real-time performance.

  • Enterprise-grade features: Polar_AI inherits all the advanced features of the cloud-native database PolarDB, including automatic failover, online scaling, and tiered storage. These features provide a solid foundation for building stable and reliable large-scale data processing platforms.

Quick start

  1. Create an extension

    Run the following statement as a :

    CREATE EXTENSION IF NOT EXISTS polar_ai;
  2. Understand the function for creating custom models

    Prepare the following information for the function parameters:

    • A deployed and running model service. This example uses the DeepSeek-R1-Distill-Qwen-7B model deployed on Platform for AI (PAI).

      Note

      The deployed model service must be in the same Virtual Private Cloud (VPC) as your PolarDB cluster.

      After deployment, go to Service Details > Call Information to view and record the VPC Call Information. This information includes the Access address and token, which are used to create the model.

    • Define model input and output functions.

      There are two invocation methods: Chat and Completions. This topic uses Completions as an example.

      • Model input function:

        When you call the DeepSeek-R1-Distill-Qwen-7B model over HTTP, the request body is as follows:

        {
          "model": "DeepSeek-R1-Distill-Qwen-7B",
          "prompt": "Hello!"
        }

        In the request body, the parameters model and prompt are required as input for the model. The model input function can be defined as follows:

        CREATE OR REPLACE FUNCTION ai_text_in_fn(model_name text, content text)
            RETURNS jsonb
            LANGUAGE plpgsql
            AS $function$
            BEGIN
                RETURN ('{"model": "'|| model_name ||'","prompt":"'|| content ||'"}')::jsonb;
            END;
            $function$;
      • Model output function:

        When you call the DeepSeek-R1-Distill-Qwen-7B model over HTTP, the response body is as follows:

        {
            "id": "8e44xxxx",
            "object": "text_completion",
            "created": 1744355891,
            "model": "DeepSeek-R1-Distill-Qwen-7B",
            "choices": [
                {
                    "index": 0,
                    "text": " I have a\n\n\n</think>\n\nHello! How can I assist you today? ",
                    "logprobs": null,
                    "finish_reason": "stop",
                    "matched_stop": 151643
                }
            ],
            "usage": {
                "prompt_tokens": 3,
                "total_tokens": 21,
                "completion_tokens": 18,
                "prompt_tokens_details": null
            }
        }

        Because you only need to obtain the content of choices[0].text and the output is a block of text that requires no further processing, you do not need to define a model output function.

  3. Create an AI model

    Call the function to create the model. When you create the model, configure the following parameters:

    • The model call address model_url is the access address of the model deployed on Platform for AI (PAI), to which you must append the invocation method /v1/completions.

    • In the model configuration model_config, the token is the token for the model deployed on Platform for AI (PAI).

    • The model input transformation function model_in_transform_fn is the ai_text_in_fn function created in the preceding steps.

    SELECT polar_ai.ai_createmodel('my_test_pai_model', '<EAS Endpoint>/v1/completions','Alibaba','EAS large language model','DeepSeek-R1-Distill-Qwen-7B','{"author_type": "token", "token": "<EAS Token>"}', NULL,'ai_text_in_fn'::regproc,NULL);
    Note

    You can run the SELECT * FROM polar_ai._ai_models; query to view created model information.

  4. Create a function to call the AI model

    Use the function to create a custom model-calling function:

    CREATE OR REPLACE FUNCTION my_text_pai_model_func(model_id text, content text)
        RETURNS text
        AS $$ select (polar_ai.AI_CALLMODEL($1,$2)::jsonb -> 'choices' -> 0 ->> 'text')::text AS result $$
        LANGUAGE 'sql' IMMUTABLE;
  5. Call the model by using SQL

    SELECT my_text_pai_model_func('my_test_pai_model', 'Hello');

    The following output is returned:

            my_text_pai_model_func        
    --------------------------------------
     Hello! How can I assist you today? 
    (1 row)