forked from kaarthik108/snowChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (97 loc) · 3.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import streamlit as st
import re
import warnings
from chain import load_chain
from utils.snowchat_ui import message_func
from utils.snowddl import Snowddl
from snowflake.snowpark.exceptions import SnowparkSQLException
from utils.snow_connect import SnowflakeConnection
warnings.filterwarnings("ignore")
chat_history = []
snow_ddl = Snowddl()
st.title("snowChat")
st.caption("Talk your way through data")
INITIAL_MESSAGE = [
{"role": "user", "content": "Hi!"},
{
"role": "assistant",
"content": "Hey there, I'm Chatty McQueryFace, your SQL-speaking sidekick, ready to chat up Snowflake and fetch answers faster than a snowball fight in summer! ❄️🔍",
},
]
with open("ui/sidebar.md", "r") as sidebar_file:
sidebar_content = sidebar_file.read()
with open("ui/styles.md", "r") as styles_file:
styles_content = styles_file.read()
# Display the DDL for the selected table
st.sidebar.markdown(sidebar_content)
# Create a sidebar with a dropdown menu
selected_table = st.sidebar.selectbox(
"Select a table:", options=list(snow_ddl.ddl_dict.keys())
)
st.sidebar.markdown(f"### DDL for {selected_table} table")
st.sidebar.code(snow_ddl.ddl_dict[selected_table], language="sql")
# Add a reset button
if st.sidebar.button("Reset Chat"):
for key in st.session_state.keys():
del st.session_state[key]
st.session_state["messages"] = INITIAL_MESSAGE
st.session_state["history"] = []
st.write(styles_content, unsafe_allow_html=True)
# Initialize the chat messages history
if "messages" not in st.session_state.keys():
st.session_state["messages"] = INITIAL_MESSAGE
if "history" not in st.session_state:
st.session_state["history"] = []
# Prompt for user input and save
if prompt := st.chat_input():
st.session_state.messages.append({"role": "user", "content": prompt})
for message in st.session_state.messages:
message_func(
message["content"],
True if message["role"] == "user" else False,
True if message["role"] == "data" else False,
)
chain = load_chain()
conn = SnowflakeConnection().get_session()
def append_chat_history(question, answer):
st.session_state["history"].append((question, answer))
def get_sql(text):
sql_match = re.search(r"```sql\n(.*)\n```", text, re.DOTALL)
return sql_match.group(1) if sql_match else None
def append_message(content, role="assistant", display=False):
message = {"role": role, "content": content}
message_func(content, False, display)
st.session_state.messages.append(message)
if role != "data":
append_chat_history(st.session_state.messages[-2]["content"], content)
def handle_sql_exception(query, conn, e, retries=2):
append_message("Uh oh, I made an error, let me try to fix it..")
error_message = (
"I have an SQL query that's causing an error. FIX The SQL query by searching the schema definition: \n```sql\n"
+ query
+ "\n```\n Error message: \n "
+ str(e)
)
new_query = chain({"question": error_message, "chat_history": ""})["answer"]
append_message(new_query)
if get_sql(new_query) and retries > 0:
return execute_sql(get_sql(new_query), conn, retries - 1)
else:
append_message("I'm sorry, I couldn't fix the error. Please try again.")
return None
def execute_sql(query, conn, retries=2):
try:
return conn.sql(query).collect()
except SnowparkSQLException as e:
return handle_sql_exception(query, conn, e, retries)
if st.session_state.messages[-1]["role"] != "assistant":
content = st.session_state.messages[-1]["content"]
if isinstance(content, str):
result = chain(
{"question": content, "chat_history": st.session_state["history"]}
)["answer"]
append_message(result)
if get_sql(result):
df = execute_sql(get_sql(result), conn)
if df is not None:
append_message(df, "data", True)