-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
55 lines (44 loc) · 1.41 KB
/
database.py
File metadata and controls
55 lines (44 loc) · 1.41 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
import sqlite3
# Connect to the SQLite database (or create it if it doesn't exist)
def connect_db():
return sqlite3.connect('chatbot_history.db')
# Create a table to store chat history
def create_table():
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS chat_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_name TEXT,
chat_history TEXT
)
''')
conn.commit()
conn.close()
# Save chat history to the database
def save_chat_to_db(file_name, chat_history):
conn = connect_db()
cursor = conn.cursor()
# Convert chat history to a single string
chat_history_str = '\n'.join(
[f"User: {entry['content']}" if entry['role'] == 'user' else f"Bot: {entry['content']}" for entry in chat_history]
)
# Insert data into the database
cursor.execute('''
INSERT INTO chat_history (file_name, chat_history)
VALUES (?, ?)
''', (file_name, chat_history_str))
conn.commit()
conn.close()
# Retrieve chat history for a specific file
def get_chat_history(file_name):
conn = connect_db()
cursor = conn.cursor()
cursor.execute('''
SELECT chat_history FROM chat_history WHERE file_name = ?
''', (file_name,))
result = cursor.fetchone()
conn.close()
return result[0] if result else None
# Initialize the database
create_table()