|
1 | 1 | import sqlite3 |
| 2 | +import streamlit as st |
| 3 | +from utils import load_yaml_file |
2 | 4 |
|
3 | | -DB_FILE = "users.db" |
| 5 | +# Read config data |
| 6 | +config_data = load_yaml_file("config.yaml") |
| 7 | + |
| 8 | +DB_FILE = config_data["dbpath"] |
4 | 9 |
|
5 | 10 | # create a SQLite database to store chat history |
6 | 11 | # and user information |
7 | 12 | def init_db(): |
8 | 13 | with sqlite3.connect(DB_FILE) as conn: |
9 | 14 | conn.execute(''' |
10 | | - CREATE TABLE IF NOT EXISTS messages ( |
| 15 | +CREATE TABLE IF NOT EXISTS messages ( |
11 | 16 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
12 | 17 | username TEXT NOT NULL, |
13 | 18 | role TEXT NOT NULL, |
14 | 19 | content TEXT NOT NULL, |
15 | | - timestamp DATETIME DEFAULT CURRENT_TIMESTAMP |
| 20 | + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, |
| 21 | + feedback INTEGER DEFAULT NULL, |
| 22 | + session_id TEXT DEFAULT NULL, |
| 23 | + ip_address TEXT DEFAULT NULL |
| 24 | + ) |
| 25 | + ''') |
| 26 | + |
| 27 | + cursor = conn.execute("PRAGMA table_info(messages)") |
| 28 | + existing_columns = [col[1] for col in cursor.fetchall()] |
| 29 | + |
| 30 | + if "feedback" not in existing_columns: |
| 31 | + conn.execute('ALTER TABLE messages ADD COLUMN feedback INTEGER DEFAULT NULL') |
| 32 | + |
| 33 | + if "session_id" not in existing_columns: |
| 34 | + conn.execute('ALTER TABLE messages ADD COLUMN session_id TEXT DEFAULT NULL') |
| 35 | + |
| 36 | + if "ip_address" not in existing_columns: |
| 37 | + conn.execute('ALTER TABLE messages ADD COLUMN ip_address TEXT DEFAULT NULL') |
| 38 | + |
| 39 | + conn.execute(''' |
| 40 | + CREATE TABLE IF NOT EXISTS analytics ( |
| 41 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 42 | + title TEXT NOT NULL, |
| 43 | + sql_query TEXT NULL, |
| 44 | + options TEXT NULL, |
| 45 | + notes TEXT NULL |
16 | 46 | ) |
17 | 47 | ''') |
| 48 | + |
18 | 49 | conn.commit() |
19 | 50 |
|
20 | 51 | # create a table to store user information |
21 | | -def save_message(username, role, content): |
| 52 | +def save_message(username, role, content, session_id=None, ipaddress=None): |
22 | 53 | with sqlite3.connect(DB_FILE) as conn: |
23 | | - conn.execute( |
24 | | - 'INSERT INTO messages (username, role, content) VALUES (?, ?, ?)', |
25 | | - (username, role, content) |
| 54 | + cursor = conn.execute( |
| 55 | + 'INSERT INTO messages (username, role, content, session_id, ip_address) VALUES (?, ?, ?, ?, ?)', |
| 56 | + (username, role, content, session_id, ipaddress) |
26 | 57 | ) |
27 | 58 | conn.commit() |
| 59 | + return cursor.lastrowid |
| 60 | + |
28 | 61 |
|
29 | 62 | # retrieve chat history for a specific user |
30 | 63 | # and return it as a list of dictionaries |
31 | 64 | def get_messages(username): |
32 | 65 | with sqlite3.connect(DB_FILE) as conn: |
33 | 66 | cursor = conn.execute( |
34 | | - 'SELECT role, content FROM messages WHERE username = ? ORDER BY timestamp ASC', |
| 67 | + 'SELECT id, role, content, feedback FROM messages WHERE username = ? ORDER BY timestamp ASC', |
35 | 68 | (username,) |
36 | 69 | ) |
37 | | - return [{"role": row[0], "content": row[1]} for row in cursor.fetchall()] |
| 70 | + return [{"id": row[0], "role": row[1], "content": row[2], "feedback": row[3]} for row in cursor.fetchall()] |
| 71 | + |
| 72 | +def update_feedback(message_id, feedback): |
| 73 | + with sqlite3.connect(DB_FILE) as conn: |
| 74 | + conn.execute( |
| 75 | + 'UPDATE messages SET feedback = ? WHERE id = ?', |
| 76 | + (feedback, message_id) |
| 77 | + ) |
| 78 | + conn.commit() |
| 79 | + |
| 80 | +def on_feedback_change(message_id, fb_key): |
| 81 | + feedback = st.session_state[fb_key] |
| 82 | + update_feedback(message_id, feedback) |
| 83 | + |
| 84 | +def get_feedback(message_id): |
| 85 | + with sqlite3.connect(DB_FILE) as conn: |
| 86 | + cursor = conn.execute( |
| 87 | + 'SELECT feedback FROM messages WHERE id = ?', |
| 88 | + (message_id,) |
| 89 | + ) |
| 90 | + row = cursor.fetchone() |
| 91 | + return row[0] if row else None |
| 92 | + |
| 93 | + |
0 commit comments