-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
340 lines (298 loc) · 13 KB
/
database.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import psycopg2
from ollama import Client
import json
import os
from dotenv import load_dotenv
from psycopg2.extras import RealDictCursor
load_dotenv()
class LargeEmbeddingFunction():
def __init__(self, model_name: str, embedding_link: str) -> None:
# This model supports two prompts: "s2p_query" and "s2s_query" for sentence-to-passage and sentence-to-sentence tasks, respectively.
self.model_name = model_name
self.client = Client(embedding_link)
def __call__(self, input):
response = self.client.embed(model=self.model_name, input=input)
embedding = response["embeddings"][0]
return json.dumps(embedding)
class RAG:
def __init__(self, postgres_link, postgres_port, embedding_link, db_user, db_password, db_name) -> None:
self.embedding_model = LargeEmbeddingFunction("cowolff/science_bge_large", embedding_link)
# self.embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="dunzhang/stella_en_400M_v5")
self.conn = psycopg2.connect(
user=db_user,
password=db_password,
host=postgres_link,
port=postgres_port, # The port you exposed in docker-compose.yml
database=db_name
)
def vectorize(self, document):
embedding = self.embedding_model(document)
return embedding
def check_id_exists(self, urls):
# Checks whether the urls are already in the papers table and returns a boolean list
cur = self.conn.cursor()
cur.execute("SELECT link FROM papers WHERE link = ANY(%s);", (urls,))
# cur.execute("SELECT link FROM papers")
results = cur.fetchall()
results = [result[0] for result in results]
return results
def check_summary_exists(self, paper_id):
cur = self.conn.cursor()
cur.execute("SELECT id, summary FROM summary WHERE paper_id = %s;", (paper_id,))
results = cur.fetchall()
if len(results) == 0:
return False, ""
else:
return True, results[0][1]
def add_documents(self, urls, titles, contents):
vectors = [self.vectorize(content) for content in contents]
cur = self.conn.cursor()
for url, title, content, vector in zip(urls, titles, contents, vectors):
cur.execute("INSERT INTO papers (link, title, content, embedding) VALUES (%s, %s, %s, %s)", (url, title, content, vector))
self.conn.commit()
cur.close()
def get_document(self, paper_id):
cur = self.conn.cursor()
cur.execute("SELECT id, title, content FROM papers WHERE id = %s;", (paper_id,))
id, title, content = cur.fetchone()
cur.close()
return id, title, content
def query(self, prompt, limit=4, updated_at=None):
cur = self.conn.cursor()
query_embedding = self.vectorize(prompt)
if updated_at:
cur.execute(
"""SELECT id, title, link, 1 - (embedding <=> %s) AS cosine_similarity
FROM papers
WHERE created_at > %s
ORDER BY cosine_similarity DESC
LIMIT %s;""",
(query_embedding, updated_at, limit)
)
else:
cur.execute(
"""SELECT id, title, link, 1 - (embedding <=> %s) AS cosine_similarity
FROM papers
ORDER BY cosine_similarity DESC
LIMIT %s;""",
(query_embedding, limit)
)
papers = []
for row in cur.fetchall():
try:
paper_data = {
"id": row[0],
"title": row[1],
"link": row[2]
}
papers.append(paper_data)
except Exception as e:
print(e)
cur.close()
return papers
class UserDatabase:
def __init__(self):
# self.db_host = os.environ.get("DB_HOST")
self.db_host = os.environ.get("DB_HOST")
self.db_name = os.environ.get("DB_NAME")
self.db_user = os.environ.get("POSTGRES_USER")
self.db_password = os.environ.get("POSTGRES_PASSWORD")
self.db_port = 5432
def connect(self):
"""Establish a connection to the PostgreSQL database."""
self.conn = psycopg2.connect(
host=self.db_host,
database=self.db_name,
user=self.db_user,
password=self.db_password,
port=self.db_port
)
self.conn.autocommit = True
def close(self):
"""Close the connection to the PostgreSQL database."""
if self.conn:
self.conn.close()
def extract_data(self):
"""Extract data and format it according to the specified dictionary format."""
self.connect()
cursor = self.conn.cursor(cursor_factory=RealDictCursor)
query = """
SELECT u.email AS name, u.id AS id, u.telegram_id, p.prompt, u.updated_at
FROM users u
JOIN prompts p ON u.id = p.user_id;
"""
cursor.execute(query)
users_data = {}
for row in cursor.fetchall():
user_key = row['id']
if user_key not in users_data:
users_data[user_key] = {
'telegram_id': row['telegram_id'],
'prompt': [],
'name': row['name'],
'updated_at': row['updated_at']
}
users_data[user_key]['prompt'].append(row['prompt'])
data_list = [{"id": key, "telegram_id": data['telegram_id'], "prompt": data['prompt'], "updated_at": data['updated_at']} for key, data in users_data.items()]
self.close()
return {"data": data_list}
def user_exists(self, telegram_id):
"""Check if a user exists in the database."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT COUNT(*) FROM users WHERE telegram_id = %s;", (telegram_id,))
count = cursor.fetchone()[0]
self.close()
return count > 0
def insert_user(self, chat_id, telegram_id):
"""Insert a new user into the database."""
self.connect()
try:
cursor = self.conn.cursor()
cursor.execute("INSERT INTO users (chat_id, telegram_id) VALUES (%s, %s);", (chat_id, telegram_id))
self.conn.commit()
finally:
self.close()
def insert_prompt(self, telegram_id, prompt, active=True):
"""Insert a new prompt into the database."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
cursor.execute("INSERT INTO prompts (user_id, prompt, active) VALUES (%s, %s, %s);", (user_id, prompt, active))
self.conn.commit()
cursor.close()
# return prompt_id
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM prompts WHERE user_id = %s AND prompt = %s;", (user_id, prompt))
prompt_id = cursor.fetchone()[0]
self.close()
return prompt_id
def delete_user(self, telegram_id):
"""Delete a user from the database."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
cursor.execute("DELETE FROM prompts WHERE user_id = %s;", (user_id,))
cursor.execute("DELETE FROM users WHERE telegram_id = %s;", (telegram_id,))
self.close()
def get_prompts(self, telegram_id, active=True):
"""Get all prompts for a user."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
if active:
cursor.execute("SELECT id, prompt FROM prompts WHERE user_id = %s AND active = TRUE;", (user_id,))
else:
cursor.execute("SELECT id, prompt FROM prompts WHERE user_id = %s;", (user_id,))
result = cursor.fetchall()
prompts = [row[1] for row in result]
ids = [row[0] for row in result]
self.close()
return prompts, ids
def prompt_exists(self, telegram_id, prompt):
"""Check if a prompt exists for a user."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
cursor.execute("SELECT id FROM prompts WHERE user_id = %s AND prompt = %s;", (user_id, prompt))
results = cursor.fetchall()
if len(results) == 0:
self.close()
return False, -1, user_id
else:
self.close()
return True, results[0][0], user_id
def set_prompt_active(self, user_id, prompt_id):
"""Set the prompt to active."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("UPDATE prompts SET active = TRUE WHERE user_id = %s AND id = %s;", (user_id, prompt_id))
self.close()
def delete_prompt(self, telegram_id, prompt):
"""Delete a prompt for a user."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
cursor.execute("DELETE FROM prompts WHERE user_id = %s AND prompt = %s;", (user_id, prompt))
self.close()
def set_prompt_inactive(self, telegram_id, prompt_id):
"""Set the prompt to inactive."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
cursor.execute("UPDATE prompts SET active = FALSE WHERE user_id = %s AND id = %s;", (user_id, prompt_id))
self.close()
def get_user_id(self, telegram_id):
"""Get the user ID based on telegram ID."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM users WHERE telegram_id = %s;", (telegram_id,))
user_id = cursor.fetchone()[0]
self.close()
return user_id
def get_chat_id(self, user_id):
"""Get the chat ID based on user ID."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT chat_id FROM users WHERE id = %s;", (user_id,))
chat_id = cursor.fetchone()[0]
self.close()
return chat_id
def record_message_sent(self, user_id, prompt_id):
"""Record the number of messages sent by a user."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("INSERT INTO messages_sent (user_id, prompt_id) VALUES (%s, %s);", (user_id, prompt_id))
cursor.close()
# Get message id of the last sent message to the user
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM messages_sent WHERE user_id = %s ORDER BY id DESC LIMIT 1;", (user_id,))
message_id = cursor.fetchone()[0]
self.close()
return message_id
def get_papers_from_last_message(self, user_id):
"""Get papers from the last message sent to the user."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("SELECT id FROM messages_sent WHERE user_id = %s ORDER BY id DESC LIMIT 1;", (user_id,))
message_id = cursor.fetchone()[0]
cursor.execute("SELECT paper_id FROM paper_in_message WHERE message_id = %s;", (message_id,))
paper_ids = [row[0] for row in cursor.fetchall()]
papers = []
print(paper_ids)
for paper_id in paper_ids:
cursor.execute("SELECT title, link FROM papers WHERE id = %s;", (paper_id,))
title, link = cursor.fetchone()
papers.append({"id": paper_id, "title": title, "link": link})
self.close()
return papers
def add_paper_to_message(self, message_id, paper_id):
"""Add a paper to a message."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("INSERT INTO paper_in_message (message_id, paper_id) VALUES (%s, %s);", (message_id, paper_id))
self.close()
def record_num_users(self, num_users):
"""Record the total number of users."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("INSERT INTO number_users (num_users) VALUES (%s);", (num_users,))
self.close()
def store_preview(self, user_id, prompt):
"""Store a preview of the prompt."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("INSERT INTO preview_papers (user_id, prompt) VALUES (%s, %s);", (user_id, prompt))
self.close()
def update_user(self, user_id, updated_at):
"""Update the user’s last updated timestamp."""
self.connect()
cursor = self.conn.cursor()
cursor.execute("UPDATE users SET updated_at = %s WHERE id = %s;", (updated_at, user_id))
self.close()