Skip to content

Commit 7c4b05b

Browse files
Update Project to Reflect Changes to Required Python Packages, Python Docker Image, Amazon Bedrock, and RDS Engine Version #58 (#59)
* Update Bedrock default model. * Update default OpenAI model * Update Image Tag to 2.0.0 * Add RDSEngineVersion Parameter and Update Version to 16.1 * Update README.md to Reflect Code Changes * New Python Docker Image, Packages, and LLMs
1 parent 8d54dcd commit 7c4b05b

13 files changed

+152
-131
lines changed

README.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ performing NLQ. In addition to model choice, NLQ accuracy also relies heavily on
2121
prompt, prompt template, labeled sample queries used for in-context learning (_aka few-shot prompting_), and the naming
2222
conventions used for your database schema (tables and columns).
2323

24-
The NLQ Application was tested on a variety of open source and commercial FMs. As a baseline both Anthropic Claude v1.3
25-
and v2, and OpenAI's GPT-3 and GPT-4 series models were capable of accurately answering most or all sample questions
26-
included in this README file and the NLQ Application's web UI. Anthropic's Claude-series models,
27-
including `anthropic.claude-v1` and `anthropic.claude-v2`, as well as OpenAI's Generative Pre-trained Transformer GPT-3
28-
and GPT-4 series models, including `text-davinci-003` (Legacy), `gpt-3.5-turbo`, and `gpt-4`, all provide accurate
29-
responses to a wide range of complex natural language queries using an average amount of in-context learning and prompt
30-
engineering.
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.
3129

3230
Open source NLQ-capable models, such as `google/flan-t5-xxl` and `google/flan-t5-xxl-fp16` (half-precision
3331
floating-point format (FP16) version of the full model), are available through Amazon SageMaker JumpStart Foundation
@@ -45,20 +43,20 @@ the `NlqSageMakerEndpointStack.yaml` CloudFormation template file. You will firs
4543
the `NlqSageMakerEndpointStack.yaml` file and update the deployed CloudFormation stack, `NlqSageMakerEndpointStack`.
4644
Additionally, you may need to make adjustments to the NLQ Application, the `app_sagemaker.py` file, modifying
4745
the `ContentHandler` Class to match the response payload of the chosen model. Then, rebuild the Amazon ECR Docker Image,
48-
incrementing the version, e.g., `nlq-genai-1.0.1-sm`, using the `Dockerfile_SageMaker` Dockerfile and push to the Amazon
46+
incrementing the version, e.g., `nlq-genai-2.0.1-sm`, using the `Dockerfile_SageMaker` Dockerfile and push to the Amazon
4947
ECR repository. Lastly, you will need to update the deployed ECS task and service, which are part of
5048
the `NlqEcsSageMakerStack` CloudFormation stack.
5149

5250
### Option 2: Switching to Amazon Bedrock
5351

5452
Switching from the solution's default Amazon SageMaker JumpStart Foundation Model to Amazon Bedrock model's, such as
55-
Anthropic `anthropic.claude-v1` and `anthropic.claude-v2`, will provide superior results. Using Amazon Bedrock
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
5654
eliminates the need for the deployment of the `NlqSageMakerEndpointStack` CloudFormation stack. If the stack has already
5755
been deployed, it can be deleted. Next, build the Amazon ECR Docker Image using the `Dockerfile_Bedrock` Dockerfile and
58-
push the resulting image, e.g., `nlq-genai-1.0.0-bedrock`, to the Amazon ECR repository. Finally, deploy
56+
push the resulting image, e.g., `nlq-genai-2.0.0-bedrock`, to the Amazon ECR repository. Finally, deploy
5957
the `NlqEcsBedrockStack.yaml` CloudFormation template file. This stack replaces the the `NlqEcsSageMakerStack`
60-
CloudFormation stack, designed for use JumpStart Foundation Models. The default model used for this option is Anthropic
61-
Claude Instant v1, `anthropic.claude-instant-v1`.
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`).
6260

6361
### Option 3: Switching to a Third-party Model Provider's API
6462

@@ -67,7 +65,7 @@ such as OpenAI, Cohere, and Anthropic is straightforward. To utilize OpenAI's mo
6765
OpenAI account and obtain your own personal API key. Using a third-party model via an API eliminates the need for the
6866
deployment of the `NlqSageMakerEndpointStack` CloudFormation stack. If the stack has already been deployed, it can be
6967
deleted. Next, build the Amazon ECR Docker Image using the `Dockerfile_OpenAI` Dockerfile and push the resulting image,
70-
e.g., `nlq-genai-1.0.0-oai`, to the Amazon ECR repository. Finally, deploy the `NlqEcsOpenAIStack.yaml` CloudFormation
68+
e.g., `nlq-genai-2.0.0-oai`, to the Amazon ECR repository. Finally, deploy the `NlqEcsOpenAIStack.yaml` CloudFormation
7169
template file. This stack replaces the the `NlqEcsSageMakerStack` CloudFormation stack, designed for use JumpStart
7270
Foundation Models.
7371

@@ -121,8 +119,8 @@ the choice of model. Not all models are capable of NLQ, while others will not re
121119
one in your account, or the `AWSServiceRoleForECS` Service-Linked Role will not yet exist and the stack will fail.
122120
Check the `AWSServiceRoleForECS` Service-Linked Role before deploying the `NlqMainStack` stack. This role is
123121
auto-created the first time you create an ECS cluster in your account.
124-
4. If you use Option 1: SageMaker JumpStart FM Endpoint, build and push the `nlq-genai:1.0.0-sm` Docker image to the new
125-
Amazon ECR repository. Alternately, build and push the `nlq-genai:1.0.0-bedrock` or `nlq-genai:1.0.0-oai` Docker
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
123+
Amazon ECR repository. Alternately, build and push the `nlq-genai:2.0.0-bedrock` or `nlq-genai:2.0.0-oai` Docker
126124
image for use with Option 2: Bedrock or Option 3: OpenAI API.
127125
5. Import the included sample data into the Amazon RDS MoMA database.
128126
6. Add the `nlqapp` user to the MoMA database.
@@ -134,14 +132,9 @@ the choice of model. Not all models are capable of NLQ, while others will not re
134132

135133
### Step 2: Create AWS Secret Manager Secrets
136134

137-
Make sure you update the secret values before continuing.
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.
138136

139137
```sh
140-
aws secretsmanager create-secret \
141-
--name /nlq/MasterUsername \
142-
--description "Master username for RDS instance." \
143-
--secret-string "<your_master_username>"
144-
145138
aws secretsmanager create-secret \
146139
--name /nlq/NLQAppUsername \
147140
--description "NLQ Application username for MoMA database." \
@@ -189,23 +182,23 @@ aws ecr get-login-password --region us-east-1 | \
189182
Option 1: SageMaker JumpStart FM Endpoint
190183

191184
```sh
192-
TAG="1.0.0-sm"
185+
TAG="2.0.0-sm"
193186
docker build -f Dockerfile_SageMaker -t $ECS_REPOSITORY:$TAG .
194187
docker push $ECS_REPOSITORY:$TAG
195188
```
196189

197190
Option 2: Amazon Bedrock
198191

199192
```sh
200-
TAG="1.0.0-bedrock"
193+
TAG="2.0.0-bedrock"
201194
docker build -f Dockerfile_Bedrock -t $ECS_REPOSITORY:$TAG .
202195
docker push $ECS_REPOSITORY:$TAG
203196
```
204197

205198
Option 3: OpenAI API
206199

207200
```sh
208-
TAG="1.0.0-oai"
201+
TAG="2.0.0-oai"
209202
docker build -f Dockerfile_OpenAI -t $ECS_REPOSITORY:$TAG .
210203
docker push $ECS_REPOSITORY:$TAG
211204
```

cloudformation/NlqEcsBedrockStack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description: "SO9250 Guidance for Natural Language Queries of Relational Databas
33
Parameters:
44
BedrockModelName:
55
Type: String
6-
Default: "anthropic.claude-instant-v1"
6+
Default: "amazon.titan-text-express-v1"
77
Description: The Bedrock Model.
88

99
# BedrockModelTemperature:
@@ -50,7 +50,7 @@ Parameters:
5050

5151
ECRImageTag:
5252
Type: String
53-
Default: "1.0.0-bedrock"
53+
Default: "2.0.0-bedrock"
5454
Description: The name of the ECR Image tag to use with ECS/Fargate.
5555

5656
TaskName:

cloudformation/NlqEcsOpenAIStack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Description: "SO9250 Guidance for Natural Language Queries of Relational Databas
33
Parameters:
44
OpenAIModelName:
55
Type: String
6-
Default: "gpt-3.5-turbo"
6+
Default: "gpt-4"
77
Description: The OpenAI LLM to call via their API.
88

99
ECRImageTag:
1010
Type: String
11-
Default: "1.0.0-oai"
11+
Default: "2.0.0-oai"
1212
Description: The name of the ECR Image tag to use with ECS/Fargate.
1313

1414
TaskName:

cloudformation/NlqEcsSageMakerStack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description: "SO9250 Guidance for Natural Language Queries of Relational Databas
33
Parameters:
44
ECRImageTag:
55
Type: String
6-
Default: "1.0.0-sm"
6+
Default: "2.0.0-sm"
77
Description: The name of the ECR Image tag to use with ECS/Fargate.
88

99
TaskName:

cloudformation/NlqMainStack.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Parameters:
3030
Default: 5432
3131
Description: The port RDS is listening on.
3232

33+
RDSEngineVersion:
34+
Type: String
35+
Default: "16.1"
36+
Description: The RDS PostgreSQL engine version.
37+
3338
ALBPort:
3439
Type: Number
3540
Default: 80
@@ -226,7 +231,7 @@ Resources:
226231
BackupRetentionPeriod: 7
227232
PreferredMaintenanceWindow: "fri:04:30-fri:05:00"
228233
MultiAZ: true
229-
EngineVersion: "15.3"
234+
EngineVersion: !Ref RDSEngineVersion
230235
AutoMinorVersionUpgrade: true
231236
LicenseModel: "postgresql-license"
232237
PubliclyAccessible: false

docker/Dockerfile_Bedrock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Natural Language Query (NLQ) demo using Amazon RDS for PostgreSQL and Amazon Bedrock.
22
# Author: Gary A. Stafford (garystaf@amazon.com)
3-
# Date: 2023-10-10
3+
# Date: 2024-01-21
44

5-
FROM python:3.11.6-slim
5+
FROM python:3.12.2-slim
66

77
LABEL name="nlq-genai" \
8-
version="1.0.0-bedrock" \
8+
version="2.0.0-bedrock" \
99
maintainer="Gary A. Stafford (garystaf@amazon.com)"
1010

1111
ENV PIP_DEFAULT_TIMEOUT=100 \

docker/Dockerfile_OpenAI

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Natural Language Query (NLQ) demo using Amazon RDS for PostgreSQL and OpenAI's LLM models via their API.
22
# Author: Gary A. Stafford (garystaf@amazon.com)
3-
# Date: 2023-10-10
3+
# Date: 2024-01-21
44

5-
FROM python:3.11.6-slim
5+
FROM python:3.12.2-slim
66

77
LABEL name="nlq-genai" \
8-
version="1.0.0-oai" \
8+
version="2.0.0-oai" \
99
maintainer="Gary A. Stafford (garystaf@amazon.com)"
1010

1111
ENV PIP_DEFAULT_TIMEOUT=100 \

docker/Dockerfile_SageMaker

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Natural Language Query (NLQ) demo using Amazon RDS for PostgreSQL and Amazon SageMaker JumpStart Foundation Models.
22
# Author: Gary A. Stafford (garystaf@amazon.com)
3-
# Date: 2023-10-10
3+
# Date: 2024-01-21
44

5-
FROM python:3.11.6-slim
5+
FROM python:3.12.2-slim
66

77
LABEL name="nlq-genai" \
8-
version="1.0.0-sm" \
8+
version="2.0.0-sm" \
99
maintainer="Gary A. Stafford (garystaf@amazon.com)"
1010

1111
ENV PIP_DEFAULT_TIMEOUT=100 \

docker/app_bedrock.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
# Natural Language Query (NLQ) demo using Amazon RDS for PostgreSQL and Amazon Bedrock.
22
# Author: Gary A. Stafford (garystaf@amazon.com)
3-
# Date: 2023-08-12
3+
# Date: 2024-02-21
44
# Usage: streamlit run app_bedrock.py --server.runOnSave true
55

66
import ast
7+
import boto3
78
import json
89
import logging
910
import os
10-
11-
import boto3
1211
import pandas as pd
1312
import streamlit as st
1413
import yaml
1514
from botocore.exceptions import ClientError
16-
from langchain.sql_database import SQLDatabase
17-
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
1815
from langchain.chains.sql_database.prompt import PROMPT_SUFFIX, _postgres_prompt
1916
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
20-
from langchain.llms import Bedrock
17+
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
2118
from langchain.prompts.example_selector.semantic_similarity import (
2219
SemanticSimilarityExampleSelector,
2320
)
24-
from langchain.vectorstores import Chroma
21+
from langchain.sql_database import SQLDatabase
22+
from langchain_community.llms import Bedrock
23+
from langchain_community.vectorstores import Chroma
2524
from langchain_experimental.sql import SQLDatabaseChain
2625

2726
# ***** CONFIGURABLE PARAMETERS *****
2827
REGION_NAME = os.environ.get("REGION_NAME", "us-east-1")
29-
MODEL_NAME = os.environ.get("MODEL_NAME", "anthropic.claude-instant-v1")
28+
MODEL_NAME = os.environ.get("MODEL_NAME", "amazon.titan-text-express-v1")
3029
TEMPERATURE = os.environ.get("TEMPERATURE", 0.3)
31-
MAX_TOKENS_TO_SAMPLE = os.environ.get("MAX_TOKENS_TO_SAMPLE", 4096)
32-
TOP_K = os.environ.get("TOP_K", 250)
3330
TOP_P = os.environ.get("TOP_P", 1)
34-
STOP_SEQUENCES = os.environ.get("STOP_SEQUENCES", ["\n\nHuman"])
3531
BASE_AVATAR_URL = (
3632
"https://raw.githubusercontent.com/garystafford-aws/static-assets/main/static"
3733
)
@@ -40,7 +36,6 @@
4036
HUGGING_FACE_EMBEDDINGS_MODEL = os.environ.get(
4137
"HUGGING_FACE_EMBEDDINGS_MODEL", "sentence-transformers/all-MiniLM-L6-v2"
4238
)
43-
# ******************************************************************
4439

4540

4641
def main():
@@ -66,11 +61,8 @@ def main():
6661
NO_ANSWER_MSG = "Sorry, I was unable to answer your question."
6762

6863
parameters = {
69-
"max_tokens_to_sample": MAX_TOKENS_TO_SAMPLE,
7064
"temperature": TEMPERATURE,
71-
"top_k": TOP_K,
72-
"top_p": TOP_P,
73-
"stop_sequences": STOP_SEQUENCES,
65+
"topP": TOP_P,
7466
}
7567

7668
llm = Bedrock(
@@ -101,10 +93,13 @@ def main():
10193
st.session_state["past"] = []
10294

10395
if "query" not in st.session_state:
104-
st.session_state["query"] = []
96+
st.session_state["query"] = ""
10597

10698
if "query_text" not in st.session_state:
107-
st.session_state["query_text"] = []
99+
st.session_state["query_text"] = ""
100+
101+
if "query_error" not in st.session_state:
102+
st.session_state["query_error"] = ""
108103

109104
tab1, tab2, tab3 = st.tabs(["Chatbot", "Details", "Technologies"])
110105

@@ -124,10 +119,10 @@ def main():
124119
- Simple
125120
- How many artists are there in the collection?
126121
- How many pieces of artwork are there?
127-
- How many artists are there whose nationality is Italian?
128-
- How many artworks are by the artist Claude Monet?
122+
- How many artists are there whose nationality is 'Italian'?
123+
- How many artworks are by the artist 'Claude Monet'?
129124
- How many artworks are classified as paintings?
130-
- How many artworks were created by Spanish artists?
125+
- How many artworks were created by 'Spanish' artists?
131126
- How many artist names start with the letter 'M'?
132127
- Moderate
133128
- How many artists are deceased as a percentage of all artists?
@@ -170,33 +165,34 @@ def main():
170165
except Exception as exc:
171166
st.session_state.generated.append(NO_ANSWER_MSG)
172167
logging.error(exc)
168+
st.session_state["query_error"] = exc
173169

174170
# https://discuss.streamlit.io/t/streamlit-chat-avatars-not-working-on-cloud/46713/2
175171
if st.session_state["generated"]:
176172
with col1:
177173
for i in range(len(st.session_state["generated"]) - 1, -1, -1):
178174
if (i >= 0) and (
179-
st.session_state["generated"][i] != NO_ANSWER_MSG
175+
st.session_state["generated"][i] != NO_ANSWER_MSG
180176
):
181177
with st.chat_message(
182-
"assistant",
183-
avatar=f"{BASE_AVATAR_URL}/{ASSISTANT_ICON}",
178+
"assistant",
179+
avatar=f"{BASE_AVATAR_URL}/{ASSISTANT_ICON}",
184180
):
185181
st.write(st.session_state["generated"][i]["result"])
186182
with st.chat_message(
187-
"user",
188-
avatar=f"{BASE_AVATAR_URL}/{USER_ICON}",
183+
"user",
184+
avatar=f"{BASE_AVATAR_URL}/{USER_ICON}",
189185
):
190186
st.write(st.session_state["past"][i])
191187
else:
192188
with st.chat_message(
193-
"assistant",
194-
avatar=f"{BASE_AVATAR_URL}/{ASSISTANT_ICON}",
189+
"assistant",
190+
avatar=f"{BASE_AVATAR_URL}/{ASSISTANT_ICON}",
195191
):
196192
st.write(NO_ANSWER_MSG)
197193
with st.chat_message(
198-
"user",
199-
avatar=f"{BASE_AVATAR_URL}/{USER_ICON}",
194+
"user",
195+
avatar=f"{BASE_AVATAR_URL}/{USER_ICON}",
200196
):
201197
st.write(st.session_state["past"][i])
202198
with col2:
@@ -210,7 +206,7 @@ def main():
210206

211207
position = len(st.session_state["generated"]) - 1
212208
if (position >= 0) and (
213-
st.session_state["generated"][position] != NO_ANSWER_MSG
209+
st.session_state["generated"][position] != NO_ANSWER_MSG
214210
):
215211
st.markdown("Question:")
216212
st.code(
@@ -242,6 +238,10 @@ def main():
242238
st.markdown("Pandas DataFrame:")
243239
df = pd.DataFrame(data)
244240
df
241+
st.markdown("Query Error:")
242+
st.code(
243+
st.session_state["query_error"], language="text"
244+
)
245245
with tab3:
246246
with st.container():
247247
st.markdown("### Technologies")
@@ -371,7 +371,7 @@ def load_few_shot_chain(llm, db, examples):
371371
few_shot_prompt = FewShotPromptTemplate(
372372
example_selector=example_selector,
373373
example_prompt=example_prompt,
374-
prefix=_postgres_prompt + "Here are some examples:",
374+
prefix=_postgres_prompt + " Here are some examples:",
375375
suffix=PROMPT_SUFFIX,
376376
input_variables=["table_info", "input", "top_k"],
377377
)
@@ -389,6 +389,7 @@ def load_few_shot_chain(llm, db, examples):
389389
def clear_text():
390390
st.session_state["query"] = st.session_state["query_text"]
391391
st.session_state["query_text"] = ""
392+
st.session_state["query_error"] = ""
392393

393394

394395
def clear_session():

0 commit comments

Comments
 (0)