Use RDS for PostgreSQL as both the relational database and vector store for Dify, an open-source LLM platform, to build an intelligent Q&A application.
Step 1: Create an RDS for PostgreSQL instance
-
Create an account and a database for the RDS for PostgreSQL instance.
Note the following:
-
When you create the account, set Account Type to Privileged Account.
-
When you create the database, set Authorized Account to the privileged account that you created.
-
-
Enable a public endpoint for the RDS for PostgreSQL instance. Enable or disable a public endpoint.
Add the public IP address of your ECS instance to the whitelist of the RDS for PostgreSQL instance. Configure an IP address whitelist.
-
Enable the vector extension for the target database. Manage extensions.
Step 2: Deploy Dify
-
Create an ECS instance. Create an instance by using the wizard.
-
A CPU-based ECS instance supports online LLMs.
-
A GPU-accelerated ECS instance supports both online and locally deployed LLMs.
Important-
This article uses Alibaba Cloud Linux 3 as an example.
-
If you use a GPU-accelerated ECS instance, install the GPU driver when you configure the image. This allows you to deploy LLMs with Ollama.
-
-
Install Docker on the ECS instance.
-
(Optional) If you are using a GPU-accelerated ECS instance, run the following command to install the container-toolkit component.
-
Clone the Dify source code.
git clone https://github.com/langgenius/dify.gitTo install a specific version, use the
branchparameter. Available versions: dify. Example for v1.0.0:git clone https://github.com/langgenius/dify.git --branch 1.0.0NoteIf Git is not installed, run
sudo yum install git -yto install it. -
Configure environment variables to set RDS for PostgreSQL as the default database and vector store.
-
Set RDS for PostgreSQL as the default database.
export DB_USERNAME=testdbuser export DB_PASSWORD=dbPassword export DB_HOST=pgm-****.pg.rds.aliyuncs.com export DB_PORT=5432 export DB_DATABASE=testdb01Replace these values with your actual settings.
Parameter
Description
DB_USERNAME
The privileged account of the RDS for PostgreSQL instance.
DB_PASSWORD
The password of the privileged account for the RDS for PostgreSQL instance.
DB_HOST
The public endpoint of the RDS for PostgreSQL instance.
DB_PORT
The public port of the RDS for PostgreSQL instance. The default value is 5432.
DB_DATABASE
The name of the database in the RDS for PostgreSQL instance.
-
Set RDS for PostgreSQL as the default vector database.
export VECTOR_STORE=pgvector export PGVECTOR_HOST=pgm-****.pg.rds.aliyuncs.com export PGVECTOR_PORT=5432 export PGVECTOR_USERNAME=testdbuser export PGVECTOR_PASSWORD=dbPassword export PGVECTOR_DATABASE=testdb01Replace these values with your actual settings.
Parameter
Description
VECTOR_STORE
Specifies the vector store. Use
pgvectorfor the PostgreSQL extension.PGVECTOR_USERNAME
The privileged account of the RDS for PostgreSQL instance.
PGVECTOR_PASSWORD
The password of the privileged account for the RDS for PostgreSQL instance.
PGVECTOR_HOST
The public endpoint of the RDS for PostgreSQL instance.
PGVECTOR_PORT
The public port of the RDS for PostgreSQL instance. The default value is 5432.
PGVECTOR_DATABASE
The name of the database in the RDS for PostgreSQL instance.
Alternatively, configure these settings in the
.envfile. -
-
(Optional) To save resources, disable the default database and Weaviate containers by editing the
.envanddocker-compose.yamlfiles. -
-
Start Dify.
cd /root/dify/docker docker compose -f docker-compose.yaml up -d
Step 3: Access the Dify service
-
Open
http://<public_IP_address_of_your_ECS_instance>/installin a browser to start the Dify installation.NoteIf the page fails to load, refresh it. Dify may still be initializing.
-
Follow the instructions to set up an administrator account (email, username, and password).
Step 4: Add and configure an AI model
This example uses Qwen.
-
Log on to Dify.
-
Click your username > Settings.
-
On the Settings page, choose Model Providers > Qwen > (Set).
-
On the Settings page for Qwen, click the link to obtain an API key from Alibaba Cloud Model Studio.
-
Paste your API key and click Save.
On a GPU-accelerated ECS instance, you can deploy the Qwen LLM locally.
Step 5: Create a knowledge base
A knowledge base enables the Q&A application to answer domain-specific questions accurately.
Prerequisites
Prepare corpus files for the knowledge base. Supported formats: TXT, MARKDOWN, MDX, PDF, HTML, XLSX, XLS, DOCX, CSV, MD, and HTM. Maximum file size: 15 MB.
Procedure
-
Click Knowledge > Create Knowledge > Import from Text > Select File > Next to upload your files.
Supported formats: TXT, MARKDOWN, MDX, PDF, HTML, XLSX, XLS, DOCX, CSV, MD, and HTM. Maximum file size: 15 MB.
-
After you click Next, follow the on-screen instructions for text chunking and cleaning.
Keep the default settings. The knowledge base automatically chunks, cleans, and indexes the documents.
Verify the knowledge base and index
Verify the knowledge base contents and index in RDS for PostgreSQL.
-
Connect to an RDS for PostgreSQL instance used by Dify.
-
Query the knowledge base ID.
SELECT * FROM datasets; -
Derive the table name from the dataset ID: replace hyphens (
-) with underscores (_), add prefixembedding_vector_index_and suffix_nod. Example query:SELECT * FROM embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod; -
Confirm the knowledge base index.
Dify creates an HNSW index by default to accelerate pgvector similarity searches. The search query looks like:
SELECT meta, text, embedding <=> $1 AS distance FROM embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod ORDER BY distance LIMIT $2;Verify that the index parameters meet your recall requirements. The relationship between
m,ef_construction, and recall is documented in pgvector performance test (based on HNSW index).SELECT * FROM pg_indexes WHERE tablename = 'embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod';If no index exists or the default parameters do not meet your recall requirements, create one manually.
-
(Optional) Drop the existing index.
DROP INDEX IF EXISTS embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod; -
Create the index.
CREATE INDEX ON embedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nod USING hnsw (embedding vector_cosine_ops) WITH (m = '16', ef_construction = '100');
Noteembedding_vector_index_6b169753_****_****_be66_9bddc44a4848_nodis an example. Replace it with your actual table name. -
Step 6: Create an intelligent Q&A application
This example uses the Question Classifier + Knowledge + Chatbot template.
-
Click Studio > Create from Template.
-
Find the Question Classifier + Knowledge + Chatbot template and click Use this template.
-
Configure the application name and icon, and then click Create.
-
On the Studio page, click the card for the new application to open the application orchestration page.
-
Configure the application workflow: delete the Answer module, keep one Knowledge Retrieval module, and change the AI model for the Question Classifier and LLM modules to Qwen.
When you configure the Qwen model, set the Top P value to less than 1.
After you select the qwen-vl-max-0809 model, click the settings icon next to the model name to open the parameter configuration panel, and set Top P to
0.8. -
Edit the Question Classifier module to define question categories. For a PostgreSQL knowledge base:
-
PostgreSQL-related questions: route to the knowledge base and Qwen for a summarized answer.
-
Other questions: route directly to Qwen.
-
-
Add the PostgreSQL knowledge base to the Knowledge Retrieval module.
In the Knowledge Retrieval node, set Query Variable to
sys.query, select pg for the knowledge base, and set the retrieval mode to High-quality · Vector Retrieval. -
Click Preview to test the application, then click Publish to deploy.