-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPaperDatabase.py
More file actions
329 lines (286 loc) · 12.7 KB
/
PaperDatabase.py
File metadata and controls
329 lines (286 loc) · 12.7 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
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
import json
from tqdm import tqdm
from Paper import Paper
from datetime import datetime, timedelta
import psycopg
from dotenv import load_dotenv
from pgvector.psycopg import register_vector
import os
from psycopg.types.json import Jsonb
from utils import get_logger
logger = get_logger()
class PaperDatabase:
def __init__(self):
load_dotenv()
self.ai_categories = ["cs:cs:AI", "cs:cs:CL", "cs:cs:LG", "cs:cs:MA"]
self.ai_categories_str = "(" + ",".join(f"'{category}'" for category in self.ai_categories) + ")"
self.con = None
self.date_format = "%Y-%m-%d"
def __enter__(self):
database_url = os.getenv("DATABASE_URL")
assert database_url is not None, "Database URL is not set"
self.con = psycopg.connect(database_url)
register_vector(self.con)
return self
def __exit__(self, exc_type, exc_value, traceback):
# NOTE: The commit only happens at the very end, and if there was an error, we will not commit.
if self.con is not None:
if exc_type is None:
self.con.commit()
else:
print(f"Error committing transaction:\n{exc_value}")
self.con.close()
self.con = None
def insert_paper(self, paper: Paper):
with self.con.cursor() as cur:
to_insert = [
paper.paper_id,
Jsonb(paper.document),
paper.abstract,
paper.title,
paper.source,
paper.paper_date.strftime(self.date_format),
paper.link]
if any(v is None for v in to_insert):
breakpoint()
# First, try to update an existing paper if the incoming record is newer
updated_rows = cur.execute(
"""
UPDATE paper
SET document = %s::jsonb,
abstract = %s,
title = %s,
source = %s,
update_date = %s,
link = %s
WHERE paper_id = %s::VARCHAR
AND update_date < %s::DATE
""",
[
Jsonb(paper.document),
paper.abstract,
paper.title,
paper.source,
paper.paper_date.strftime(self.date_format),
paper.link,
paper.paper_id,
paper.paper_date.strftime(self.date_format),
],
).rowcount
new_rows = 0
skipped_rows = 0
if updated_rows == 0:
# If no update happened, try to insert (noop if already exists with newer/same date)
new_rows = cur.execute(
"""
INSERT INTO paper (paper_id, document, abstract, title, source, update_date, link)
VALUES (%s, %s::jsonb, %s, %s, %s, %s, %s)
ON CONFLICT (paper_id) DO NOTHING;
""",
to_insert,
).rowcount
else:
# Paper content changed; reset embedding for this paper so it gets re-embedded
cur.execute(
"""
UPDATE embedding
SET embedding_gemini_embedding_001 = NULL
WHERE paper_id = %s::VARCHAR
""",
[paper.paper_id],
)
skipped_rows = 1 - (updated_rows + new_rows)
assert new_rows + updated_rows + skipped_rows == 1, f"Updated {updated_rows} rows, inserted {new_rows} rows, and skipped {skipped_rows} rows for paper {paper.paper_id}"
return updated_rows, new_rows, skipped_rows
def is_updated(self, paper: Paper):
with self.con.cursor() as cur:
return cur.execute("""
SELECT 1 FROM paper
WHERE paper_id = %s::VARCHAR AND update_date < %s::DATE
""", [paper.paper_id, paper.paper_date.strftime(self.date_format)]).fetchone() is not None
def is_new(self, paper: Paper):
with self.con.cursor() as cur:
return cur.execute("""
SELECT 1 FROM paper
WHERE paper_id = %s::VARCHAR
""", [paper.paper_id]).fetchone() is None
def get_newest_date(self):
with self.con.cursor() as cur:
return cur.execute("SELECT MAX(update_date) FROM paper").fetchone()[0]
def try_update_categories(self, paper: Paper):
with self.con.cursor() as cur:
stored_categories = cur.execute("""
SELECT category FROM arxiv_paper_categories
WHERE paper_id = %s::VARCHAR
""", [paper.paper_id]).fetchall()
stored_categories = {c[0] for c in stored_categories}
if stored_categories == paper.categories:
return False
# Delete the existing categories
cur.execute("""
DELETE FROM arxiv_paper_categories
WHERE paper_id = %s::VARCHAR
""", [paper.paper_id])
# Add the new categories
bulk_insertions = []
for category in paper.categories:
bulk_insertions.append((paper.paper_id, category))
cur.executemany("""
INSERT INTO arxiv_paper_categories (paper_id, category)
VALUES (%s, %s)
""", bulk_insertions)
return True
def get_unembedded_arxiv_ai_papers(self):
with self.con.cursor() as cur:
return cur.execute(f"""
SELECT DISTINCT ps.paper_id, ps.document
FROM paper AS ps
JOIN arxiv_paper_categories AS pc
ON ps.paper_id = pc.paper_id
LEFT JOIN embedding AS se
ON se.paper_id = ps.paper_id
WHERE se.embedding_gemini_embedding_001 IS NULL
AND pc.category IN {self.ai_categories_str}
""").fetchall()
def get_unembedded_conference_papers(self):
with self.con.cursor() as cur:
return cur.execute("""
SELECT DISTINCT ps.paper_id, ps.abstract
FROM paper AS ps
LEFT JOIN embedding AS emb
ON emb.paper_id = ps.paper_id
WHERE emb.embedding_gemini_embedding_001 IS NULL
AND ps.source != 'arxiv'
""").fetchall()
def update_embedding(self, paper_id: str, embedding: list[float]):
with self.con.cursor() as cur:
cur.execute(
"""
INSERT INTO embedding (paper_id, embedding_gemini_embedding_001)
VALUES (%s::VARCHAR, %s::halfvec(3072))
ON CONFLICT (paper_id) DO UPDATE
SET embedding_gemini_embedding_001 = EXCLUDED.embedding_gemini_embedding_001
""",
[paper_id, embedding],
)
def count_rows_to_update_and_insert(self, papers: list[Paper]):
total_updates = 0
total_new = 0
for paper in tqdm(papers, desc="Checking for paper updates and new papers", total=len(papers)):
total_updates += self.is_updated(paper)
total_new += self.is_new(paper)
return total_updates, total_new
def generate_weekly_digest(self, embedding: list[float], limit: int = 10):
last_day = datetime.now() - timedelta(days=7)
with self.con.cursor() as cur:
rows = cur.execute(f"""
SELECT ps.*, emb.embedding_gemini_embedding_001 <=> %s::halfvec(3072) AS similarity
FROM paper AS ps
LEFT JOIN embedding AS emb
ON emb.paper_id = ps.paper_id
WHERE ps.update_date > %s::DATE
ORDER BY similarity ASC
LIMIT %s
""", [embedding, last_day, limit]).fetchall()
return rows
def time_filtered_k_nearest(self, embedding: list[float], timedelta: timedelta | None, limit: int):
if timedelta is not None:
oldest_time = (datetime.now() - timedelta).strftime("%Y-%m-%d")
time_filter = f"WHERE update_date > '{oldest_time}'"
else:
time_filter = ""
with self.con.cursor() as cur:
return cur.execute(f"""
SELECT ps.document, emb.embedding_gemini_embedding_001 <=> %s::halfvec(3072) AS similarity
FROM paper AS ps
LEFT JOIN embedding AS emb
ON emb.paper_id = ps.paper_id
{time_filter}
ORDER BY similarity ASC
LIMIT %s::INTEGER
""", [embedding, limit]).fetchall()
def get_newest_conference_papers(self, embedding: list[float], timedelta: timedelta):
limit = 10
if timedelta is None:
timedelta = timedelta(days=365*50)
oldest_time = (datetime.now() - timedelta).strftime("%Y-%m-%d")
with self.con.cursor() as cur:
return cur.execute(f"""
SELECT ps.*, emb.embedding_gemini_embedding_001 <=> %s::halfvec(3072) AS similarity
FROM paper AS ps
JOIN embedding AS emb
ON emb.paper_id = ps.paper_id
WHERE ps.update_date > %s::DATE
AND ps.source != 'arxiv'
AND emb.embedding_gemini_embedding_001 IS NOT NULL
ORDER BY similarity ASC
LIMIT %s::INTEGER
""", [embedding, oldest_time, limit]).fetchall()
def get_newest_papers(self, embedding: list[float], timedelta: timedelta, filter_list: list[str], limit: int = 10):
if timedelta is None:
timedelta = timedelta(days=365*50)
oldest_time = (datetime.now() - timedelta).strftime("%Y-%m-%d")
# Combine multiple filters with OR (union of sources), wrapped to preserve precedence
filter_str = ""
if filter_list:
or_group = " OR ".join(f"({flt})" for flt in filter_list)
filter_str = f"AND ({or_group})\n"
with self.con.cursor() as cur:
return cur.execute(f"""
SELECT ps.*, emb.embedding_gemini_embedding_001 <=> %s::halfvec(3072) AS similarity
FROM paper AS ps
JOIN embedding AS emb
ON emb.paper_id = ps.paper_id
WHERE ps.update_date > %s::DATE
AND emb.embedding_gemini_embedding_001 IS NOT NULL
{filter_str}
ORDER BY similarity ASC
LIMIT %s::INTEGER
""", [embedding, oldest_time, limit]).fetchall()
# TODO: Add a way of finding the next conference date for each source
def summarise_current_conferences(self):
"""Print a summary of the conference papers that are currently in the
database. For every non-arxiv paper source (e.g. ICML, NeurIPS, …)
print the list of years for which we have papers.
Example output::
ICML : [2022, 2023, 2024]
NeurIPS : [2021, 2022, 2023]
"""
with self.con.cursor() as cur:
rows = cur.execute(
"""
SELECT source,
array_agg(DISTINCT EXTRACT(YEAR FROM update_date)::INT) AS years
FROM paper
WHERE source != 'arxiv'
GROUP BY source
ORDER BY source
"""
).fetchall()
# Sort the years inside each list for nicer presentation and print
for source, years in rows:
years_sorted = sorted(years)
print(f"{source:<10}: {years_sorted}")
def commit(self):
if self.con is not None:
self.con.commit()
def compute_similarity_over_time(self, embedding: list[float], similarity_threshold: float, filter_list: list[str]):
filter_str = ""
if filter_list:
or_group = " OR ".join(f"({flt})" for flt in filter_list)
filter_str = f"AND ({or_group})\n"
with self.con.cursor() as cur:
rows = cur.execute(f"""
SELECT ps.update_date, (emb.embedding_gemini_embedding_001 <=> %s::halfvec(3072)) < %s AS is_similar
FROM paper AS ps
JOIN embedding AS emb
ON emb.paper_id = ps.paper_id
WHERE emb.embedding_gemini_embedding_001 IS NOT NULL
{filter_str}
ORDER BY update_date ASC
""", [embedding, similarity_threshold]).fetchall()
return rows
if __name__ == "__main__":
with PaperDatabase() as db:
# papers = db.get_unembedded_conference_papers()
db.summarise_current_conferences()