-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
354 lines (292 loc) · 12.1 KB
/
chatbot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import json
import logging
from typing import Dict, List, Optional
import streamlit as st
from snowflake.snowpark.context import get_active_session
from snowflake.snowpark.session import Session
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Available models for selection
AVAILABLE_MODELS = [
"claude-3-5-sonnet",
"gemma-7b",
"jamba-1.5-mini",
"jamba-1.5-large",
"jamba-instruct",
"llama2-70b-chat",
"llama3-8b",
"llama3-70b",
"llama3.1-8b",
"llama3.1-70b",
"llama3.1-405b",
"llama3.2-1b",
"llama3.2-3b",
"llama3.3-70b",
"mistral-large",
"mistral-large2",
"mistral-7b",
"mixtral-8x7b",
"reka-core",
"reka-flash",
"snowflake-arctic",
"snowflake-llama-3.1-405b",
"snowflake-llama-3.3-70b",
]
class ChatMessage:
"""
A class to represent and validate individual chat messages.
This class ensures that messages conform to expected format and content requirements
before they are processed by the chat system.
"""
def __init__(self, role: str, content: str):
"""
Initialize a new chat message with validation.
Args:
role (str): The role of the message sender ('assistant' or 'user')
content (str): The actual message content
Raises:
ValueError: If role is invalid or content is empty/non-string
"""
# Validate the role is one of the allowed types
if role not in ["assistant", "user"]:
raise ValueError(f"Invalid role: {role}")
# Ensure content is a non-empty string
if not content or not isinstance(content, str):
raise ValueError("Content must be a non-empty string")
self.role = role
self.content = content
def to_dict(self) -> Dict[str, str]:
"""
Convert the message to a dictionary format for API communication.
Returns:
Dict[str, str]: A dictionary containing role and content
"""
return {"role": self.role, "content": self.content}
class SnowflakeChat:
"""
Handles all Snowflake-specific chat operations including message formatting
and communication with the Snowflake Cortex API.
"""
def __init__(self, session: Session):
"""
Initialize the Snowflake chat handler.
Args:
session (Session): An active Snowflake session for database operations
"""
self.session = session
# Model will be set dynamically based on user selection
self.model = None
# Default parameters for the model
self.default_params = {
"temperature": 0.7, # Controls response randomness (0.0-1.0)
"max_tokens": 4096, # Maximum length of generated response
}
def set_model(self, model: str):
"""
Set the model to use for chat completion.
Args:
model (str): The name of the model to use
"""
if model not in AVAILABLE_MODELS:
raise ValueError(f"Invalid model: {model}")
self.model = model
def format_messages(self, messages: List[Dict[str, str]]) -> List[Dict[str, str]]:
"""
Format a list of messages for the Snowflake Cortex API.
This method handles message validation and escaping special characters
to prevent SQL injection and ensure proper JSON formatting.
Args:
messages (List[Dict[str, str]]): Raw messages from the chat history
Returns:
List[Dict[str, str]]: Formatted messages ready for API submission
"""
# Skip the first message if it's just the initial greeting
messages_to_send = messages[1:] if len(messages) > 1 else messages
formatted_messages = []
for msg in messages_to_send:
try:
# Validate message format using ChatMessage class
chat_msg = ChatMessage(msg["role"], msg["content"])
# Escape newlines and quotes for proper JSON formatting
content = chat_msg.content.replace("\n", "\\n").replace('"', '\\"')
formatted_messages.append({"role": chat_msg.role, "content": content})
except ValueError as e:
# Log formatting errors but continue processing other messages
logger.error(f"Error formatting message: {e}")
continue
return formatted_messages
def query_cortex(self, messages: List[Dict[str, str]]) -> Optional[str]:
"""
Send a query to Snowflake Cortex and handle the response.
This method constructs and executes the SQL query to interact with the
Snowflake Cortex API, handling any errors that occur during the process.
Args:
messages (List[Dict[str, str]]): Formatted messages to send to Cortex
Returns:
Optional[str]: The model's response text, or None if an error occurs
Raises:
Exception: Various exceptions might be raised during API communication
"""
if not self.model:
raise ValueError("Model not set")
try:
# Prepare messages for SQL query, escaping single quotes
messages_json = json.dumps(messages).replace("'", "''")
# Construct the SQL query for the Cortex API
query = f"""
SELECT SNOWFLAKE.CORTEX.COMPLETE(
'{self.model}',
parse_json('{messages_json}'),
parse_json('{json.dumps(self.default_params)}')
)
"""
# Execute query and get results
result = self.session.sql(query).collect()
response_json = json.loads(result[0][0])
# Validate response contains expected data
if not response_json.get("choices"):
raise ValueError("No choices in response")
return response_json["choices"][0]["messages"]
except Exception as e:
# Log error details and re-raise for higher-level handling
logger.error(f"Error querying Cortex: {e}")
raise
class ChatInterface:
"""
Manages the Streamlit user interface and coordinates between the UI
and the Snowflake chat functionality.
"""
def __init__(self):
"""
Initialize the chat interface, setting up session state and
establishing Snowflake connection.
"""
self.initialize_session_state()
self.snowflake_session = self._get_snowflake_session()
if self.snowflake_session:
self.chat = SnowflakeChat(self.snowflake_session)
@staticmethod
def initialize_session_state():
"""
Initialize or reset the Streamlit session state for chat history
and model selection.
"""
if "messages" not in st.session_state:
st.session_state["messages"] = [
ChatMessage("assistant", "How can I help you?").to_dict()
]
if "selected_model" not in st.session_state:
st.session_state["selected_model"] = "claude-3-5-sonnet"
@staticmethod
def _get_snowflake_session() -> Optional[Session]:
"""
Establish a connection to Snowflake with error handling.
Returns:
Optional[Session]: Active Snowflake session or None if connection fails
"""
try:
return get_active_session()
except Exception as e:
logger.error(f"Failed to connect to Snowflake: {e}")
st.error(
"❌ Failed to connect to Snowflake. Please check your connection settings."
)
return None
def handle_model_change(self):
"""
Handle model selection changes and update the chat interface accordingly.
"""
selected_model = st.selectbox(
"Select Model",
options=AVAILABLE_MODELS,
index=AVAILABLE_MODELS.index(st.session_state.selected_model),
key="model_selector",
)
if selected_model != st.session_state.selected_model:
st.session_state.selected_model = selected_model
# Clear chat history when model changes
# st.session_state.messages = [
# ChatMessage("assistant", "How can I help you?").to_dict()
# ]
# st.re_run()
def handle_user_input(self):
"""
Process user input, send it to Snowflake Cortex, and display the response.
This method handles the main chat interaction loop, including:
- Capturing user input
- Updating chat history
- Processing messages
- Displaying responses
- Error handling
"""
# Set the current model
self.chat.set_model(st.session_state.selected_model)
if prompt := st.chat_input("Type your message here..."):
# Add user's message to chat history
st.session_state.messages.append(ChatMessage("user", prompt).to_dict())
st.chat_message("user").write(prompt)
with st.chat_message("assistant"):
with st.spinner("💭 Thinking..."):
try:
# Format messages for API
agentic_messages = self.chat.format_messages(
st.session_state.messages
)
# Add instruction for specific answers to the last message
if agentic_messages:
last_message = agentic_messages[-1]["content"]
agentic_messages[-1][
"content"
] = f"{last_message} - *Only Answer very specific to this question*"
# Get response from Snowflake Cortex
response_content = self.chat.query_cortex(agentic_messages)
# Update chat history with assistant's response
assistant_message = ChatMessage(
"assistant", response_content
).to_dict()
st.session_state.messages.append(assistant_message)
st.write(response_content)
except json.JSONDecodeError as e:
# Handle JSON parsing errors
logger.error(f"JSON parsing error: {e}")
st.error("🚫 Error processing the response. Please try again.")
except Exception as e:
# Handle all other errors
logger.error(f"Unexpected error: {e}")
st.error(
"⚠️ Something went wrong. Please try again or contact support."
)
# Offer debug information for troubleshooting
if st.checkbox("Show debug information"):
st.code(str(e))
def display_chat_history(self):
"""
Display the entire chat history in the Streamlit interface.
Each message is displayed with appropriate styling based on its role.
"""
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.write(msg["content"])
def main():
"""
Main application entry point.
Sets up the Streamlit page configuration, initializes the chat interface,
and manages the main application flow.
"""
# Configure Streamlit page settings
st.set_page_config(page_title="Snowflake Chat", page_icon="💬", layout="wide")
# Display application title
st.title("💬 Snowflake Completion Chatbot")
# Initialize chat interface
chat_interface = ChatInterface()
# Only proceed if Snowflake connection is successful
if chat_interface.snowflake_session:
# Add model selector before chat history
chat_interface.handle_model_change()
# Display chat interface
chat_interface.display_chat_history()
chat_interface.handle_user_input()
else:
st.warning("Please ensure you have a valid Snowflake connection to continue.")
if __name__ == "__main__":
main()