Skip to content

Commit cb7d1ee

Browse files
authored
Merge pull request #54 from aws-samples/dengru_dev
Add prompt management page with text2sql prompt storage
2 parents 77fec41 + 79505dc commit cb7d1ee

File tree

8 files changed

+85
-7
lines changed

8 files changed

+85
-7
lines changed

application/nlq/business/profile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ def update_table_def(cls, profile_name, tables_info, merge_before_update=False):
6666

6767
cls.profile_config_dao.update_table_def(profile_name, tables_info)
6868
logger.info(f"Table definition updated")
69+
70+
@classmethod
71+
def update_table_prompt(cls, profile_name, system_prompt, user_prompt):
72+
cls.profile_config_dao.update_table_prompt(profile_name, system_prompt, user_prompt)
73+
logger.info(f"System and user prompt updated")

application/nlq/data_access/dynamo_profile.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import List
44
from boto3.dynamodb.conditions import Key, Attr
55
from botocore.exceptions import ClientError
6+
from utils.prompts.generate_prompt import system_prompt_dict, user_prompt_dict
67

78
logger = logging.getLogger(__name__)
89

@@ -12,13 +13,17 @@
1213

1314
class ProfileConfigEntity:
1415

15-
def __init__(self, profile_name: str, conn_name: str, schemas: List[str], tables: List[str], comments: str, tables_info: dict=None):
16+
def __init__(self, profile_name: str, conn_name: str, schemas: List[str], tables: List[str], comments: str,
17+
tables_info: dict = None, system_prompt: dict = system_prompt_dict,
18+
user_prompt: dict = user_prompt_dict):
1619
self.profile_name = profile_name
1720
self.conn_name = conn_name
1821
self.schemas = schemas
1922
self.tables = tables
2023
self.comments = comments
2124
self.tables_info = tables_info
25+
self.system_prompt = system_prompt
26+
self.user_prompt = user_prompt
2227

2328
def to_dict(self):
2429
"""Convert to DynamoDB item format"""
@@ -27,7 +32,9 @@ def to_dict(self):
2732
'profile_name': self.profile_name,
2833
'schemas': self.schemas,
2934
'tables': self.tables,
30-
'comments': self.comments
35+
'comments': self.comments,
36+
'system_prompt': self.system_prompt,
37+
'user_prompt': self.user_prompt
3138
}
3239
if self.tables_info:
3340
base_props['tables_info'] = self.tables_info
@@ -137,4 +144,24 @@ def update_table_def(self, profile_name, tables_info):
137144
)
138145
raise
139146
else:
140-
return response["Attributes"]
147+
return response["Attributes"]
148+
149+
def update_table_prompt(self, profile_name, system_prompt, user_prompt):
150+
try:
151+
response = self.table.update_item(
152+
Key={"profile_name": profile_name},
153+
UpdateExpression="set system_prompt=:sp, user_prompt=:up",
154+
ExpressionAttributeValues={":sp": system_prompt, ":up": user_prompt},
155+
ReturnValues="UPDATED_NEW",
156+
)
157+
except ClientError as err:
158+
logger.error(
159+
"Couldn't update profile %s in table %s. Here's why: %s: %s",
160+
profile_name,
161+
self.table.name,
162+
err.response["Error"]["Code"],
163+
err.response["Error"]["Message"],
164+
)
165+
raise
166+
else:
167+
return response["Attributes"]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import streamlit as st
2+
from dotenv import load_dotenv
3+
import logging
4+
from nlq.business.profile import ProfileManagement
5+
from utils.navigation import make_sidebar
6+
7+
logger = logging.getLogger(__name__)
8+
9+
10+
def main():
11+
load_dotenv()
12+
logger.info('start prompt management')
13+
st.set_page_config(page_title="Prompt Management")
14+
make_sidebar()
15+
16+
with st.sidebar:
17+
st.title("Prompt Management")
18+
current_profile = st.selectbox("My Data Profiles", ProfileManagement.get_all_profiles(),
19+
index=None,
20+
placeholder="Please select data profile...", key='current_profile_name')
21+
22+
if current_profile is not None:
23+
profile_detail = ProfileManagement.get_profile_by_name(current_profile)
24+
25+
system_prompt = profile_detail.system_prompt
26+
user_prompt = profile_detail.user_prompt
27+
if system_prompt is not None and user_prompt is not None:
28+
model_selected_table = st.selectbox("LLM Model", system_prompt.keys(), index=None,
29+
placeholder="Please select a model")
30+
if model_selected_table is not None:
31+
system_prompt_input = st.text_area('System Prompt', system_prompt[model_selected_table])
32+
user_prompt_input = st.text_area('User Prompt', user_prompt[model_selected_table], height=500)
33+
34+
if st.button('Save', type='primary'):
35+
# assign new system/user prompt by selected model
36+
system_prompt[model_selected_table] = system_prompt_input
37+
user_prompt[model_selected_table] = user_prompt_input
38+
39+
# save new profile to DynamoDB
40+
ProfileManagement.update_table_prompt(current_profile, system_prompt, user_prompt)
41+
st.success('saved.')
42+
43+
44+
if __name__ == '__main__':
45+
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

application/utils/navigation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ def make_sidebar():
3939
st.page_link("pages/2_🪙_Data_Connection_Management.py", label="Data Connection Management", icon="🪙")
4040
st.page_link("pages/3_🪙_Data_Profile_Management.py", label="Data Profile Management", icon="🪙")
4141
st.page_link("pages/4_🪙_Schema_Description_Management.py", label="Schema Description Management", icon="🪙")
42+
st.page_link("pages/5_🪙_Prompt_Management.py", label="Prompt Management", icon="🪙")
4243
st.markdown(":gray[Performance Enhancement]")
43-
st.page_link("pages/5_📚_Index_Management.py", label="Index Management", icon="📚")
44-
st.page_link("pages/6_📚_Entity_Management.py", label="Entity Management", icon="📚")
45-
st.page_link("pages/7_📚_Agent_Cot_Management.py", label="Agent Cot Management", icon="📚")
44+
st.page_link("pages/6_📚_Index_Management.py", label="Index Management", icon="📚")
45+
st.page_link("pages/7_📚_Entity_Management.py", label="Entity Management", icon="📚")
46+
st.page_link("pages/8_📚_Agent_Cot_Management.py", label="Agent Cot Management", icon="📚")
4647
st.markdown(":gray[Dashboard Customization Management]")
47-
st.page_link("pages/8_🖥_Suggested_Question_Management.py", label="Suggested Question Management",
48+
st.page_link("pages/9_🖥_Suggested_Question_Management.py", label="Suggested Question Management",
4849
icon="🖥")
4950

5051
if st.button("Log out"):

0 commit comments

Comments
 (0)