JumaRubea commited on
Commit
7706084
·
verified ·
1 Parent(s): 8978ebe

Update src/chats.py

Browse files
Files changed (1) hide show
  1. src/chats.py +61 -0
src/chats.py CHANGED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import datetime
3
+
4
+ DB_FILE = "chats.db"
5
+
6
+ def init_db():
7
+ with sqlite3.connect(DB_FILE) as conn:
8
+ c = conn.cursor()
9
+ c.execute('''
10
+ CREATE TABLE IF NOT EXISTS chats (
11
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
12
+ title TEXT,
13
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
14
+ )
15
+ ''')
16
+ c.execute('''
17
+ CREATE TABLE IF NOT EXISTS messages (
18
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
19
+ chat_id INTEGER,
20
+ role TEXT,
21
+ content TEXT,
22
+ timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
23
+ FOREIGN KEY(chat_id) REFERENCES chats(id)
24
+ )
25
+ ''')
26
+ conn.commit()
27
+
28
+ def create_new_chat():
29
+ title = f"Chat {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
30
+ with sqlite3.connect(DB_FILE) as conn:
31
+ c = conn.cursor()
32
+ c.execute("INSERT INTO chats (title) VALUES (?)", (title,))
33
+ chat_id = c.lastrowid
34
+ conn.commit()
35
+ return chat_id
36
+
37
+ def get_all_chats():
38
+ with sqlite3.connect(DB_FILE) as conn:
39
+ c = conn.cursor()
40
+ c.execute("SELECT id, title FROM chats ORDER BY id DESC")
41
+ chats = c.fetchall()
42
+ return chats
43
+
44
+ def get_messages(chat_id):
45
+ with sqlite3.connect(DB_FILE) as conn:
46
+ c = conn.cursor()
47
+ c.execute("SELECT role, content FROM messages WHERE chat_id = ? ORDER BY id", (chat_id,))
48
+ messages = c.fetchall()
49
+ return messages
50
+
51
+ def save_message(chat_id, role, content):
52
+ with sqlite3.connect(DB_FILE) as conn:
53
+ c = conn.cursor()
54
+ c.execute("INSERT INTO messages (chat_id, role, content) VALUES (?, ?, ?)", (chat_id, role, content))
55
+ conn.commit()
56
+
57
+ def system_prompt() -> str:
58
+ return """ You are a friendly and helpful personal assistant for Juma.
59
+ Juma is curious and works on different kinds of digital projects.
60
+ He likes clear, simple answers and sometimes needs help with tasks, organizing things, or solving problems.
61
+ Keep the conversation casual, ask if you're not sure, and always try to be useful and respectful."""