-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
212 lines (185 loc) · 7.99 KB
/
app.py
File metadata and controls
212 lines (185 loc) · 7.99 KB
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
import streamlit as st
import openai
import os
from dotenv import load_dotenv
import hashlib
import sqlite3
import json
import uuid
import re
load_dotenv()
SYSTEM_PROMPT = "You are the world's best data analyst, specializing exclusively in analyzing bicycle sales data for our company. You never make calculation errors. When appropriate, describe graphics or charts to visualize the data. Do not respond to any topics outside of sales data analysis. If the query is not about sales data, politely decline. You can query the sales database using the query_sales_db function. The sales table has columns: id (INTEGER), date (TEXT), product (TEXT), quantity (INTEGER), price (REAL), total (REAL)."
CONVERSATIONS_DIR = "conversations"
INVALID_FILENAME_CHARS = r'[<>:"/\\|?*]'
TOOLS = [
{
"type": "function",
"function": {
"name": "query_sales_db",
"description": "Execute a SQL query on the sales database and return the results.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The SQL query to execute on the sales table."
}
},
"required": ["query"]
}
}
}
]
def _sanitize_filename_component(component):
if not component:
return "unknown"
safe = re.sub(INVALID_FILENAME_CHARS, "_", str(component))
safe = safe.strip()
return safe or "unknown"
def _extract_serializable_messages(messages):
serializable = []
for msg in messages:
if msg.get("role") in {"user", "assistant"}:
cleaned = {
"role": msg.get("role", "assistant"),
"content": msg.get("content") or ""
}
serializable.append(cleaned)
return serializable
def save_conversation(messages=None):
if messages is None:
messages = st.session_state.get("messages", [])
serializable_messages = _extract_serializable_messages(messages)
if not serializable_messages:
return None
username = _sanitize_filename_component(st.session_state.get("username", "unknown"))
session_id = _sanitize_filename_component(st.session_state.get("session_id", "session"))
os.makedirs(CONVERSATIONS_DIR, exist_ok=True)
file_path = os.path.join(CONVERSATIONS_DIR, f"{username}_{session_id}_messages.json")
try:
with open(file_path, "w", encoding="utf-8") as f:
json.dump(serializable_messages, f, ensure_ascii=False, indent=2)
except Exception as exc:
st.warning(f"Could not save the conversation history: {exc}")
return None
return file_path
def init_db():
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password_hash TEXT)''')
# Insert default user if not exists
c.execute("INSERT OR IGNORE INTO users (username, password_hash) VALUES (?, ?)", ('admin', '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8'))
conn.commit()
conn.close()
def query_sales_db(query):
"""
Execute a SQL query on the sales.db database.
"""
try:
conn = sqlite3.connect('sales.db')
c = conn.cursor()
c.execute(query)
rows = c.fetchall()
conn.close()
return str(rows)
except Exception as e:
return f"Error: {str(e)}"
init_db()
# Session state for login
if 'logged_in' not in st.session_state:
st.session_state.logged_in = False
if not st.session_state.logged_in:
saved_transcript_path = st.session_state.pop("last_saved_transcript", None)
if saved_transcript_path:
st.success(f"Conversation saved to `{saved_transcript_path}`")
st.title("Login")
username = st.text_input("Username")
password = st.text_input("Password", type="password")
if st.button("Login"):
hashed_input = hashlib.sha256(password.encode()).hexdigest()
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute("SELECT * FROM users WHERE username = ? AND password_hash = ?", (username, hashed_input))
if c.fetchone():
st.session_state.username = username
st.session_state.session_id = uuid.uuid4().hex
st.session_state.messages = []
st.session_state.logged_in = True
st.rerun()
else:
st.error("Invalid credentials")
conn.close()
else:
st.title("Chatbot with OpenAI")
# Get API key
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
st.error("Please set the OPENAI_API_KEY environment variable.")
st.stop()
# Initialize OpenAI client
client = openai.OpenAI(api_key=api_key)
# Session state for messages
if 'messages' not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message['role']):
st.markdown(message['content'])
# User input
if prompt := st.chat_input("Type your message"):
# Add user message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Get response
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
try:
# Prepare messages with system prompt
messages_with_system = [{"role": "system", "content": SYSTEM_PROMPT}] + st.session_state.messages
response = client.chat.completions.create(
model="gpt-4o",
messages=messages_with_system,
tools=TOOLS,
tool_choice="auto"
)
# Handle tool calls
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
if tool_call.function.name == "query_sales_db":
query = eval(tool_call.function.arguments)["query"]
result = query_sales_db(query)
# Add tool response
st.session_state.messages.append({"role": "assistant", "content": "", "tool_calls": response.choices[0].message.tool_calls})
st.session_state.messages.append({"role": "tool", "content": result, "tool_call_id": tool_call.id})
# Get final response
final_response = client.chat.completions.create(
model="gpt-4o",
messages=messages_with_system + [{"role": "assistant", "content": "", "tool_calls": response.choices[0].message.tool_calls}, {"role": "tool", "content": result, "tool_call_id": tool_call.id}]
)
full_response = final_response.choices[0].message.content
else:
full_response = "Unknown tool called."
else:
full_response = response.choices[0].message.content
message_placeholder.markdown(full_response)
except Exception as e:
full_response = f"Error: {str(e)}"
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
save_conversation()
# Logout button
if st.button("Logout"):
saved_path = save_conversation()
if saved_path:
try:
display_path = os.path.relpath(saved_path)
except ValueError:
display_path = saved_path
st.session_state.last_saved_transcript = display_path.replace(os.sep, "/")
st.session_state.logged_in = False
st.session_state.pop('messages', None)
st.session_state.pop('session_id', None)
st.session_state.pop('username', None)
st.rerun()