Skip to content

Commit afd9e90

Browse files
authored
Add files via upload
1 parent da75f52 commit afd9e90

File tree

2 files changed

+365
-0
lines changed

2 files changed

+365
-0
lines changed

app.py

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import streamlit as st
2+
from utils.config import db_credentials, MAX_TOKENS_ALLOWED, MAX_MESSAGES_TO_OPENAI, TOKEN_BUFFER
3+
from utils.system_prompts import get_final_system_prompt
4+
from utils.chat_functions import run_chat_sequence, clear_chat_history, count_tokens, prepare_sidebar_data
5+
from utils.database_functions import database_schema_dict
6+
from utils.function_calling_spec import functions
7+
from utils.helper_functions import save_conversation
8+
from assets.dark_theme import dark
9+
from assets.light_theme import light
10+
from assets.made_by_sdw import made_by_sdw
11+
12+
13+
14+
15+
16+
if __name__ == "__main__":
17+
18+
########### A. SIDEBAR ###########
19+
20+
# Prepare data for the sidebar dropdowns
21+
sidebar_data = prepare_sidebar_data(database_schema_dict)
22+
st.sidebar.markdown("<div class='made_by'>Made by SDW🔋</div>", unsafe_allow_html=True)
23+
24+
25+
26+
### POSTGRES DB OBJECTS VIEWER ###
27+
28+
st.markdown(made_by_sdw, unsafe_allow_html=True)
29+
st.sidebar.title("🔍 Postgres DB Objects Viewer")
30+
31+
32+
# Dropdown for schema selection
33+
selected_schema = st.sidebar.selectbox("📂 Select a schema", list(sidebar_data.keys()))
34+
35+
36+
# Dropdown for table selection based on chosen Schema
37+
selected_table = st.sidebar.selectbox("📜 Select a table", list(sidebar_data[selected_schema].keys()))
38+
39+
40+
# Display columns of the chosen table with interactivity using checkboxes
41+
st.sidebar.subheader(f"🔗 Columns in {selected_table}")
42+
for column in sidebar_data[selected_schema][selected_table]:
43+
is_checked = st.sidebar.checkbox(f"📌 {column}")
44+
45+
46+
47+
48+
49+
### SAVE CONVERSATION BUTTON ###
50+
51+
# Add a button to SAVE the chat/conversation
52+
if st.sidebar.button("Save Conversation💾"):
53+
saved_file_path = save_conversation(st.session_state["full_chat_history"])
54+
st.sidebar.success(f"Conversation saved to: {saved_file_path}")
55+
st.sidebar.markdown(f"Conversation saved! [Open File]({saved_file_path})")
56+
57+
58+
59+
60+
61+
### CLEAR CONVERSATION BUTTON ###
62+
63+
# Add a button to CLEAR the chat/conversation
64+
if st.sidebar.button("Clear Conversation🗑️"):
65+
save_conversation(st.session_state["full_chat_history"])
66+
clear_chat_history()
67+
68+
69+
70+
71+
72+
### TOGGLE THEME BUTTON ###
73+
74+
# Retrieve the current theme from session state
75+
current_theme = st.session_state.get("theme", "light")
76+
st.markdown(f"<body class='{current_theme}'></body>", unsafe_allow_html=True)
77+
78+
79+
# Initialize the theme in session state
80+
if "theme" not in st.session_state:
81+
st.session_state.theme = "light"
82+
83+
84+
# Add a button to toggle the UI colour theme
85+
if st.sidebar.button("Toggle Theme🚨"):
86+
st.session_state.theme = "dark" if st.session_state.theme == "light" else "light"
87+
st.experimental_rerun()
88+
89+
90+
91+
# Apply the theme based on session state
92+
theme_style = dark if st.session_state.theme == "dark" else light
93+
st.markdown(theme_style, unsafe_allow_html=True)
94+
95+
96+
97+
98+
99+
100+
101+
102+
103+
104+
105+
########### B. CHAT INTERFACE ###########
106+
107+
108+
109+
### TITLE ###
110+
111+
# Add title to the Streamlit chatbot app
112+
st.title("🤖 AI Database Chatbot 🤓")
113+
114+
115+
116+
### SESSION STATE ###
117+
118+
# Initialize the full chat messages history for UI
119+
if "full_chat_history" not in st.session_state:
120+
st.session_state["full_chat_history"] = [{"role": "system", "content": get_final_system_prompt(db_credentials=db_credentials)}]
121+
122+
123+
124+
# Initialize the API chat messages history for OpenAI requests
125+
if "api_chat_history" not in st.session_state:
126+
st.session_state["api_chat_history"] = [{"role": "system", "content": get_final_system_prompt(db_credentials=db_credentials)}]
127+
128+
129+
130+
### CHAT FACILITATION ###
131+
132+
# Start the chat
133+
if (prompt := st.chat_input("What do you want to know?")) is not None:
134+
st.session_state.full_chat_history.append({"role": "user", "content": prompt})
135+
136+
# Limit the number of messages sent to OpenAI by token count
137+
total_tokens = sum(count_tokens(message["content"]) for message in st.session_state["api_chat_history"])
138+
while total_tokens + count_tokens(prompt) + TOKEN_BUFFER > MAX_TOKENS_ALLOWED:
139+
removed_message = st.session_state["api_chat_history"].pop(0)
140+
total_tokens -= count_tokens(removed_message["content"])
141+
142+
st.session_state.api_chat_history.append({"role": "user", "content": prompt})
143+
144+
145+
146+
# Display previous chat messages from full_chat_history (ingore system prompt message)
147+
for message in st.session_state["full_chat_history"][1:]:
148+
if message["role"] == "user":
149+
st.chat_message("user", avatar='🧑‍💻').write(message["content"])
150+
elif message["role"] == "assistant":
151+
st.chat_message("assistant", avatar='🤖').write(message["content"])
152+
153+
if st.session_state["api_chat_history"][-1]["role"] != "assistant":
154+
with st.spinner("⌛Connecting to AI model..."):
155+
# Send only the most recent messages to OpenAI from api_chat_history
156+
recent_messages = st.session_state["api_chat_history"][-MAX_MESSAGES_TO_OPENAI:]
157+
new_message = run_chat_sequence(recent_messages, functions) # Get the latest message
158+
159+
# Add this latest message to both api_chat_history and full_chat_history
160+
st.session_state["api_chat_history"].append(new_message)
161+
st.session_state["full_chat_history"].append(new_message)
162+
163+
# Display the latest message from the assistant
164+
st.chat_message("assistant", avatar='🤖').write(new_message["content"])
165+
166+
max_tokens = MAX_TOKENS_ALLOWED
167+
current_tokens = sum(count_tokens(message["content"]) for message in st.session_state["full_chat_history"])
168+
progress = min(1.0, max(0.0, current_tokens / max_tokens))
169+
st.progress(progress)
170+
st.write(f"Tokens Used: {current_tokens}/{max_tokens}")
171+
if current_tokens > max_tokens:
172+
st.warning("Note: Due to character limits, some older messages might not be considered in ongoing conversations with the AI.")

requirements.txt

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
aiofiles==23.1.0
2+
aiohttp==3.9.3
3+
aiosignal==1.3.1
4+
altair==5.0.1
5+
anyio==3.7.1
6+
appdirs==1.4.4
7+
argcomplete==1.10.3
8+
asttokens==2.4.1
9+
async-timeout==4.0.3
10+
attrs==23.2.0
11+
backcall==0.2.0
12+
backports.functools-lru-cache==2.0.0
13+
beautifulsoup4==4.8.2
14+
blinker==1.6.2
15+
Bottleneck==1.3.8
16+
Brotli==1.0.9
17+
brotlipy==0.7.0
18+
cachetools==5.3.1
19+
certifi==2023.5.7
20+
cffi==1.16.0
21+
chardet==3.0.4
22+
charset-normalizer==3.3.2
23+
click==8.1.3
24+
colorama==0.4.6
25+
comm==0.2.2
26+
compressed-rtf==1.0.6
27+
comtypes==1.2.0
28+
contourpy==1.2.0
29+
cryptography==42.0.5
30+
cycler==0.12.1
31+
dataclasses-json==0.5.8
32+
debugpy==1.8.1
33+
decorator==5.1.1
34+
distro==1.9.0
35+
docx2txt==0.8
36+
dpcpp-cpp-rt==2024.0.2
37+
duckduckgo-search==3.8.4
38+
ebcdic==1.1.1
39+
et-xmlfile==1.1.0
40+
exceptiongroup==1.1.2
41+
executing==2.0.1
42+
extract-msg==0.28.7
43+
faiss-cpu==1.7.4
44+
ffmpeg-python==0.2.0
45+
filelock==3.12.2
46+
fonttools==4.25.0
47+
frozenlist==1.4.1
48+
fsspec==2023.6.0
49+
future==0.18.3
50+
gitdb==4.0.10
51+
GitPython==3.1.31
52+
greenlet==2.0.2
53+
h11==0.14.0
54+
h2==4.1.0
55+
hpack==4.0.0
56+
httpcore==0.17.3
57+
httpx==0.24.1
58+
huggingface-hub==0.15.1
59+
hyperframe==6.0.1
60+
idna==3.6
61+
IMAPClient==2.1.0
62+
importlib-metadata==6.11.0
63+
importlib_resources==6.3.0
64+
intel-cmplr-lib-rt==2024.0.2
65+
intel-cmplr-lic-rt==2024.0.2
66+
intel-opencl-rt==2024.0.2
67+
intel-openmp==2024.0.2
68+
ipykernel==6.29.3
69+
ipython==8.22.2
70+
jedi==0.19.1
71+
Jinja2==3.1.2
72+
joblib==1.3.2
73+
jsonschema==4.17.3
74+
jupyter_client==8.6.1
75+
jupyter_core==5.7.2
76+
kiwisolver==1.4.5
77+
langchain==0.0.150
78+
langchainplus-sdk==0.0.17
79+
llvmlite==0.40.1
80+
lxml==4.9.2
81+
Markdown==3.4.3
82+
markdown-it-py==3.0.0
83+
MarkupSafe==2.1.3
84+
marshmallow==3.19.0
85+
marshmallow-enum==1.5.1
86+
matplotlib==3.8.3
87+
matplotlib-inline==0.1.6
88+
mdurl==0.1.2
89+
mkl==2024.0.0
90+
mkl-fft==1.3.6
91+
mkl-random==1.2.2
92+
mkl-service==2.4.0
93+
more-itertools==10.0.0
94+
mpmath==1.3.0
95+
multidict==6.0.5
96+
munkres==1.1.4
97+
mypy-extensions==1.0.0
98+
nest-asyncio==1.6.0
99+
networkx==3.1
100+
numba==0.57.1
101+
numexpr==2.9.0
102+
numpy==1.24.4
103+
olefile==0.46
104+
openai==1.14.1
105+
openai-whisper==20230314
106+
openapi-schema-pydantic==1.2.4
107+
openpyxl==3.0.10
108+
packaging==23.2
109+
pandas==2.2.1
110+
pandas-stubs==2.0.2.230605
111+
parso==0.8.3
112+
pdfminer.six==20191110
113+
pickleshare==0.7.5
114+
Pillow==9.4.0
115+
platformdirs==4.2.0
116+
plotly==5.20.0
117+
pooch==1.8.1
118+
prompt-toolkit==3.0.43
119+
protobuf==4.23.3
120+
psutil==5.9.8
121+
psycopg2==2.9.9
122+
pure-eval==0.2.2
123+
pyarrow==12.0.1
124+
PyAudio==0.2.13
125+
pycparser==2.21
126+
pycryptodome==3.18.0
127+
pydantic==1.10.9
128+
pydeck==0.8.1b0
129+
Pygments==2.17.2
130+
Pympler==1.0.1
131+
pyOpenSSL==24.1.0
132+
pyparsing==3.1.2
133+
pypdf==3.11.0
134+
pypiwin32==223
135+
pyrsistent==0.19.3
136+
PySocks==1.7.1
137+
python-dateutil==2.9.0.post0
138+
python-docx==0.8.11
139+
python-dotenv==1.0.0
140+
python-pptx==0.6.21
141+
pyttsx3==2.90
142+
pytz==2024.1
143+
pytz-deprecation-shim==0.1.0.post0
144+
pywin32==306
145+
PyYAML==6.0
146+
pyzmq==25.1.2
147+
regex==2023.6.3
148+
requests==2.31.0
149+
rich==13.4.2
150+
safetensors==0.3.1
151+
scikit-learn==1.4.1.post1
152+
scipy==1.10.1
153+
six==1.12.0
154+
smmap==5.0.0
155+
sniffio==1.3.0
156+
socksio==1.0.0
157+
sortedcontainers==2.4.0
158+
soupsieve==2.4.1
159+
SpeechRecognition==3.8.1
160+
SQLAlchemy==2.0.17
161+
stack-data==0.6.3
162+
streamlit==1.24.1
163+
structlog==23.1.0
164+
sympy==1.12
165+
tbb==2021.11.0
166+
tenacity==8.2.3
167+
termcolor==2.3.0
168+
textract==1.6.5
169+
threadpoolctl==3.3.0
170+
tiktoken==0.3.1
171+
tokenizers==0.13.3
172+
toml==0.10.2
173+
toolz==0.12.0
174+
torch==2.0.1
175+
tornado==6.4
176+
tqdm==4.66.2
177+
traitlets==5.14.2
178+
transformers==4.30.2
179+
types-pytz==2024.1.0.20240203
180+
typing-inspect==0.9.0
181+
typing_extensions==4.10.0
182+
tzdata==2023.3
183+
tzlocal==4.3.1
184+
urllib3==2.2.1
185+
validators==0.20.0
186+
watchdog==3.0.0
187+
wcwidth==0.2.13
188+
wikipedia==1.4.0
189+
win-inet-pton==1.1.0
190+
xlrd==1.2.0
191+
XlsxWriter==3.1.2
192+
yarl==1.9.4
193+
zipp==3.18.1

0 commit comments

Comments
 (0)