Updated on 2026-07-02 GMT+08:00

In-database Inference

AI is developing rapidly and penetrating into various industries. It has become a key driving force for technological innovation and business growth. From natural language processing to computer vision and deep learning, AI is changing the way we work and live at an unprecedented speed. In data management and processing, AI enables more intelligent, efficient, and accurate information processing. As databases become more intelligent, the pgai plugin, an AI enhancement solution for databases, integrates advanced AI capabilities into data operations for more intelligent, efficient, and accurate data processing.

DWS is a high-performance data warehouse solution that provides powerful data processing capabilities for users. Having realized the importance of AI in data analytics and decision-making, we have integrated the pgai plug-in into DWS clusters of version 9.1.1.200 or later to provide more intelligent data analytics, inference, and processing. This not only improves the speed and efficiency of data processing, but also enables enterprises to quickly gain data insights from big data analysis and make better decisions.

After the pgai plug-in is integrated into DWS, users can directly invoke LLMs and embedding models in databases without relying on an external AI platform. This makes it easier to use complex RAG applications and allows users to analyze data more efficiently and flexibly. This marks an important step for intelligent data processing using DWS and lays a solid foundation for AI and database convergence in the future.

Precautions

  • This function is in beta for clusters of version 9.1.1.200. To use this function, contact technical support to set the GUC parameter feature_support_options to enable_pgai_extension.
  • In the clusters with decoupled storage and compute, the pgai extension function (CREATE EXTENSION statement) can be executed only on the primary VWs.
  • Due to the limitations of the tiktoken library, the ai.openai_tokenize and ai.openai_detokenize functions depend on OpenAI. You can use other functions by setting base-url and api-key.
  • You are advised to purchase Huawei Cloud Model as a Service (MaaS) that supports OpenAI.

In-database Inference Functions

Run CREATE EXTENSION ai; to create the pgai extension (This extension depends on pgvector, you need to create the pgvector extension first). The following table lists the in-database inference functions supported by DWS.

Table 1 In-database Inference functions

Function

Description

Remarks

ai.set_func_model

Sets the name of the model used by a function by default.

-

ai.dws_pgai_encrypt_info

Sets and encrypts the baseurl and apikey used by all functions.

-

ai.openai_tokenize

Converts text to tokens.

Only the GPT models provided by OpenAI are supported.

ai.openai_detokenize

Converts tokens to text.

Only the GPT models provided by OpenAI are supported.

ai.openai_list_models

Displays available models.

Some large model services may not support this function.

ai.openai_list_models_with_raw_response

Displays available models in JSON format.

Some large model services may not support this function.

ai.openai_embed

Converts text to vectors.

Embedding models

ai.openai_embed_with_raw_response

Converts text to vectors in JSON format.

Embedding models

ai.openai_chat_complete

Interacts with LLMs.

LLMs

ai.openai_chat_complete_with_raw_response

Interacts with LLMs. The return is in JSON format.

LLMs

ai.openai_moderate

Classifies text based on whether it is harmful.

Moderate models

ai.openai_moderate_with_raw_response

Classifies text based on whether it is harmful. The return is in JSON format.

Moderate models

ai.chunk_text

Chunks text using a single delimiter.

-

ai.chunk_text_recursively

Chunks text using multiple delimiters.

-

ai.similarity

Computes the similarity between two pieces of input text.

Embedding models

ai.vector_cosine_similarity

Computes the similarity between two input vectors.

-

ai.classify

Classifies text based on input labels.

LLMs

ai.extract

Extracts keywords from the input text.

LLMs

ai.mask

Masks keywords in the input text.

LLMs

ai.fix_grammar

Corrects the grammar of the input text.

LLMs

ai.summarize

Generates a summary of the input text.

LLMs

ai.translate

Translates the input text into a specified language.

LLMs

ai.rank

Scores the relevance of multiple pieces of text based on the topic.

LLMs

ai.sentiment

Performs sentiment analysis on the input text.

LLMs

ai.textfilter

Filters the input text based on the given conditions.

LLMs

ai.set_func_model

Description: sets the name of the model used by a function by default. If no model name is set, the name of the model used by a function is NULL. In this case, the function is unavailable. After the model name is set, the default model is preferentially used when the function is called.

Parameters:

  • funcname: function name, which is mandatory text. For example, the value can be the name of an LLM or embedding model used by functions such as openai_embed and openai_chat_complete.
  • modelname: model name, which is mandatory text. It is determined by the model name provided by the model service.

Return value: none

Example:

1
SELECT ai.set_func_model('openai_embed', 'nomic-embed-text');

You can run the following statement to query the name of the default model used by the current function:

SELECT * from ai.ai_model_info;

The query result is as follows:

               func_name                |    model_name    
------------------------------------------------------------
 openai_tokenize                        | 
 openai_detokenize                      | 
 openai_embed_with_raw_response         | 
 openai_chat_complete                   | 
 openai_chat_complete_with_raw_response | 
 openai_moderate                        | 
 openai_moderate_with_raw_response      | 
 similarity                             | 
 classify                               | 
 extract                                | 
 mask                                   | 
 fix_grammar                            | 
 summarize                              | 
 translate                              | 
 rank                                   | 
 sentiment                              | 
 textfilter                             | 
 openai_embed                           | nomic-embed-text
(18 rows)

ai.dws_pgai_encrypt_info

Description: sets and encrypts the baseurl and apikey of the model service used by all functions.

Parameters:

  • base_url: baseurl of the model service used. This parameter is mandatory. The value is in text format.
  • api_key: api_key of the model service used. This parameter is mandatory. The value is in text format.

Return value: none

Example:

SELECT ai.dws_pgai_encrypt_info('https://example.com', 'your_api_key');

ai.openai_tokenize

Description: converts text to tokens for a given model. It is only available for the model services provided by OpenAI.

Parameters:

  • text_input: input text, which is mandatory
  • model: model name, which is mandatory text. The default value is the model name set by ai.set_func_model.

Return value: tokens of the int[] type

Example:

1
SELECT ai.openai_tokenize('have a test');

ai.openai_detokenize

Description: converts tokens to text for a given model. It is only available for the model services provided by OpenAI.

Parameters:

  • tokens: token array of the int[] type to be converted to text
  • model: model name, which is mandatory text. The default value is the model name set by ai.set_func_model.

Return value: text

Example:

1
SELECT ai.openai_detokenize(array[15365, 23456, 29889, 11, 9906, 1917, 0]);

ai.openai_list_models

Description: displays the models supported by the model service platform. This function is not supported by some model platforms.

There is no input parameter.

Return value: models of the text[] type

Example:

1
2
3
4
5
SELECT ai.openai_list_models();
                                                                             openai_list_models  
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"{'id': 'bge-large', 'created': datetime.datetime(2023, 2, 28, 18, 56, 42, tzinfo=datetime.timezone.utc), 'owned_by': 'openai'}","{'id': 'deepseek-r1-distill-qwen-1.5b', 'created': datetime.datetime(2023, 2, 28, 18, 56, 42, tzinfo=datetime.timezone.utc), 'owned_by': 'openai'}"}
(1 row)

ai.openai_list_models_with_raw_response

Description: displays the models supported by the model service platform and returns the raw response.

There is no input parameter.

Return value: models of the text type

Example:

1
2
3
4
5
SELECT ai.openai_list_models_with_raw_response();
                                                                     openai_list_models_with_raw_response
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"data": [{"id": "deepseek-v3-0324", "object": "model", "created": 1677610602, "owned_by": "openai"},{"id": "nomic-embed-text", "object": "model", "created": 1677610602, "owned_by": "openai"}], "object": "list"}
(1 row)

ai.openai_embed

Description: converts text or tokens (int[]) to vectors.

Table 2 Parameters

Parameter

Type

Mandatory

Description

input_text | input_texts | input_tokens

text | text[] | int[]

Yes

Three input types are supported. The input type is automatically adapted.

model

text

No

Model name, which is set using ai.set_func_model by default.

Return value: text, jsonb, or text, which corresponds to the input type.

Example:

Input: text:

1
2
3
4
5
SELECT ai.openai_embed('have a test');
                           openai_embed
------------------------------------------------------------------------
[0.012326963, 0.015280011, -0.17099911,..., -0.005275759, -0.03978255] 
(1 row)

Input: text[]:

1
2
3
4
5
SELECT ai.openai_embed(array['have a test1', 'have a test2']);
                                                                                     openai_embed
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"index": 0, "embedding": [-.02023241, .01978845, -.1544127, ..., -.006075094, -.057604033]}","{"index": 1, "embedding": [-.0066108834, -.0045409035, -.15522258, ..., .0038487795, -.03174798]}]
(1 row)

ai.openai_embed_with_raw_response

Description: converts text or tokens to vectors.

Table 3 Parameters

Parameter

Type

Mandatory

Description

input_text | input_texts | input_tokens

text | text[] | int[]

Yes

Three input types are supported. The input type is automatically adapted.

model

text

No

Model name, which is set using ai.set_func_model by default.

Return value: unprocessed API service response of the jsonb type

Example:

Input: text

1
2
3
4
5
SELECT ai.openai_embed_with_raw_response('have a test');
                                                                     openai_embed_with_raw_response
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"data": [{"index": 0, "object": "embedding", "embedding": [.012326963, .015280011, ..., -.005275759, -.03978255]}], "model": "nomic-embed-text:latest", "usage": {"total_tokens": 3, "prompt_tokens": 3, "completion_tokens": 0, "prompt_tokens_details": null, "completion_tokens_details": null}, "object": "list"}
(1 row)

Input: text[]:

1
2
3
4
5
SELECT ai.openai_embed_with_raw_response(array['have a test1', 'have a test2']);
                                                                     openai_embed_with_raw_response
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"data": [{"index": 0, "object": "embedding", "embedding": [-.02023241, .01978845, ..., -.006075094, -.057604033]}, {"index": 1, "object": "embedding", "embedding": [-.0066108834, -.0045409035, ..., .0038487795, -.03174798]}], "model": "nomic-embed-text:latest", "usage": {"total_tokens": 8, "prompt_tokens": 8, "completion_tokens": 0, "prompt_tokens_details": null, "completion_tokens_details": null}, "object": "list"}
(1 row)

ai.openai_chat_complete

Description: interacts with a LLM to generate text.

Table 4 Parameters

Parameter

Type

Mandatory

Description

messages | input_text

jsonb | text

Yes

Two input types are supported for interacting with a LLM.

model

text

No

Model name, which is set using ai.set_func_model by default.

Return value: answer returned by the LLM, in text format

Example:

Input: jsonb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
SELECT ai.openai_chat_complete(
'[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Do your know Huawei Cloud?"},
{"role": "assistant", "content": "Huawei Cloud is a cloud service provider."}
]'::jsonb
);
                                                                           openai_chat_complete
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Yes, **Huawei Cloud** is a significant player in the global cloud computing market, offering comprehensive cloud services powered by Huawei's expertise in ICT infrastructure. Here's what I know about it:
 ### Core Offerings:
 1. **IaaS/PaaS/SaaS Solutions**:
    - Compute, storage, networking (e.g., Elastic Cloud Server, Object Storage Service).
    - Databases (GaussDB), AI/ML platforms (ModelArts), big data services, and container orchestration.
    - Industry-specific SaaS solutions (e.g., finance, healthcare, manufacturing).
 2. **Global Infrastructure**: 
    - Data centers in over 30 regions (including Asia-Pacific, Latin America, Africa, and Europe), with ~86 Availability Zones (AZs).
 3. **AI & Advanced Tech**:
    - **Pangu Models**: Large-scale AI models for industries like healthcare, mining, and meteorology.
    - AI-native development tools and platforms like **CodeArts Snap** (AI-assisted coding).
 4. **Hybrid & Multi-Cloud**:
    - Supports hybrid deployments (e.g., HCS Online/Stack) and integrations with third-party clouds.
 5. **Security & Compliance**: 
    - End-to-end security (hardware to application layers).
    - Compliance with global standards (GDPR, C5, etc.).
 6. **Industry Ecosystems**:
    - Partner programs (e.g., Huawei Cloud Startup Program) and industry-specific solutions (e.g., smart cities, autonomous driving). ### Key Strengths:    
    - **Hardware-Software Integration**: Optimization through in-house chips (e.g., Ascend AI processors) and hardware. 
    - **Edge Computing**: Kunpeng-based edge solutions for low-latency applications.
    - **Digital Sovereignty**: Tools for data localization and regulatory adherence.
 ### Recent Updates (20232024): 
    - **Pangu 3.0**: Enhanced multi-modal industrial AI models.
    - **MetaStudio**: Digital twin/metaverse development suite.
    - **Green Initiatives**: Focus on sustainable data centers.
 ### Need more details?
    Tell me what you're curious about:
       - Specific services (e.g., AI tools, databases, pricing)? 
       - How it compares to AWS/Azure? 
       - Use
(1 row)

Input: text

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
SELECT ai.openai_chat_complete('Do your know Huawei Cloud?');
                                                                           openai_chat_complete
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Yes, I'm familiar with **Huawei Cloud**, which is the cloud services division of **Huawei Technologies**. Here's a quick overview:
 ### Key Highlights:
 1. **Services Offered**:
    - **Compute**: Infrastructure as a Service (IaaS) and Platform as a Service (PaaS) for scalable computing.
    - **Storage**: High-performance storage solutions for data centers and cloud environments.
    - **Networking**: Next-generation networking and security services.
    - **AI/Big Data**: Integration with AI models, big data analytics, and cloud-native applications.
    - **Enterprise Solutions**: Customized cloud solutions for businesses, including hybrid and multi-cloud architectures.
 2. **Key Features**:
    - **Global Reach**: Operates in over 170 countries, providing cloud services worldwide.
    - **Security & Compliance**: Certifications like ISO 27001, SOC 2, and compliance with GDPR and other standards.
    - **AI Integration**: Partnerships with AI companies like **Tengda** for AI-driven cloud solutions.
    - **5G & Edge Computing**: Supports 5G networks and edge computing for low-latency applications.
 3. **Use Cases**:
    - Enterprise scalability, AI-driven analytics, IoT, and hybrid cloud deployments.

 ### Why Choose Huawei Cloud?
 - **Reliability**: Proven infrastructure and disaster recovery capabilities.
 - **Flexibility**: Support for both public and private cloud environments.
 - **Innovation**: Integration with cutting-edge technologies like AI and 5G.

 If you're looking to deploy applications or scale infrastructure, Huawei Cloud offers a robust platform with strong enterprise support. Let me know if you need specific details! 
(1 row)

ai.openai_chat_complete_with_raw_response

Description: interacts with a LLM to generate text.

Table 5 Description

Parameter

Type

Mandatory

Description

messages | input

jsonb | text

Mandatory

Two input types are supported for interacting with a LLM.

model

text

No

Model name, which is set using ai.set_func_model by default.

Return value: unprocessed API service response of the jsonb type

Example:

Input: jsonb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
SELECT ai.openai_chat_complete_with_raw_response(
'[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Do your know Huawei Cloud?"},
{"role": "assistant", "content": "Huawei Cloud is a cloud service provider."}
]'::jsonb
);

                                                                   openai_chat_complete_with_raw_response
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": "chatcmpl-a9298cb4ef", "model": "hosted_vllm/deepseek-r1-671b", "usage": {"tpot": 68, "ttft": 228, "total_tokens": 698, "prompt_tokens": 27, "completion_tokens": 671}, "object": "chat.completion", "choices": [{"index": 0, "message": {"role": "assistant", "content": "\nYes, I'm familiar with **Huawei Cloud** (officially **HUAWEI CLOUD**). Here's a concise overview of its key aspects: \n\n### Key Features & Offerings:\n1. **Global Infrastructure**: \n - 30+ regions and 84 availability zones worldwide. \n - Compliant with local regulations (e.g., GDPR in Europe). \n\n2. **Hybrid & Multi-Cloud Solutions**: \n - **Huawei Cloud Stack**: On-premises extension for hybrid cloud. \n - Partnerships for interoperability (e.g., with AWS, Azure). \n\n3. **AI-Centric Services**: \n - **Pangu Models**: Large AI models for industry-specific applications. \n - AI development platforms (**ModelArts**) and inferencing tools. \n\n4. **Industry-Specific Solutions**: \n - Focus on **smart manufacturing**, **finance**, **healthcare**, and **smart cities**. \n\n5. **Advanced Technologies**: \n - Cloud-native ecosystem (Kubernetes, serverless). \n - **Big Data**, **IoT**, and **5G integration**. \n\n6. **Developer Ecosystem**: \n - Open-source contributions (e.g., openEuler OS, MindSpore AI framework). \n - Extensive SDKs/APIs and low-code tools. \n\n### Competitive Edge:\n- **Cost-Effectiveness**: Competitive pricing, especially in Asia-Pacific markets. \n- **Security/Compliance**: Emphasis on data sovereignty and end-to-end encryption. \n- **5G Synergy**: Unique integration with Huawei's 5G infrastructure. \n\n### Market Position:\n- **#2 in China** (behind Alibaba Cloud), top 5 global IaaS market share. \n- Rapid growth in emerging markets (Asia, Latin America, Africa). \n\n### Controversies:\n- Geopolitical challenges (U.S. trade restrictions) impacting global expansion. \n\nHuawei Cloud is a strategic choice for enterprises needing AI/5G-driven solutions, especially in regions with strong Huawei presence. Let me know if you'd like deep dives into specific services!", "reasoning_content": "\nOkay, the user asked if I know Huawei Cloud. They probably want a quick confirmation or basic info. \n\nLooking at the history, my previous reply was super short-just calling it a \"cloud service provider.\" That's accurate but kinda barebones. Maybe the user expected more detail, since Huawei Cloud is a major player. \n\nThe user didn't specify their knowledge level, so they could be a newbie checking options or a tech pro comparing providers. Since they didn't ask for specifics, I'll keep it broad but add layers: technical depth (AI, infrastructure) and practical angles (use cases, global reach). \n\nHmm, should I mention controversy? Like US-China tensions affecting Huawei? Probably not-unless the user asks. They seem neutral, so stick to facts. \n\nNoting they're Tier 1: that's a credibility signal for enterprise users. Adding revenue rank (#2 in China) too-it's impressive but not overselling. \n\nBalancing conciseness with usefulness: bullet points in reply will help, but avoid jargon avalanches. They can always ask follow-ups.\n"}, "finish_reason": "stop", "provider_specific_fields": {}}], "created": 1763017779450, "message": ""}
(1 row)

Input: text

1
2
3
4
5
SELECT ai.openai_chat_complete_with_raw_response('Do your know Huawei Cloud?');
                                                                     openai_chat_complete_with_raw_response
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": "chatcmpl-cdff97c366", "model": "hosted_vllm/deepseek-r1-671b", "usage": {"tpot": 69, "ttft": 185, "total_tokens": 736, "prompt_tokens": 15, "completion_tokens": 721}, "object": "chat.completion", "choices": [{"index": 0, "message": {"role": "assistant", "content": "\nYes, I'm familiar with **Huawei Cloud**! Here's a comprehensive overview: \n\n### **What is Huawei Cloud?** \nHuawei Cloud is the global cloud service arm of Huawei Technologies, launched in 2011. It's one of China's largest and the world's fastest-growing cloud providers (Top 5 globally by market share). \n\n### **Key Strengths** \n1. **Full-Stack Services**: \n - **IaaS**: Virtual machines, storage, networking. \n - **PaaS**: Databases (GaussDB), AI development (ModelArts), container services (CCI/CCE). \n - **SaaS**: Collaboration tools (WeLink), industry-specific solutions. \n\n2. **AI & Big Data Leadership**: \n - **Pangu AI Models**: Large language models (LLMs) rivaling GPT-3.5, tailored for industries (e.g., finance, healthcare). \n - **ModelArts**: One-stop AI development platform supporting training/deployment. \n\n3. **Global Infrastructure**: \n - 88 availability zones across 30 regions (Asia-Pacific, LATAM, Africa, Europe). \n - Compliant with GDPR, CCPA, and local regulations (e.g., China's DSL). \n\n4. **Hybrid & Multi-Cloud Solutions**: \n - **Huawei Cloud Stack**: On-premises private cloud integration. \n - Partnerships for seamless multi-cloud management. \n\n5. **Industry-Specific Solutions**: \n - Smart cities, autonomous driving, manufacturing (Industrial IoT), and fintech (distributed databases). \n\n### **Competitive Edge** \n- **Security**: End-to-end encryption + \"Zero Trust\" architecture. \n- **Cost Efficiency**: Aggressive pricing, especially in emerging markets. \n- **5G + Cloud Synergy**: Edge computing optimized for 5G latency (e.g., Intelligent EdgeFabric). \n- **Open Source**: Major contributor to Kubernetes, Spark, and EdgeGallery. \n\n### **Market Position** \n- Dominant in China (≈20% market share), expanding globally. \n- Key clients include governments, telecoms (e.g., China Mobile), and enterprises (Ping An, SAIC Motor). \n\n### **Free Tier & Trials** \nHuawei Cloud offers free", "reasoning_content": "\nHmm, the user is asking about Huawei Cloud. They might be researching cloud service providers for personal or professional use, or perhaps evaluating options for a project. Since they kept it very general (\"Do you know\"), I should give a solid foundational overview.\n\nI recall Huawei Cloud is China's largest cloud provider with a global reach. Key highlights to cover: their \"Everything as a Service\" model, AI focus with Pangu models, infrastructure strength (they have hyper-scale regions), and industry solutions. The compliance point is crucial too - multinational users always care about data sovereignty. \n\nBetter structure this clearly: start with positioning, then major tech strengths (compute/storage/AI), deployment flexibility (hybrid/multi-cloud), and close with why businesses pick them. The free credits note could hook users exploring cost options. Should mention Pangu separately - it's becoming a major USP against Western clouds. \n\nUser seems technical given they named a specific cloud platform, but keeping explanations accessible. No signs of urgency in their tone, so a comprehensive (but not overwhelming) response fits. Might follow up with questions about their use case next.\n"}, "finish_reason": "timeout", "provider_specific_fields": {}}], "created": 1763017897477, "message": "Reach time limitation! Use stream request to avoid timeout."}
(1 row)

ai.openai_moderate

Description: determines whether the text content is harmful.

Parameters:
  • input_text: text to be judged. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: judgment result in jsonb format

Example:

1
SELECT ai.openai_moderate('You're such a fool.');

ai.openai_moderate_with_raw_response

Description: determines whether the text content is harmful.

Parameters:

  • input_text: text to be judged. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: unprocessed API service response of the jsonb type

Example:
SELECT ai.openai_moderate_with_raw_response('You are such a fool.');

ai.chunk_text

Description: chunks text using a separator.

Parameters:

  • input: text to be chunked. This parameter is mandatory.
  • chunk_size: maximum length of each text chunk. This parameter is optional. The value is in int format, and the default value is NULL.
  • chunk_overlap: number of overlapping characters between adjacent text chunks. This parameter is optional. The value is in int format, and the default value is NULL.
  • separator: custom separator. This parameter is optional. The value is in text format, and the default value is NULL.
  • is_separator_regex: whether the separator is a regular expression. This parameter is optional. The value is in boolean format, and the default value is false.

Return value: text chunks in text[] format

Example:
1
2
3
4
5
SELECT ai.chunk_text('A data warehouse is a centralized data storage system specifically designed to collect, store, process, and analyze large volumes of data from multiple heterogeneous data sources. By integrating information from different operating systems, transaction systems, and external data sources, it enables enterprises to perform complex data analysis and reporting. Unlike traditional database systems, data warehouses emphasize historical data and query efficiency, typically supporting business intelligence (BI) and decision support systems (DSS).', 100, 10, '.');
                                                                                    chunk_text
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"(0, 'A data warehouse is a centralized data storage system specifically designed to collect, store, process, and analyze large volumes of data from multiple heterogeneous data sources')","(1, 'By integrating information from different operating systems, transaction systems, and external data sources, it enables enterprises to perform complex data analysis and reporting')","(2, 'Unlike traditional database systems, data warehouses emphasize historical data and query efficiency, typically supporting business intelligence (BI) and decision support systems (DSS)')"}
(1 row)

ai.chunk_text_recursively

Description: chunks text recursively using separators

Parameters::

  • input: text to be chunked. This parameter is mandatory.
  • chunk_size: maximum length of each text chunk. This parameter is optional. The value is in int format, and the default value is NULL.
  • chunk_overlap: number of overlapping characters between adjacent text chunks. This parameter is optional. The value is in int format, and the default value is NULL.
  • separator: custom separator. This parameter is optional. The value is in text[] format, and the default value is NULL.
  • is_separator_regex: whether the separator is a regular expression. This parameter is optional. The value is in boolean format, and the default value is false.

Return value: text chunks in text[] format

Example:

1
2
3
4
5
6
7
SELECT ai.chunk_text_recursively('
A data warehouse is a centralized data storage system specifically designed to collect, store, process, and analyze large volumes of data from multiple heterogeneous data sources. By integrating information from different operating systems, transaction systems, and external data sources, it enables enterprises to perform complex data analysis and reporting. Unlike traditional database systems, data warehouses emphasize historical data and query efficiency, typically supporting business intelligence (BI) and decision support systems (DSS).
', 100, 10, array[',', '.']);
                                                                           chunk_text_recursively
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"(0, 'A data warehouse is a centralized data storage system specifically designed to collect, store')","(1, ', store, process')","(2, ', and analyze large volumes of data from multiple heterogeneous data sources')","(3, '. By integrating information from different operating systems')","(4, ', transaction systems, and external data sources')","(5, ', it enables enterprises to perform complex data analysis and reporting')","(6, '. Unlike traditional database systems')","(7, ', data warehouses emphasize historical data and query efficiency')","(8, ', typically supporting business intelligence (BI) and decision support systems (DSS).')"}
(1 row)

ai.similarity

Description: computes the similarity between two pieces of text.

Parameters:
  • input_text1: the first piece of text used for the similarity computation. This parameter is mandatory.
  • input_text2: the second piece of text used for the similarity computation. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: similarity result in float format. The value ranges from -1 to 1. The closer the value is to 1, the higher the similarity.

Example:

1
2
3
4
5
SELECT ai.similarity('I like apple.', 'I like pen.');
similarity
------------
 .5875276
(1 row)

ai.vector_cosine_similarity

Description: computes the similarity between two vectors.

Parameters:
  • input_vec1: the first vector used for the similarity computation. This parameter is mandatory.
  • input_vec2: the second vector used for the similarity computation. This parameter is mandatory.

Return value: similarity result in float format. The value ranges from -1 to 1. The closer the value is to 1, the higher the similarity.

Example:

1
2
3
4
5
SELECT ai.vector_cosine_similarity('[1.0, 2.0, 3.0]', '[-2.0, 5.0, -3.0]');
vector_cosine_similarity
------------------------
-.043355495
(1 row)

ai.classify

Description: classifies the input text using specified labels.

Parameters:
  • input_text: the text to be classified. This parameter is mandatory.
  • category: labels used to classify the text. This parameter is mandatory. The value is in text[] format.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: a label in category, in text format

Example:

1
2
3
4
5
SELECT ai.classify('The football match between Team A and Team B was thrilling. The game ended with a 3-2 victory for Team A, and the players celebrated with their fans.', array['Sports', 'Technology', 'Culture']);
classify
---------
Sports
(1 row)

ai.extract

Description: extracts specified labels from the input text.

Parameters:
  • input_text: text from which information is to be extracted
  • extract_keywords: keywords to be extracted from the input text. This parameter is mandatory. The value is in text[] format.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: labels and their information, in jsonb format

Example:

1
2
3
4
5
SELECT ai.extract('Name: John Smith, Gender: Male, Age: 30, Email: john.smith@example.com, Phone: +1 555-1234, Experience: Highly experienced professional', array['Name', 'Gender', 'Email']);
                                  extract
----------------------------------------------------------------------------
{"Name": "John Smith", "Email": "john.smith@example.com", "Gender": "Male"}
(1 row)

ai.mask

Description: masks specified key information in the input text.

Parameters:
  • input_text: input text to be masked. This parameter is mandatory.
  • mask_keywords: keywords to be masked in the input text. This parameter is mandatory. The value is in text[] format.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: text that contains the masked keywords

Example:

1
2
3
4
5
SELECT ai.mask('Name: John Smith, Gender: Male, Age: 30, Email: john.smith@example.com, Phone: +1 555-1234, Experience: Highly experienced professional', array['Name', 'Gender', 'Email']);
                                                                mask
-------------------------------------------------------------------------------------------------------------------------------
Name: [#####], Gender: [#####], Age: 30, Email: [#####], Phone: +1 555-1234, Experience: Highly experienced professional
(1 row)

ai.fix_grammar

Description: corrects the grammar errors in the input text.

Parameters:
  • input_text: input text whose grammar errors are to be corrected. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: text content with corrected grammar

Example:

1
2
3
4
5
SELECT ai.fix_grammar('She do not like him.');
      fix_grammar
-----------------------
She does not like him.
(1 row)

ai.summarize

Description: generates a summary of the input text.

Parameters:
  • input_text: input text for which a summary is to be generated
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: summary of the input text

Example:

1
2
3
4
5
6
7
8
9
SELECT ai.summarize('
Huawei Cloud GaussDB is a high-performance, distributed database service launched by Huawei, supporting multiple database engines including relational databases (such as MySQL and PostgreSQL) and non-relational databases. GaussDB delivers elasticity, high availability, and auto-scaling capabilities to meet enterprises'' demanding requirements for database performance, reliability, and flexibility.
As Huawei Cloud''s flagship database, GaussDB integrates an advanced distributed architecture that supports automatic fault recovery and data backup to ensure data security. Simultaneously, it optimizes query performance and reduces operational costs through intelligent scheduling and resource management technologies. GaussDB also excels in high concurrency and high throughput, making it ideal for large-scale data processing and high-load applications across industries such as finance, e-commerce, and manufacturing.
Furthermore, through deep integration with other Huawei Cloud services, GaussDB delivers comprehensive data processing solutions to empower enterprises in digital transformation and intelligent management. It not only fulfills fundamental database requirements but also supports AI and big data applications, further enhancing the value of enterprise data.
');
                                                                                      summarize
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Huawei Cloud GaussDB is a high-performance, distributed database service supporting relational and non-relational databases, offering elasticity, high availability, and auto-scaling to meet enterprise needs for performance, reliability, and flexibility. It integrates an advanced distributed architecture for automatic fault recovery, data backup, and optimized query performance with cost reduction. Its high concurrency and throughput make it suitable for large-scale data processing and high-load applications in industries like finance and e-commerce. Integrated with other Huawei Cloud services, it provides comprehensive data processing solutions, supporting AI and big data to drive digital transformation and intelligent management.
(1 row)

ai.translate

Description: translates the input text into a specified language.

Parameters:
  • input_text: text to be translated
  • target_language: target language in text format. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: translated text

ai.rank

Description: scores the relevance of multiple pieces of text based on the topic.

Parameters:
  • input_topic: topic content in text format. This parameter is mandatory.
  • input_texts: text[]: multiple pieces of text. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: relevance score of each piece of text, in jsonb format. The value ranges from 0 to 1. A larger value indicates higher relevance.

Example:

1
2
3
4
5
6
7
8
SELECT ai.rank('The Future Development of Artificial Intelligence.', 
array['The rapid advancement of artificial intelligence has already made a significant impact across multiple industries, including healthcare and finance.', 
'Machine learning and deep learning are core technologies of artificial intelligence, and an increasing number of companies are beginning to apply these technologies.', 
'Forecast data for the global technology market in 2023 indicates that the cloud computing market is experiencing steady growth.']);
                                                                                     rank
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"Forecast data for the global technology market in 2023 indicates that the cloud computing market is experiencing steady growth.": 3.0, "The rapid advancement of artificial intelligence has already made a significant impact across multiple industries, including healthcare and finance.": 8.0, "Machine learning and deep learning are core technologies of artificial intelligence, and an increasing number of companies are beginning to apply these technologies.": 7.0}
(1 row)

ai.sentiment

Description: performs sentiment analysis on the input text to determine whether the text content is positive, negative, neutral, or mixed.

Parameters:
  • input_text: text to be analyzed. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: sentiment analysis result of the input text

Example:

1
2
3
4
5
SELECT ai.sentiment('You are such a kind person.');
sentiment
-----------
positive
(1 row)

ai.textfilter

Description: filters the input text based on the given condition.

Parameters:

  • input_text: text to be filtered. This parameter is mandatory.
  • filter_condition: filter condition in text format. This parameter is mandatory.
  • model: model name in text format, which is set using ai.set_func_model by default. This parameter is optional.

Return value: a boolean value that specifies whether the text meets the filter condition

Example:

1
2
3
4
5
SELECT ai.textfilter('The food at this restaurant is delicious!', 'Is this a negative review?');
textfilter
-----------
f
(1 row)

RAG Usage Example

Generate a pizza review report based on user comments.

Prerequisites: The pgvector plugin is supported.

  1. Create a sample table and insert data into it.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    CREATE TABLE public.pizza_reviews  (
    id bigserial NOT NULL,
    product text NOT NULL,
    customer_message text NULL,
    text_length INTEGER,
    CONSTRAINT pizza_reviews_pkey PRIMARY KEY (id)
    );
    
    INSERT INTO public.pizza_reviews values
    (1, 'pizza','The best pizza I''ve ever eaten. The sauce was so tangy!'),
    (2, 'pizza','The pizza was disgusting. I think the pepperoni was made from rats.'),
    (3, 'pizza','I ordered a hot-dog and was given a pizza, but I ate it anyway.'),
    (4, 'pizza','I hate pineapple on pizza. It is a disgrace. Somehow, it worked well on this izza though.'),
    (5, 'pizza','I ate 11 slices and threw up. The pizza was tasty in both directions.');
    
  2. Create the pizza_reviews_embeddings table that stores intermediate results (vectors) and the ai_report table that records the output.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    CREATE TABLE public.pizza_reviews_embeddings (
    id bigserial NOT NULL,
    text_id text NOT NULL,
    text_content text NOT NULL, -- it is same as pizza_reviews
    model_name text NOT NULL,
    ntoken int4 NULL,
    nlength int4 NULL,
    embedding vector NOT NULL,
    CONSTRAINT pizza_reviews_embeddings_pkey PRIMARY KEY (id)
    );
    
    CREATE TABLE public.ai_report (
    send_message text NULL,
    chat_completion jsonb NULL,
    final_report text NULL,
    create_time timestamptz NULL
    );
    
  3. Vectorize and insert data into the pizza_reviews_embeddings table.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    WITH tmp AS (
    SELECT
    tt.id, tt.customer_message,
    'text-embedding-3-small'::text as model_name,
    ai.openai_embed(customer_message)::vector as embedding
    FROM
    pizza_reviews  as tt
    )
    INSERT INTO pizza_reviews_embeddings
    (text_id, text_content, model_name, embedding )
    SELECT
    id, customer_message, model_name, embedding
    FROM
    tmp;
    
  4. Vectorize questions and find the three most similar data records.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    WITH
    business_question AS (
    SELECT question
    FROM
    (values
    ('why customer do not like our pizza?')
    )as t(question)
    )
    , embedding_question AS (
    SELECT
    question, ai.openai_embed(question)::vector as embedding
    FROM
    business_question
    )
    SELECT
    eqt.question,
    emt.text_content ,
    emt.embedding <-> eqt.embedding as similarity
    FROM pizza_reviews_embeddings emt  CROSS JOIN embedding_question eqt
    ORDER BY emt.embedding <-> eqt.embedding
    LIMIT 3;
    
  5. Generate a report.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    WITH embedding_question AS (
    SELECT
    'why customer dont like our pizza'::text as question, ai.openai_embed('why customer dont like our pizza')::vector AS embedding
    ),
    reasons AS (
    SELECT
    eqt.question,
    emt.text_content ,
    emt.embedding <-> eqt.embedding as similarity
    FROM pizza_reviews_embeddings emt  CROSS JOIN embedding_question eqt
    ORDER BY
    emt.embedding <-> eqt.embedding
    LIMIT 5
    )
    ,agg_resons AS (
    SELECT
    question, jsonb_pretty(jsonb_agg(text_content)) AS reasons
    FROM reasons
    GROUP BY question
    )
    ,report_needs AS (
    SELECT
    chr(10)||'// 1. requirements:
    // 1.1 generate a business report to answer user question with provided data.
    // 1.2 The report should be markdown format and less than 300 words' || chr(10) AS report_needs,
    chr(10)||'// 2. data' || chr(10) AS  data_needs,
    chr(10)||'// 3. user question'|| chr(10) AS user_question
    )
    ,report AS (
    SELECT
    report_needs || data_needs ||  reasons  ||user_question || question AS send_message,
    ai.openai_chat_complete(
    jsonb_build_array(
    jsonb_build_object(
    'role', 'user', 'content',
    report_needs || data_needs ||  reasons  ||user_question || question)
    )) AS chat_completion
    FROM
    agg_resons CROSS JOIN report_needs
    )
    INSERT INTO ai_report
    (send_message, chat_completion, final_report, create_time)
    SELECT
    send_message,
    chat_completion,
    now() AS create_time
    FROM report;