A NLQ(Natural Language Query) demo using Amazon Bedrock, Amazon OpenSearch with RAG technique.
Create an EC2 with following configuration:
- Software Image (AMI): Amazon Linux 2023 - Virtual server type (instance type): t3.large or higher - Firewall (security group): Allow 22, 80 port - Storage (volumes): 1 GP3 volume(s) - 30 GiB
Bind an IAM Role to your EC2 instance. And attach an inline policy to this IAM Role with following permissions:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "bedrock:*", "secretsmanager:GetSecretValue", "dynamodb:*" ], "Resource": "*" } ] }
Make sure you have enabled model access in AWS Console in us-west-2 (Oregon) region for Anthropic Claude model and Amazon Titan embedding model.
On the EC2 instance, log in to the SSH command line as the ec2-user user or use the AWS EC2 Instance Connect feature in the EC2 console to log in to the command line. In the session, execute the following commands. If you are not this user, you can switch with the following command:
Note: Execute each command one line at a time.
sudo su - ec2-user
# Install components sudo dnf install docker python3-pip git -y && pip3 install docker-compose # Fix docker python wrapper 7.0 SSL version issue pip3 install docker==6.1.3 # Configure components sudo systemctl enable docker && sudo systemctl start docker && sudo usermod -aG docker $USER # Exit the terminal exit
Reopen a terminal session and continue executing the following commands:
Note: Execute each command one line at a time.
# Log in as user ec2-user # Configure OpenSearch server parameters sudo sh -c "echo 'vm.max_map_count=262144' > /etc/sysctl.conf" && sudo sysctl -p # Clone the code git clone https://github.com/aws-samples/generative-bi-using-rag.git # Build docker images locally cd generative-bi-using-rag/application && cp .env.template .env && docker-compose build # Start all services docker-compose up -d # Wait 3 minutes for MySQL and OpenSearch to initialize sleep 180
Here is the English translation:
In the terminal, continue executing the following commands:
cd initial_data && unzip init_mysql_db.sql.zip && cd .. docker exec nlq-mysql sh -c "mysql -u root -ppassword -D llm < /opt/data/init_mysql_db.sql"
6.1 Initialize the index for the sample data by creating a new index:
docker exec nlq-webserver python opensearch_deploy.py
If the script fails due to any errors, delete the index and rerun the previous command:
curl -XDELETE -k -u admin:admin "https://localhost:9200/uba"
6.2 (Optional) Bulk import custom QA data by appending to an existing index:
docker exec nlq-webserver python opensearch_deploy.py custom false
Open in your browser: http://<your-ec2-public-ip>
Note: Use HTTP instead of HTTPS.
-
First create the corresponding Data Profile in Data Connection Management and Data Profile Management.
-
After selecting the Data Profile, start asking questions. For simple questions, the LLM can directly generate the correct SQL. If the generated SQL is incorrect, try adding more annotations to the Schema.
-
Use the Schema Management page, select the Data Profile, and add comments to the tables and fields. These comments will be included in the prompt sent to the LLM. (1) For some fields, add values to the Annotation attribute, e.g. "Values: Y|N", "Values: Shanghai|Jiangsu". (2) For table comments, add domain knowledge to help answer business questions.
-
Ask the question again. If still unable to generate the correct SQL, add Sample QA pairs to OpenSearch. (1) Using the Index Management page, select the Data Profile then you can add, view and delete QA pairs.
-
Ask again. In theory, the RAG approach (PE uses Few shots) should now be able to generate the correct SQL.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.