You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+92-81Lines changed: 92 additions & 81 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,11 @@
1
1
## Guidance for Natural Language Queries of Relational Databases on AWS
2
2
3
-
This AWS Solution contains a demonstration of Generative AI, specifically, the use of Natural Language Query (NLQ) to
4
-
ask questions of an Amazon RDS for PostgreSQL database. The solution has the option to use Amazon SageMaker JumpStart
5
-
Foundation Models, Amazon Bedrock, or OpenAI. The demonstration's web-based application, running on Amazon ECS on AWS
6
-
Fargate, uses a combination
7
-
of [LangChain](https://python.langchain.com/docs/get_started/introduction.html), [Streamlit](https://streamlit.io/), [Chroma](https://www.trychroma.com/)
8
-
open-source embedding database, and [HuggingFace SentenceTransformers](https://huggingface.co/sentence-transformers) to
9
-
create sentence embeddings. The application accepts natural language questions from end-users and returns natural
3
+
This [AWS Solution](https://aws.amazon.com/solutions/guidance/natural-language-queries-of-relational-databases-on-aws/#) contains a demonstration of Generative AI, specifically, the use of Natural Language Query (NLQ) to
4
+
ask questions of an Amazon RDS for PostgreSQL database. This solution offers three architectural options for Foundation
5
+
Models: 1. Amazon SageMaker JumpStart, 2. Amazon Bedrock, and 3. OpenAI API. The demonstration's web-based
6
+
application, running on Amazon ECS on AWS Fargate, uses a combination
7
+
of [LangChain](https://python.langchain.com/docs/get_started/introduction.html), [Streamlit](https://streamlit.io/), [Chroma](https://www.trychroma.com/), and [HuggingFace SentenceTransformers](https://huggingface.co/sentence-transformers).
8
+
The application accepts natural language questions from end-users and returns natural
10
9
language answers, along with the associated SQL query and Pandas DataFrame-compatible result set.
11
10
12
11
#### NLQ Application Chatbot Preview
@@ -19,55 +18,25 @@ The selection of the Foundation Model (FM) for Natural Language Query (NLQ) play
19
18
ability to accurately translate natural language questions into natural language answers. Not all FMs are capable of
20
19
performing NLQ. In addition to model choice, NLQ accuracy also relies heavily on factors such as the quality of the
21
20
prompt, prompt template, labeled sample queries used for in-context learning (_aka few-shot prompting_), and the naming
22
-
conventions used for your database schema (tables and columns).
23
-
24
-
The NLQ Application was tested on a variety of open source and commercial FMs. As a baseline OpenAI's Generative
25
-
Pre-trained Transformer GPT-3 and GPT-4 series models, including `gpt-3.5-turbo`, and
26
-
`gpt-4`, all provide accurate responses to a wide range of complex natural language queries using an average amount of
27
-
in-context learning and prompt engineering. In addition, the Amazon Titan Text G1 - Express, `amazon.titan-text-express-v1`,
28
-
was able to answer most of the Simple questions with some model specific prompt optimization.
29
-
30
-
Open source NLQ-capable models, such as `google/flan-t5-xxl` and `google/flan-t5-xxl-fp16` (half-precision
31
-
floating-point format (FP16) version of the full model), are available through Amazon SageMaker JumpStart Foundation
32
-
Models. While the `google/flan-t5` series of models are a popular choice for building Generative AI applications, their
33
-
capabilities for NLQ are limited compared to other commercial LLMs. The demonstration's `google/flan-t5-xxl-fp16` is
34
-
capable of answering basic natural language queries with sufficient in-context learning, but will fail to return an
35
-
answer, provide incorrect answers, or cause the model endpoint to experience timeouts due to resource exhaustion when
36
-
faced with moderate to complex questions. Users are encouraged to experiment with a variety of open source and
37
-
commercial JumpStart Foundation Models and Amazon Bedrock.
38
-
39
-
### Option 1: Switching JumpStart Foundation Models
40
-
41
-
You can replace the default `google/flan-t5-xxl-fp16` JumpStart Foundation Model, deployed using
42
-
the `NlqSageMakerEndpointStack.yaml` CloudFormation template file. You will first need to modify the model parameters in
43
-
the `NlqSageMakerEndpointStack.yaml` file and update the deployed CloudFormation stack, `NlqSageMakerEndpointStack`.
44
-
Additionally, you may need to make adjustments to the NLQ Application, the `app_sagemaker.py` file, modifying
45
-
the `ContentHandler` Class to match the response payload of the chosen model. Then, rebuild the Amazon ECR Docker Image,
46
-
incrementing the version, e.g., `nlq-genai-2.0.1-sm`, using the `Dockerfile_SageMaker` Dockerfile and push to the Amazon
47
-
ECR repository. Lastly, you will need to update the deployed ECS task and service, which are part of
48
-
the `NlqEcsSageMakerStack` CloudFormation stack.
49
-
50
-
### Option 2: Switching to Amazon Bedrock
51
-
52
-
Switching from the solution's default Amazon SageMaker JumpStart Foundation Model to Amazon Bedrock model's, such as
53
-
Amazon Titan Text G1 - Express (`amazon.titan-text-express-v1`), will provide better results than JumpStarts's `google/flan-t5-xxl-fp16`. Using Amazon Bedrock
54
-
eliminates the need for the deployment of the `NlqSageMakerEndpointStack` CloudFormation stack. If the stack has already
55
-
been deployed, it can be deleted. Next, build the Amazon ECR Docker Image using the `Dockerfile_Bedrock` Dockerfile and
56
-
push the resulting image, e.g., `nlq-genai-2.0.0-bedrock`, to the Amazon ECR repository. Finally, deploy
57
-
the `NlqEcsBedrockStack.yaml` CloudFormation template file. This stack replaces the the `NlqEcsSageMakerStack`
58
-
CloudFormation stack, designed for use JumpStart Foundation Models. The default Amazon Bedrock model used for this option is
59
-
Amazon Titan Text G1 - Express (`amazon.titan-text-express-v1`).
60
-
61
-
### Option 3: Switching to a Third-party Model Provider's API
62
-
63
-
Switching from the solution's default Amazon SageMaker JumpStart Foundation Model to third-party model provider's API,
64
-
such as OpenAI, Cohere, and Anthropic is straightforward. To utilize OpenAI's models, you will first need to create an
65
-
OpenAI account and obtain your own personal API key. Using a third-party model via an API eliminates the need for the
66
-
deployment of the `NlqSageMakerEndpointStack` CloudFormation stack. If the stack has already been deployed, it can be
67
-
deleted. Next, build the Amazon ECR Docker Image using the `Dockerfile_OpenAI` Dockerfile and push the resulting image,
68
-
e.g., `nlq-genai-2.0.0-oai`, to the Amazon ECR repository. Finally, deploy the `NlqEcsOpenAIStack.yaml` CloudFormation
69
-
template file. This stack replaces the the `NlqEcsSageMakerStack` CloudFormation stack, designed for use JumpStart
70
-
Foundation Models.
21
+
conventions used for the database schema, both tables and columns.
22
+
23
+
The NLQ Application was tested on a variety of open source and commercial FMs. As a baseline, OpenAI's Generative
24
+
Pre-trained Transformer GPT-3 and GPT-4 series models, including `gpt-3.5-turbo`, and
25
+
`gpt-4`, all provide accurate responses to a wide range of simple to complex natural language queries using an average
26
+
amount of in-context learning and minimal prompt engineering.
27
+
28
+
Amazon Titan Text G1 - Express, `amazon.titan-text-express-v1`, available through Amazon Bedrock, was also tested. This
29
+
model provided accurate responses to basic natural language queries using some model-specific prompt
30
+
optimization. However, this model was not able to respond to more complex queries. Further prompt
31
+
optimization could improve model accuracy.
32
+
33
+
Open source models, such as `google/flan-t5-xxl` and `google/flan-t5-xxl-fp16` (half-precision
34
+
floating-point format (FP16) version of the full model), are available through Amazon SageMaker JumpStart. While the `google/flan-t5` series of models are a popular choice for building Generative AI applications, their
35
+
capabilities for NLQ are limited compared to newer open-source and commercial LLMs. The
36
+
demonstration's `google/flan-t5-xxl-fp16` is capable of answering basic natural language queries with sufficient
37
+
in-context learning. However, it often failed during testing to return an
38
+
answer or provide correct answers, and it frequently caused SageMaker model endpoint timeouts due to
39
+
resource exhaustion when faced with moderate to complex queries.
71
40
72
41
## Sample Dataset
73
42
@@ -105,34 +74,38 @@ Using the MoMA dataset, we can ask natural language questions, with varying leve
105
74
- Who won the 2022 FIFA World Cup final?
106
75
107
76
Again, the ability of the NLQ Application to return an answer and return an accurate answer, is primarily dependent on
108
-
the choice of model. Not all models are capable of NLQ, while others will not return accurate answers.
77
+
the choice of model. Not all models are capable of NLQ, while others will not return accurate answers. Optimizing the
78
+
above prompts for specific models can help improve accuracy.
109
79
110
-
## Deployment Instructions
80
+
## Deployment Instructions (see details below)
111
81
112
-
1.If you use Option 1: SageMaker JumpStart FM Endpoint, make you have the required EC2 instance for the endpoint
113
-
inference, or request it using Service Quotas in the AWS Management Console (e.g., `ml.g5.24xlarge` for
82
+
1.Optionally, for Option 1: Amazon SageMaker JumpStart, ensure that you have the required EC2 instance for the endpoint
83
+
inference, or request it using [Service Quotas](https://us-east-1.console.aws.amazon.com/servicequotas/home/services/sagemaker/quotas/L-6821867B) in the AWS Management Console (e.g., `ml.g5.24xlarge` for
114
84
the `google/flan-t5-xxl-fp16`
115
-
model: https://us-east-1.console.aws.amazon.com/servicequotas/home/services/sagemaker/quotas/L-6821867B). Refer to
116
-
the model's documentation for the choice of instance type.
85
+
model. Refer to
86
+
the model's documentation for the choice of instance types.
117
87
2. Create the required secrets in AWS Secret Manager using the AWS CLI.
118
88
3. Deploy the `NlqMainStack` CloudFormation template. Please note, you will have needed to have used Amazon ECS at least
119
89
one in your account, or the `AWSServiceRoleForECS` Service-Linked Role will not yet exist and the stack will fail.
120
90
Check the `AWSServiceRoleForECS` Service-Linked Role before deploying the `NlqMainStack` stack. This role is
121
91
auto-created the first time you create an ECS cluster in your account.
122
-
4. If you use Option 1: SageMaker JumpStart FM Endpoint, build and push the `nlq-genai:2.0.0-sm` Docker image to the new
92
+
4. If you use Option 1: Amazon SageMaker JumpStart, build and push the `nlq-genai:2.0.0-sm` Docker image to the new
123
93
Amazon ECR repository. Alternately, build and push the `nlq-genai:2.0.0-bedrock` or `nlq-genai:2.0.0-oai` Docker
124
-
image for use with Option 2: Bedrock or Option 3: OpenAI API.
125
-
5.Import the included sample data into the Amazon RDS MoMA database.
94
+
image for use with Option 2: Amazon Bedrock or Option 3: OpenAI API.
95
+
5.Create the Amazon RDS MoMA database tables and import the included sample data.
126
96
6. Add the `nlqapp` user to the MoMA database.
127
-
7. Deploy the `NlqSageMakerEndpointStack` CloudFormation template, using the Amazon SageMaker JumpStart Foundation
128
-
Models option.
129
-
8. If you use Option 1: SageMaker JumpStart FM Endpoint, deploy the `NlqEcsSageMakerStack` CloudFormation template.
130
-
Alternately, deploy the `NlqEcsBedrockStack` CloudFormation template for use with Option 2: Bedrock, or
97
+
7. Optionally, for Option 1: Amazon SageMaker JumpStart, deploy the `NlqSageMakerEndpointStack` CloudFormation template.
98
+
8. For Option 1: Amazon SageMaker JumpStart, deploy the `NlqEcsSageMakerStack` CloudFormation template.
99
+
Alternately, deploy the `NlqEcsBedrockStack` CloudFormation template for use with Option 2: Amazon Bedrock or
131
100
the `NlqEcsOpenAIStack` template for use with Option 3: OpenAI API.
132
101
133
102
### Step 2: Create AWS Secret Manager Secrets
134
103
135
-
Make sure you update the secret values below before continuing. This step will create secrets for the credentials for the NLQ application. Optionally, this step will create a secret to store your OpenAI API key. Master User credentials for the RDS instance are set automatically and stored in AWS Secret Manager as part of the `NlqMainStack` CloudFormation template.
104
+
Make sure you update the secret values below before continuing. This step will create secrets for the credentials for
105
+
the NLQ application. NLQ application access to the database is limited to read-only. For Option 3: OpenAI API, this step
106
+
will create a secret to store your OpenAI API key. Master User credentials for the Amazon RDS instance are set
107
+
automatically and stored in AWS Secret Manager as part of the `NlqMainStack`CloudFormation template deployment. These
0 commit comments