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 install the pgai package and 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.
| Function | Description | Remarks |
|---|---|---|
| Sets the name of the model used by a function by default. | - | |
| Sets and encrypts the baseurl and apikey used by all functions. | - | |
| Converts text to tokens. | Only the GPT models provided by OpenAI are supported. | |
| Converts tokens to text. | Only the GPT models provided by OpenAI are supported. | |
| Displays available models. | Some large model services may not support this function. | |
| Displays available models in JSON format. | Some large model services may not support this function. | |
| Converts text to vectors. | Embedding models | |
| Converts text to vectors in JSON format. | Embedding models | |
| Interacts with LLMs. | LLMs | |
| Interacts with LLMs. The return is in JSON format. | LLMs | |
| Classifies text based on whether it is harmful. | Moderate models | |
| Classifies text based on whether it is harmful. The return is in JSON format. | Moderate models | |
| Chunks text using a single delimiter. | - | |
| Chunks text using multiple delimiters. | - | |
| Computes the similarity between two pieces of input text. | Embedding models | |
| Computes the similarity between two input vectors. | - | |
| Classifies text based on input labels. | LLMs | |
| Extracts keywords from the input text. | LLMs | |
| Masks keywords in the input text. | LLMs | |
| Corrects the grammar of the input text. | LLMs | |
| Generates a summary of the input text. | LLMs | |
| Translates the input text into a specified language. | LLMs | |
| Scores the relevance of multiple pieces of text based on the topic. | LLMs | |
| Performs sentiment analysis on the input text. | LLMs | |
| 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.
| 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.
| 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.
| 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 | SELECT ai.openai_chat_complete( '[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is a Database?"}, {"role": "assistant", "content": "Database is a system software."} ]'::jsonb ); openai_chat_complete -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- A database (typically referring to a database management system, or DBMS) is a system software. Just as an operating system (OS) manages a computer's hardware resources (CPU, memory, disk), this system software is specifically responsible for managing "data, " the core resource. It runs on top of the operating system and provides unified, efficient, and secure data access services to upper-layer application software (such as e-commerce systems, banking apps, and ERP systems). (1 row) |
Input: text
1 2 3 4 5 | SELECT ai.openai_chat_complete('Do your know Database?'); openai_chat_complete -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Yes, I'm very familiar with databases. Which specific module of the database would you like to explore in depth today? Have you encountered a specific kernel-level bug (such as a concurrency deadlock or memory out-of-bounds error), or would you like to discuss performance optimization strategies for a particular operator (such as SIMD acceleration or hash joins)? We can dive right into the code or architecture. (1 row) |
ai.openai_chat_complete_with_raw_response
Description: interacts with a LLM to generate text.
| 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": "What is a Database?"}, {"role": "assistant", "content": "Database is a system software."} ]'::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": "A database (typically referring to a database management system, or DBMS) is a system software. Just as an operating system (OS) manages a computer's hardware resources (CPU, memory, disk), this system software is specifically responsible for managing "data, " the core resource. It runs on top of the operating system and provides unified, efficient, and secure data access services to upper-layer application software (such as e-commerce systems, banking apps, and ERP systems)."}, "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 Database?'); 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": "Yes, I'm very familiar with databases. Which specific module of the database would you like to explore in depth today? Have you encountered a specific kernel-level bug (such as a concurrency deadlock or memory out-of-bounds error), or would you like to discuss performance optimization strategies for a particular operator (such as SIMD acceleration or hash joins)? We can dive right into the code or architecture."}, "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.
- 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
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
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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.');
- 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 );
- 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;
- 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;
- 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;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot