-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessagedb.py
303 lines (268 loc) · 12.4 KB
/
messagedb.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
import sqlite3
from datetime import datetime
class MessageDB:
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name)
self.cursor = self.conn.cursor()
self.create_tables()
def create_tables(self):
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS phone_numbers (
phone_id INTEGER PRIMARY KEY,
phone_number TEXT
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS messages (
message_id INTEGER PRIMARY KEY,
message_sid TEXT,
from_phone_id INTEGER,
to_phone_id INTEGER,
body TEXT,
timestamp TEXT,
FOREIGN KEY (from_phone_id) REFERENCES phone_numbers (phone_id),
FOREIGN KEY (to_phone_id) REFERENCES phone_numbers (phone_id)
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS system_prompts (
system_prompt_id INTEGER PRIMARY KEY,
system_prompt TEXT
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS models (
model_id INTEGER PRIMARY KEY,
model_name TEXT
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS settings (
phone_id INTEGER PRIMARY KEY,
system_prompt_id INTEGER,
model_id INTEGER,
stop_sequence TEXT,
max_tokens INTEGER,
temperature FLOAT,
top_p FLOAT,
frequency_penalty FLOAT,
presence_penalty FLOAT,
FOREIGN KEY (phone_id) REFERENCES phone_numbers (phone_id),
FOREIGN KEY (system_prompt_id) REFERENCES system_prompts (system_prompt_id),
FOREIGN KEY (model_id) REFERENCES models (model_id)
)
''')
self.conn.commit()
#### GETTING IDs FROM VALUES####
def get_phone_id(self, phone_number):
self.cursor.execute(
'SELECT phone_id FROM phone_numbers WHERE phone_number = ?', (phone_number,))
if result := self.cursor.fetchone():
return result[0]
else:
print(f"[DB]: Phone number '{phone_number}' not found.")
return None
def get_system_prompt_id(self, system_prompt):
self.cursor.execute(
'SELECT system_prompt_id FROM system_prompts WHERE system_prompt = ?', (system_prompt,))
if result := self.cursor.fetchone():
return result[0]
else:
print(f"[DB]: System prompt '{system_prompt}' not found.")
return None
def get_model_id(self, model_name):
self.cursor.execute(
'SELECT model_id FROM models WHERE model_name = ?', (model_name,))
if result := self.cursor.fetchone():
return result[0]
else:
print(f"[DB]: Model '{model_name}' not found.")
return None
#### ADDING TO DB ####
def add_phone_number(self, phone_number):
if (phone_id := self.get_phone_id(phone_number)) is not None:
print(
f"[DB]: Phone number '{phone_number}' already exists in the database.")
return phone_id
self.cursor.execute(
'INSERT INTO phone_numbers (phone_number) VALUES (?)', (phone_number,))
self.conn.commit()
phone_id = self.cursor.lastrowid
print(
f"[DB]: Phone number '{phone_number}' added successfully. Phone ID: {phone_id}")
return phone_id
def add_message(self, message_sid, from_phone_number, to_phone_number, body):
from_phone_id = self.add_phone_number(from_phone_number)
to_phone_id = self.add_phone_number(to_phone_number)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.cursor.execute('''
INSERT INTO messages (message_sid, from_phone_id, to_phone_id, body, timestamp)
VALUES (?, ?, ?, ?, ?)
''', (message_sid, from_phone_id, to_phone_id, body, timestamp))
self.conn.commit()
message_id = self.cursor.lastrowid
print(f"[DB]: Message added successfully. Message ID: {message_id}")
return Message(message_id, message_id, from_phone_number, to_phone_number, body, timestamp)
def add_system_prompt(self, system_prompt):
if (system_prompt_id := self.get_system_prompt_id(system_prompt)) is not None:
print(
f"[DB]: System prompt '{system_prompt}' already exists in the database.")
return system_prompt_id
self.cursor.execute(
'INSERT INTO system_prompts (system_prompt) VALUES (?)', (system_prompt,))
self.conn.commit()
system_prompt_id = self.cursor.lastrowid
print(
f"[DB]: System prompt '{system_prompt}' added successfully. System Prompt ID: {system_prompt_id}")
return system_prompt_id
def add_model(self, model_name):
if (model_id := self.get_model_id(model_name)) is not None:
print(
f"[DB]: Model '{model_name}' already exists in the database.")
return model_id
self.cursor.execute(
'INSERT INTO models (model_name) VALUES (?)', (model_name,))
self.conn.commit()
model_id = self.cursor.lastrowid
print(
f"[DB]: Model '{model_name}' added successfully. Model ID: {model_id}")
return model_id
def _handle_settings_kwargs(self, **kwargs):
if system_prompt := kwargs.pop('system_prompt', None):
system_prompt_id = self.add_system_prompt(system_prompt)
kwargs['system_prompt_id'] = system_prompt_id
if model := kwargs.pop('model', None):
model_id = self.add_model(model)
kwargs['model_id'] = model_id
return kwargs
def add_settings(self, phone_number, **kwargs):
if (phone_id := self.get_phone_id(phone_number)) is not None:
kwargs = self._handle_settings_kwargs(**kwargs)
values = tuple(kwargs.values())
set_keys = ', '.join(kwargs.keys())
sql_question_marks = ', '.join(
['?' for _ in range(len(values) + 1)])
query = f'INSERT INTO settings (phone_id, {set_keys}) VALUES ({sql_question_marks})'
self.cursor.execute(query, (phone_id, *values))
self.conn.commit()
settings_id = self.cursor.lastrowid
print(
f"[DB]: Settings for phone number '{phone_number}' added successfully.")
return settings_id
else:
print(f"[DB]: Phone number '{phone_number}' not found.")
#### UPDATING DB ####
def update_settings_for_phone_number(self, phone_number, **kwargs):
if (phone_id := self.get_phone_id(phone_number)) is not None:
kwargs = self._handle_settings_kwargs(**kwargs)
values = tuple(kwargs.values())
set_values = ', '.join([f'{key} = ?' for key in kwargs])
query = f'UPDATE settings SET {set_values} WHERE phone_id = ?'
self.cursor.execute(query, (*values, phone_id))
self.conn.commit()
print(
f"[DB]: Settings for phone number '{phone_number}' updated successfully.")
else:
print(f"[DB]: Phone number '{phone_number}' not found.")
#### GETTING FROM DB ####
def get_phone_number(self, phone_id):
self.cursor.execute(
'SELECT phone_number FROM phone_numbers WHERE phone_id = ?', (phone_id,))
if result := self.cursor.fetchone():
return result[0]
else:
print(f"[DB]: Phone number with ID '{phone_id}' not found.")
return None
def get_messages_for_phone_number(self, phone_number):
if (phone_id := self.get_phone_id(phone_number)) is not None:
self.cursor.execute(
'SELECT * FROM messages WHERE from_phone_id = ? OR to_phone_id = ?', (phone_id, phone_id))
results = self.cursor.fetchall()
messages = []
for row in results:
message_id, message_sid, from_phone_id, to_phone_id, body, timestamp = row
from_phone_number = self.get_phone_number(from_phone_id)
to_phone_number = self.get_phone_number(to_phone_id)
# timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
message = Message(
message_id, message_sid, from_phone_number, to_phone_number, body, timestamp)
messages.append(message)
return messages
else:
print(f"[DB]: Phone number '{phone_number}' not found.")
return []
def get_system_prompt(self, system_prompt_id):
self.cursor.execute(
'SELECT system_prompt FROM system_prompts WHERE system_prompt_id = ?', (system_prompt_id,))
if result := self.cursor.fetchone():
return result[0]
else:
print(
f"[DB]: System prompt with ID '{system_prompt_id}' not found.")
return None
def get_model(self, model_id):
self.cursor.execute(
'SELECT model_name FROM models WHERE model_id = ?', (model_id,))
if result := self.cursor.fetchone():
return result[0]
else:
print(f"[DB]: Model with ID '{model_id}' not found.")
return None
def get_settings_for_phone_number(self, phone_number):
if (phone_id := self.get_phone_id(phone_number)) is not None:
self.cursor.execute(
'SELECT * FROM settings WHERE phone_id = ?', (phone_id,))
if result := self.cursor.fetchone():
phone_id, system_prompt_id, model_id, stop_sequence, max_tokens, temperature, top_p, frequency_penalty, presence_penalty = result
system_prompt = self.get_system_prompt(system_prompt_id)
model = self.get_model(model_id)
# return Settings(system_prompt, stop_sequence, max_tokens, temperature, top_p, frequency_penalty, presence_penalty)
return {
'model': model,
'system_prompt': system_prompt,
'stop_sequence': stop_sequence,
'max_tokens': max_tokens,
'temperature': temperature,
'top_p': top_p,
'frequency_penalty': frequency_penalty,
'presence_penalty': presence_penalty
}
else:
print(
f"[DB]: Settings for phone number '{phone_number}' not found.")
return None
else:
print(f"[DB]: Phone number '{phone_number}' not found.")
return None
def delete_messages_for_phone_number(self, phone_number):
if (phone_id := self.get_phone_id(phone_number)) is not None:
self.cursor.execute(
'DELETE FROM messages WHERE from_phone_id = ? OR to_phone_id = ?', (phone_id, phone_id))
self.conn.commit()
print(
f"[DB]: Messages for phone number '{phone_number}' removed successfully.")
else:
print(f"[DB]: Phone number '{phone_number}' not found.")
def close(self):
self.conn.close()
class Message:
def __init__(self, message_id, message_sid, from_phone_number, to_phone_number, body, timestamp):
self.message_id = message_id
self.message_sid = message_sid
self.from_phone_number = from_phone_number
self.to_phone_number = to_phone_number
self.body = body
self.timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
def __repr__(self):
return f"Message(from_phone_number={self.from_phone_number}\nto_phone_number={self.to_phone_number}\nbody={self.body}\ntimestamp={self.timestamp}))"
class Settings:
def __init__(self, system_prompt, stop_sequence, max_tokens, temperature, top_p, frequency_penalty, presence_penalty):
self.system_prompt = system_prompt
self.stop_sequence = stop_sequence
self.max_tokens = max_tokens
self.temperature = temperature
self.top_p = top_p
self.frequency_penalty = frequency_penalty
self.presence_penalty = presence_penalty
def __repr__(self):
return f'{type(self)}(' + ','.join(f'{key}={value}' for key, value in self.__dict__.items()) + ')'