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 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 the beta test phase. To use it, contact technical support.
- To use in-database Inference, contact technical support to add the feature_support_options parameter and set its value to enable_pgai_extension. This function is supported only by clusters running version 9.1.1.200 and later.
- 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
You can create a pgai extension by running CREATE EXTENSION ai;. 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.
Parameters:
- extra_headers: HTTP request headers. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- extra_query: custom query parameters in the request. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- verbose: whether to display detailed information. This parameter is optional. The value type is boolean. The default value is false.
- client_config: custom configuration such as the retry policy and request header. This parameter is optional. The default value is NULL.
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.
Parameters:
- extra_headers: HTTP request headers. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- extra_query: custom query parameters in the request. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- verbose: whether to display detailed information. This parameter is optional. The value type is boolean. The default value is false.
- client_config: custom configuration such as the retry policy and request header. This parameter is optional. The default value is NULL.
Return value: models of the JSON 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. |
|
dimensions |
int |
No |
Dimensions of the generated embedding vector. The dimensions can be set for some models but not for others. Set this parameter as needed. The default value is NULL. |
|
openai_user |
text |
No |
User in the request. The default value is NULL. |
|
extra_headers |
jsonb |
No |
Custom HTTP request headers. The default value is NULL. |
|
extra_query |
jsonb |
No |
Custom query parameters in the request. The default value is NULL. |
|
extra_body |
jsonb |
No |
Additional request body. The default value is NULL. |
|
verbose |
boolean |
No |
Whether to display detailed information. The default value is false. |
|
client_config |
jsonb |
No |
Custom configuration such as the retry policy and request header. The default value is NULL. |
Return value: text for input_text and jsonb for input_texts and input_tokens
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. |
|
dimensions |
int |
No |
Dimensions of the generated embedding vector. The dimensions can be set for some models but not for others. Set this parameter as needed. The default value is NULL. |
|
openai_user |
text |
No |
User in the request. The default value is NULL. |
|
extra_headers |
jsonb |
No |
Custom HTTP request headers. The default value is NULL. |
|
extra_query |
jsonb |
No |
Custom query parameters in the request. The default value is NULL. |
|
extra_body |
jsonb |
No |
Additional request body. The default value is NULL. |
|
verbose |
boolean |
No |
Whether to display detailed information. The default value is false. |
|
client_config |
jsonb |
No |
Custom configuration such as the retry policy and request header. The default value is NULL. |
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 |
|---|---|---|---|
|
message | input |
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. |
|
frequency_penalty |
float8 |
No |
Controls the repetition of the same words or phrases in the generated text. A negative value encourages the model to repeat the same words or phrases, and a positive value discourages the model from doing so. The value ranges from -2.0 to 2.0. The default value is NULL. |
|
logit_bias |
jsonb |
No |
Likelihood (increase or decrease) of specified words appearing in the text generated by a model. The default value is NULL. |
|
logprobs |
boolean |
No |
Whether to return the log probability of each token. The default value is false. |
|
top_logprobs |
int |
No |
Number of candidate words that are mostly likely to return for each token and their log probabilities. The default value is NULL. |
|
max_tokens |
int |
No |
Maximum number of tokens allowed for the generated answer. The default value is NULL. |
|
max_completion_tokens |
int |
No |
Maximum number of tokens that can be generated for a completion. The default value is NULL. |
|
n |
int |
No |
Number of candidate answers generated by the model. The default value is NULL. |
|
presence_penalty |
float8 |
No |
Controls the model's tendency to generate new topics. The value ranges from -2.0 to 2.0. A negative value encourages the model to repeat existing content, while a positive value encourages the model to generate new and non-repetitive content. The default value is NULL. |
|
response_format |
jsonb |
No |
Response format. The default value is NULL. |
|
seed |
int |
No |
Controls the randomness of a model and consistency of the output. Requests with the same seed and parameters should return the same result. The default value is NULL. |
|
stop |
text |
No |
Where the LLM stops generating text. The default value is NULL. |
|
temperature |
float8 |
No |
Controls the randomness of the generated text. A higher temperature value results in more random text, while a lower temperature value results in more focused and deterministic text. The value ranges from 0 to 2. The default value is NULL. |
|
top_p |
float8 |
No |
Sampling range. The value ranges from 0 to 1. The default value is NULL. |
|
tools |
jsonb |
No |
Toolset used by the LLM. The default value is NULL. |
|
tool_choice |
text |
No |
Used with the tools parameter to specify one or more tools to execute specific tasks. The default value is NULL. |
|
openai_user |
text |
No |
User in the request. The default value is NULL. |
|
extra_headers |
jsonb |
No |
Custom HTTP request headers. The default value is NULL. |
|
extra_query |
jsonb |
No |
Custom query parameters in the request. The default value is NULL. |
|
extra_body |
jsonb |
No |
Additional request body. The default value is NULL. |
|
verbose |
boolean |
No |
Whether to display detailed information. The default value is false. |
|
client_config |
jsonb |
No |
Custom configuration such as the retry policy and request header. The default value is NULL. |
For details about the optional parameters, see Create chat completion.
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 (2023–2024): - **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.
|
Parameter |
Type |
Mandatory |
Description |
|---|---|---|---|
|
message | 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. |
|
frequency_penalty |
float8 |
No |
Controls the repetition of the same words or phrases in the generated text. A negative value encourages the model to repeat the same words or phrases, and a positive value discourages the model from doing so. The value ranges from -2.0 to 2.0. The default value is NULL. |
|
logit_bias |
jsonb |
No |
Likelihood (increase or decrease) of specified words appearing in the text generated by a model. The default value is NULL. |
|
logprobs |
boolean |
No |
Whether to return the log probability of each token. The default value is false. |
|
top_logprobs |
int |
No |
Number of candidate words that are mostly likely to return for each token and their log probabilities. The default value is NULL. |
|
max_tokens |
int |
No |
Maximum number of tokens in the generated text. The default value is NULL. |
|
max_completion_tokens |
int |
No |
Maximum number of tokens that can be generated for a completion. The default value is NULL. |
|
n |
int |
No |
Number of candidate answers generated by the model. The default value is NULL. |
|
presence_penalty |
float8 |
No |
Controls the model's tendency to generate new topics. The value ranges from -2.0 to 2.0. A negative value encourages the model to repeat existing content, while a positive value encourages the model to generate new and non-repetitive content. The default value is NULL. |
|
response_format |
jsonb |
No |
Response format. The default value is NULL. |
|
seed |
int |
No |
Controls the randomness of a model and consistency of the output. Requests with the same seed and parameters should return the same result. The default value is NULL. |
|
stop |
text |
No |
Where a model stops generating text. The default value is NULL. |
|
temperature |
float8 |
No |
Controls the randomness of the generated text. A higher temperature value results in more random text, while a lower temperature value results in more focused and deterministic text. The value ranges from 0 to 2. The default value is NULL. |
|
top_p |
float8 |
No |
Sampling range. The value ranges from 0 to 1. The default value is NULL. |
|
tools |
jsonb |
No |
Toolset used by the model. The default value is NULL. |
|
tool_choice |
text |
No |
Used with the tools parameter to specify one or more tools to execute specific tasks. The default value is NULL. |
|
openai_user |
text |
No |
User in the request. The default value is NULL. |
|
extra_headers |
jsonb |
No |
Custom HTTP request headers. The default value is NULL. |
|
extra_query |
jsonb |
No |
Custom query parameters in the request. The default value is NULL. |
|
extra_body |
jsonb |
No |
Additional request body. The default value is NULL. |
|
verbose |
boolean |
No |
Whether to display detailed information. The default value is false. |
|
client_config |
jsonb |
No |
Custom configuration such as the retry policy and request header. The default value is NULL. |
For details about the optional parameters, see Create chat completion.
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.
- 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.
- extra_headers: HTTP request headers. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- extra_query: custom query parameters in the request. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- extra_body: additional request body in jsonb format. This parameter is optional. The default value is NULL.
- verbose: whether to display detailed information. This parameter is optional. The value type is boolean. The default value is false.
- client_config: custom configuration such as the retry policy and request header. This parameter is optional. The default value is NULL.
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.
- extra_headers: HTTP request headers. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- extra_query: custom query parameters in the request. This parameter is optional. The value is in jsonb format, and the default value is NULL.
- extra_body: additional request body in jsonb format. This parameter is optional. The default value is NULL.
- verbose: whether to display detailed information. This parameter is optional. The value type is boolean. The default value is false.
- client_config: custom configuration such as the retry policy and request header. This parameter is optional. The default value is NULL.
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 float8 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 float8 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 specifiied 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 gramma
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;
Feedback
Was this page helpful?
Provide feedbackThank 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