Skip to content

Commit 35ff65c

Browse files
Update Readme to Reflect Recent Updates to Code (#60)
Update Readme to Reflect Recent Updates to Code.
1 parent 7c4b05b commit 35ff65c

File tree

1 file changed

+92
-81
lines changed

1 file changed

+92
-81
lines changed

README.md

Lines changed: 92 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
## Guidance for Natural Language Queries of Relational Databases on AWS
22

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
109
language answers, along with the associated SQL query and Pandas DataFrame-compatible result set.
1110

1211
#### NLQ Application Chatbot Preview
@@ -19,55 +18,25 @@ The selection of the Foundation Model (FM) for Natural Language Query (NLQ) play
1918
ability to accurately translate natural language questions into natural language answers. Not all FMs are capable of
2019
performing NLQ. In addition to model choice, NLQ accuracy also relies heavily on factors such as the quality of the
2120
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.
7140

7241
## Sample Dataset
7342

@@ -105,34 +74,38 @@ Using the MoMA dataset, we can ask natural language questions, with varying leve
10574
- Who won the 2022 FIFA World Cup final?
10675

10776
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.
10979

110-
## Deployment Instructions
80+
## Deployment Instructions (see details below)
11181

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
11484
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.
11787
2. Create the required secrets in AWS Secret Manager using the AWS CLI.
11888
3. Deploy the `NlqMainStack` CloudFormation template. Please note, you will have needed to have used Amazon ECS at least
11989
one in your account, or the `AWSServiceRoleForECS` Service-Linked Role will not yet exist and the stack will fail.
12090
Check the `AWSServiceRoleForECS` Service-Linked Role before deploying the `NlqMainStack` stack. This role is
12191
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
12393
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.
12696
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
131100
the `NlqEcsOpenAIStack` template for use with Option 3: OpenAI API.
132101

133102
### Step 2: Create AWS Secret Manager Secrets
134103

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
108+
values can be found in AWS Secret Manager.
136109

137110
```sh
138111
aws secretsmanager create-secret \
@@ -154,6 +127,9 @@ aws secretsmanager create-secret \
154127

155128
### Step 3: Deploy the Main NLQ Stack: Networking, Security, RDS Instance, and ECR Repository
156129

130+
Access to the ALB and RDS will be limited externally to your current IP address. You will need to update if
131+
your IP address changes after deployment.
132+
157133
```sh
158134
cd cloudformation/
159135

@@ -166,7 +142,9 @@ aws cloudformation create-stack \
166142

167143
### Step 4: Build and Push the Docker Image to ECR
168144

169-
You can build the image locally, in a CI/CD pipeline, using SageMaker Notebook environment, or AWS Cloud9.
145+
Build the Docker image(s) for the NLQ application, based on your choice of model options. You can build the Docker
146+
image(s) locally, in a CI/CD pipeline, using SageMaker Notebook environment, or AWS Cloud9. I prefer AWS Cloud9 for
147+
developing and testing the application and building the Docker images.
170148

171149
```sh
172150
cd docker/
@@ -179,7 +157,7 @@ aws ecr get-login-password --region us-east-1 | \
179157
docker login --username AWS --password-stdin $ECS_REPOSITORY
180158
```
181159

182-
Option 1: SageMaker JumpStart FM Endpoint
160+
Option 1: Amazon SageMaker JumpStart
183161

184162
```sh
185163
TAG="2.0.0-sm"
@@ -203,12 +181,12 @@ docker build -f Dockerfile_OpenAI -t $ECS_REPOSITORY:$TAG .
203181
docker push $ECS_REPOSITORY:$TAG
204182
```
205183

206-
### Step 5: Import Sample Data and Configure the MoMA Database
184+
### Step 5: Configure MoMA Database and Import Sample Data
207185

208-
A. Connect to the `moma` database using your preferred PostgreSQL tool. You may need to enable `Public access` for the
186+
5a. Connect to the `moma` database using your preferred PostgreSQL tool. You will need to enable `Public access` for the
209187
RDS instance temporarily depending on how you connect to the database.
210188

211-
B. Create the two MoMA collection tables into the `moma` database.
189+
5b. Create the two MoMA collection tables into the `moma` database.
212190

213191
```sql
214192
CREATE TABLE public.artists
@@ -248,7 +226,7 @@ CREATE TABLE public.artworks
248226
)
249227
```
250228

251-
C. Unzip and import the two data files into the `moma` database using the text files in the `/data` sub-directory. The
229+
5c. Unzip and import the two data files into the `moma` database using the text files in the `/data` subdirectory. The
252230
both files contain a header row and pipe-delimited ('|').
253231

254232
```txt
@@ -260,8 +238,8 @@ both files contain a header row and pipe-delimited ('|').
260238

261239
### Step 6: Add NLQ Application to the MoMA Database
262240

263-
Create the read-only NLQ Application database user account. Update the username and password you configured in step 2,
264-
with the secrets.
241+
Create the read-only NLQ Application database user account. Update the username and password values in the SQL script,
242+
in three places, with the secrets you configured in Step 2 above.
265243

266244
```sql
267245
CREATE ROLE <your_nlqapp_username> WITH
@@ -280,9 +258,9 @@ TO
280258
<your_nlqapp_username>;
281259
```
282260

283-
### Step 7: Deploy the ML Stack: Model and Endpoint
261+
### Optional Step 7: Deploy the Amazon SageMaker JumpStart Stack: Model and Endpoint
284262

285-
Option 1: SageMaker JumpStart FM Endpoint
263+
Option 1: Amazon SageMaker JumpStart
286264

287265
```sh
288266
cd cloudformation/
@@ -295,7 +273,7 @@ aws cloudformation create-stack \
295273

296274
### Step 8: Deploy the ECS Service Stack: Task and Service
297275

298-
Option 1: SageMaker JumpStart FM Endpoint
276+
Option 1: Amazon SageMaker JumpStart
299277

300278
```sh
301279
aws cloudformation create-stack \
@@ -322,6 +300,39 @@ aws cloudformation create-stack \
322300
--capabilities CAPABILITY_NAMED_IAM
323301
```
324302

303+
## Switching Foundation Models
304+
305+
### Option 1: Alternate Amazon SageMaker JumpStart Foundation Models
306+
307+
You can replace the default `google/flan-t5-xxl-fp16` JumpStart Foundation Model, deployed using
308+
the `NlqSageMakerEndpointStack.yaml` CloudFormation template file. You will first need to modify the model parameters in
309+
the `NlqSageMakerEndpointStack.yaml` file and update the deployed CloudFormation stack, `NlqSageMakerEndpointStack`.
310+
Additionally, you will need to make adjustments to the NLQ Application, `app_sagemaker.py`, modifying
311+
the `ContentHandler` Class to match the response payload of the chosen model. Then, rebuild the Amazon ECR Docker Image,
312+
incrementing the version, e.g., `nlq-genai-2.0.1-sm`, using the `Dockerfile_SageMaker` Dockerfile and push to the Amazon
313+
ECR repository. Lastly, you will need to update the deployed ECS task and service, which are part of
314+
the `NlqEcsSageMakerStack` CloudFormation stack.
315+
316+
### Option 2: Alternate Amazon Bedrock Foundation Models
317+
318+
To switch from the solution's default Amazon Titan Text G1 - Express (`amazon.titan-text-express-v1`) Foundation Model,
319+
you need to modify and rdeploy the `NlqEcsBedrockStack.yaml` CloudFormation template file. Additionally, you will need
320+
to modify to the NLQ Application, `app_bedrock.py` Then, rebuild the Amazon ECR Docker Image using
321+
the `Dockerfile_Bedrock`
322+
Dockerfile and push the resulting image, e.g., `nlq-genai-2.0.1-bedrock`, to the Amazon ECR repository. Lastly, you will
323+
need to
324+
update the deployed ECS task and service, which are part of the `NlqEcsBedrockStack` CloudFormation stack.
325+
326+
### Option 3: Alternate Third-party Foundation Models
327+
328+
Switching from the solution's default OpenAI API to another third-party model provider's API,
329+
such as Cohere or Anthropic, is similarly straightforward. To utilize OpenAI's models, you will first need to create an
330+
OpenAI account and obtain your own personal API key. Next, modify and rebuild the Amazon ECR Docker Image using
331+
the `Dockerfile_OpenAI` Dockerfile and push the resulting image,
332+
e.g., `nlq-genai-2.0.1-oai`, to the Amazon ECR repository. Finally, modify and redeploy the `NlqEcsOpenAIStack.yaml`
333+
CloudFormation
334+
template file.
335+
325336
## Security
326337

327338
See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.

0 commit comments

Comments
 (0)