File size: 20,817 Bytes
8e698eb
70e35a5
8e698eb
 
 
94dc00e
 
 
70e35a5
8e698eb
 
 
94dc00e
fb8c051
94dc00e
 
fb8c051
4ed4801
94dc00e
 
 
 
8e698eb
1457d21
94dc00e
 
1457d21
94dc00e
 
1457d21
94dc00e
1457d21
 
 
8ef9348
94dc00e
 
 
 
 
a7f1695
3a7ead9
a7f1695
94dc00e
 
 
 
 
 
 
 
 
 
 
 
 
1457d21
 
94dc00e
1457d21
 
 
94dc00e
1457d21
 
 
94dc00e
 
1457d21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94dc00e
1457d21
 
 
 
 
 
 
94dc00e
 
 
 
 
 
 
1457d21
94dc00e
1457d21
94dc00e
1457d21
 
 
 
 
 
 
 
 
 
 
 
 
 
94dc00e
1457d21
 
 
 
 
 
 
 
8ef9348
8e698eb
 
d18c569
70e35a5
64fee17
 
 
 
 
 
 
70e35a5
b730fc0
 
70e35a5
8e698eb
 
94dc00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed4801
 
 
94dc00e
 
4ed4801
 
 
 
94dc00e
 
 
4ed4801
 
 
 
 
 
94dc00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e698eb
 
 
 
 
 
 
 
70e35a5
8e698eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d18c569
94dc00e
4ed4801
 
94dc00e
 
 
 
 
 
 
 
 
 
 
 
d18c569
 
a7f1695
3a7ead9
a7f1695
3a7ead9
 
 
 
 
 
4ed4801
3a7ead9
 
 
 
 
 
 
 
 
8ef9348
 
 
295e94f
 
 
 
 
3a7ead9
 
 
 
 
a7f1695
 
c9efba3
295e94f
a7f1695
 
 
3a7ead9
 
8ef9348
70e35a5
 
 
3a7ead9
8e698eb
8ef9348
3a7ead9
 
 
 
 
 
70e35a5
8ef9348
 
3a7ead9
 
70e35a5
3a7ead9
 
70e35a5
3a7ead9
 
 
 
365213e
 
c9efba3
 
 
 
 
3a7ead9
 
 
8e698eb
3a7ead9
 
 
70e35a5
 
3a7ead9
8e698eb
 
8ef9348
3a7ead9
 
70e35a5
 
 
3a7ead9
 
 
 
 
d18c569
a7f1695
8ef9348
a7f1695
8ef9348
fb8c051
94dc00e
 
 
 
 
 
 
 
 
365213e
94dc00e
 
365213e
1457d21
70e35a5
94dc00e
70e35a5
 
 
 
 
94dc00e
fb8c051
2dc9347
 
fb8c051
 
 
 
2dc9347
 
 
 
 
 
70e35a5
94dc00e
fb8c051
 
 
d18c569
fb8c051
94dc00e
 
fb8c051
 
 
 
 
 
c9efba3
fb8c051
c9efba3
 
 
 
fb8c051
 
 
 
 
 
 
 
 
 
 
4e7254f
 
510bfff
fb8c051
 
d18c569
70e35a5
 
 
 
 
 
 
 
94dc00e
fb8c051
 
 
2dc9347
 
 
 
 
 
 
 
 
d18c569
2dc9347
1457d21
 
2dc9347
 
 
 
 
fb8c051
2dc9347
 
c42190b
 
 
 
 
 
2dc9347
 
a6aecff
 
94dc00e
70e35a5
 
 
 
 
4ed4801
 
 
 
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# Each `paper` is a dictionary containing:
#       (1) paper_id (2) title (3) authors (4) year (5) link (6) abstract (7) journal (8) embeddings
#
# Generate references:
#   `Reference` class:
#       1. Two methods to load papers:
#           1.1. Read a given string including paper titles separated by `,`
#           1.2. Read a .bib file
#       2. Given some keywords; use Semantic Scholar API to find papers.
#       3. Generate bibtex from the selected papers. --> to_bibtex()
#       4. Generate prompts from the selected papers: --> to_prompts()
#               A sample prompt: {"paper_id": "paper summary"}
#       5. Generate json from the selected papers. --> to_json()

import itertools
import json
import re
import time
import uuid
from typing import Dict, List, Optional, Union

import arxiv
import bibtexparser
import numpy as np
import requests
import tiktoken
from numpy.linalg import norm
from scholarly import ProxyGenerator
from scholarly import scholarly

# used to evaluate embeddings
URL = "https://model-apis.semanticscholar.org/specter/v1/invoke"
MAX_BATCH_SIZE = 16
MAX_ATTEMPTS = 20

# `tokenizer`: used to count how many tokens
tokenizer_name = tiktoken.encoding_for_model('gpt-4')
tokenizer = tiktoken.get_encoding(tokenizer_name.name)


######################################################################################################################
# Some basic tools
######################################################################################################################
def remove_special_characters(s):
    return ''.join(c for c in s if c.isalnum() or c.isspace() or c == ',')


def remove_newlines(serie):
    # This function is applied to the abstract of each paper to reduce the length of prompts.
    serie = serie.replace('\n', ' ')
    serie = serie.replace('\\n', ' ')
    serie = serie.replace('  ', ' ')
    serie = serie.replace('  ', ' ')
    return serie


def evaluate_cosine_similarity(v1, v2):
    try:
        return np.dot(v1, v2) / (norm(v1) * norm(v2))
    except ValueError:
        return 0.0


def chunks(lst, chunk_size=MAX_BATCH_SIZE):
    """Splits a longer list to respect batch size"""
    for i in range(0, len(lst), chunk_size):
        yield lst[i: i + chunk_size]


def embed(papers):
    embeddings_by_paper_id: Dict[str, List[float]] = {}
    for chunk in chunks(papers):
        # Allow Python requests to convert the data above to JSON
        response = requests.post(URL, json=chunk)

        if response.status_code != 200:
            raise RuntimeError("Sorry, something went wrong, please try later!")

        for paper in response.json()["preds"]:
            embeddings_by_paper_id[paper["paper_id"]] = paper["embedding"]

    return embeddings_by_paper_id


def get_embeddings(paper_title, paper_description):
    output = [{"title": paper_title, "abstract": paper_description, "paper_id": "target_paper"}]
    emb_vector = embed(output)["target_paper"]
    target_paper = output[0]
    target_paper["embeddings"] = emb_vector
    return target_paper


def get_embeddings_vector(paper_title, paper_description):
    output = [{"title": paper_title, "abstract": paper_description, "paper_id": "target_paper"}]
    emb_vector = embed(output)["target_paper"]
    return emb_vector


def get_top_k(papers_dict, paper_title, paper_description, k=None):
    # returns the top k papers most similar to the target paper
    target_paper = get_embeddings(paper_title, paper_description)
    papers = papers_dict  # must include embeddings

    # if k < len(papers_json), return k most relevant papers
    # if k >= len(papers_json) or k is None, return all papers
    max_num_papers = len(papers)
    if k is None:
        k = max_num_papers
    num_papers = min(k, max_num_papers)

    # evaluate the cosine similarity for each paper
    target_embedding_vector = target_paper["embeddings"]

    for k in papers:
        v = papers[k]
        embedding_vector = v["embeddings"]
        cos_sim = evaluate_cosine_similarity(embedding_vector, target_embedding_vector)
        papers[k]["cos_sim"] = cos_sim

    # return the best k papers
    sorted_papers = {k: v for k, v in sorted(papers.items(), key=lambda x: x[1]["cos_sim"], reverse=True)[:num_papers]}
    for key in sorted_papers:
        sorted_papers[key].pop("embeddings", None)
    return sorted_papers


def search_paper_abstract(title):
    pg = ProxyGenerator()
    success = pg.FreeProxies()  # pg.ScraperAPI("921b16f94d701308b9d9b4456ddde155")
    if success:
        try:
            scholarly.use_proxy(pg)
            # input the title of a paper, return its abstract
            search_query = scholarly.search_pubs(title)
            found_paper = next(search_query)
        except:
            return ""
    else:
        return ""
        # raise RuntimeError("ScraperAPI fails.")
    return remove_newlines(found_paper['bib']['abstract'])


def tiktoken_len(text):
    # evaluate how many tokens for the given text
    tokens = tokenizer.encode(text, disallowed_special=())
    return len(tokens)


######################################################################################################################
# Academic search tools
######################################################################################################################
def externalIds2link(externalIds):
    # Sample externalIds:
    #   "{'MAG': '2932819148', 'DBLP': 'conf/icml/HaarnojaZAL18', 'ArXiv': '1801.01290', 'CorpusId': 28202810}"
    if externalIds:
        # Supports ArXiv, MAG, ACL, PubMed, Medline, PubMedCentral, DBLP, DOI
        # priority: DBLP > arXiv > (todo: MAG > CorpusId > DOI > ACL > PubMed > Mdeline > PubMedCentral)
        # DBLP
        dblp_id = externalIds.get('DBLP')
        if dblp_id is not None:
            dblp_link = f"dblp.org/rec/{dblp_id}"
            return dblp_link
        # arXiv
        arxiv_id = externalIds.get('ArXiv')
        if arxiv_id is not None:
            arxiv_link = f"arxiv.org/abs/{arxiv_id}"
            return arxiv_link
        return ""
    else:
        # if this is an empty dictionary, return an empty string
        return ""


def search_paper_arxiv(title):
    search = arxiv.Search(
        query=title,
        max_results=1,
        sort_by=arxiv.SortCriterion.Relevance
    )
    try:
        #       (1) paper_id (2) title (3) authors (4) year (5) link (6) abstract (7) journal (8) embeddings
        result = next(search.results())
        title = result.title
        authors = " and ".join([author.name for author in result.authors])
        year = str(result.updated.now().year)
        link = result.pdf_url
        abstract = result.summary
        journal = f"Arxiv: {result.entry_id}"
        paper_id = result.authors[0].name.replace(" ", "")[:4] + year + title[:6].replace(" ", "")
        paper_id = paper_id.lower()

        paper = {"paper_id": paper_id,
                 "title": title,
                 "authors": authors,
                 "year": year,
                 "link": link,
                 "abstract": abstract,
                 "journal": journal}
    except StopIteration:
        paper = {}
    return paper


def search_paper_ss(title):
    if not title:
        return {}

    fields = ["title", "abstract", "venue", "year", "authors", "tldr", "externalIds"]
    limit = 1
    query = title.lower()
    query = query.replace(" ", "+")
    url = f'https://api.semanticscholar.org/graph/v1/paper/search?query={query}&limit={limit}&fields={",".join(fields)}'
    time.sleep(5)
    headers = {"Accept": "*/*"}
    response = requests.get(url, headers=headers, timeout=30)
    results = response.json()
    try:
        total = results['total']
        if total == 0:
            return {}
    except KeyError:
        return {}
    raw_paper = results['data'][0]
    if raw_paper['tldr'] is not None:
        abstract = raw_paper['tldr']['text']
    elif raw_paper['abstract'] is not None:
        abstract = remove_newlines(raw_paper['abstract'])
    else:
        abstract = ""

    authors = [author['name'] for author in raw_paper['authors']]
    authors_str = " and ".join(authors)
    year_str = str(raw_paper['year'])
    title = raw_paper['title']

    paper_id = authors_str.replace(" ", "")[:4] + year_str + title[:6].replace(" ", "")

    # some journal may contain &; replace it. e.g. journal={IEEE Power & Energy Society General Meeting}
    journal = remove_special_characters(raw_paper['venue'])
    if not journal:
        journal = "arXiv preprint"
    link = externalIds2link(raw_paper['externalIds'])
    paper = {
        "paper_id": paper_id,
        "title": title,
        "abstract": abstract,
        "link": link,
        "authors": authors_str,
        "year": year_str,
        "journal": journal
    }
    return paper


def search_paper_scrape(title):
    pg = ProxyGenerator()
    success = pg.ScraperAPI("921b16f94d701308b9d9b4456ddde155")
    if success:
        try:
            scholarly.use_proxy(pg)
            # input the title of a paper, return its abstract
            search_query = scholarly.search_pubs(title)
            found_paper = next(search_query)
            url = found_paper['pub_url']

            result = found_paper['bib']

            title = result['title']
            authors = " and ".join(result['author'])
            year = str(result['pub_year'])
            journal = result['pub_year']
            abstract = result['abstract']

            paper_id = authors.replace(" ", "")[:4] + year + title[:6].replace(" ", "")
            paper = {
                "paper_id": paper_id,
                "title": title,
                "abstract": abstract,
                "link": url,
                "authors": authors,
                "year": year,
                "journal": journal
            }
            return paper
        except StopIteration:
            return {}


def search_paper(title, verbose=True):
    if verbose:
        print(f"Searching {title}...")
    # try Semantic Scholar first
    paper = search_paper_ss(title)
    if not paper:
        paper = search_paper_arxiv(title)
    if not paper:
        paper = search_paper_scrape(title)
    if paper:
        paper["embeddings"] = get_embeddings_vector(paper_title=paper['title'], paper_description=paper['abstract'])
    if verbose:
        print(f"Search result: {paper}.")
    return paper


def load_papers_from_bibtex(bib_file_path):
    with open(bib_file_path) as bibtex_file:
        bib_database = bibtexparser.load(bibtex_file)
    if len(bib_database.entries) == 0:
        return []
    else:
        bib_papers = []
        for bibitem in bib_database.entries:
            # Add each paper to `bib_papers`
            paper_id = bibitem.get("ID")
            title = bibitem.get("title")
            if title is None:
                continue
            journal = bibitem.get("journal")
            year = bibitem.get("year")
            author = bibitem.get("author")
            abstract = bibitem.get("abstract")
            if abstract is None:
                abstract = search_paper_abstract(title)
            result = {
                "paper_id": paper_id,
                "title": title,
                "link": "",
                "abstract": abstract,
                "authors": author,
                "year": year,
                "journal": journal
            }
            bib_papers.append(result)
        return bib_papers


def load_papers_from_text(text):
    print(text)

    # split text by comma
    titles = [part.strip() for part in text.split(',')]
    titles = [remove_special_characters(title) for title in titles]
    papers = []
    if len(titles) > 0:
        for title in titles:
            paper = search_paper(title)
            if paper:
                papers.append(paper)
        return papers
    else:
        return []


######################################################################################################################
# Semantic Scholar (SS) API
######################################################################################################################
def ss_search(keywords, limit=20, fields=None):
    # space between the  query to be removed and replaced with +
    if fields is None:
        fields = ["title", "abstract", "venue", "year", "authors", "tldr", "embedding", "externalIds"]
    keywords = keywords.lower()
    keywords = keywords.replace(" ", "+")
    url = f'https://api.semanticscholar.org/graph/v1/paper/search?query={keywords}&limit={limit}&fields={",".join(fields)}'
    # headers = {"Accept": "*/*", "x-api-key": constants.S2_KEY}
    headers = {"Accept": "*/*"}

    response = requests.get(url, headers=headers, timeout=30)
    return response.json()


def _collect_papers_ss(keyword, counts=3, tldr=False):
    def extract_paper_id(last_name, year_str, title):
        pattern = r'^\w+'
        words = re.findall(pattern, title)
        # return last_name + year_str + title.split(' ', 1)[0]
        try:
            output = last_name + year_str + words[0]
        except IndexError:
            output = last_name + year_str + title[:4]
        return output

    def extract_author_info(raw_authors):
        authors = [author['name'] for author in raw_authors]

        authors_str = " and ".join(authors)
        try:
            last_name = authors[0].split()[-1]
            last_name = last_name.replace("'", "")
        except IndexError:
            last_name = "ma"
        # pattern = r'^\w+'
        # last_name = re.findall(pattern, authors[0])
        return authors_str, last_name

    def parse_search_results(search_results_ss):
        if len(search_results_ss) == 0:
            return []

        # turn the search result to a list of paper dictionary.
        papers_ss = []
        for raw_paper in search_results_ss:
            if raw_paper["abstract"] is None:
                continue

            authors_str, last_name = extract_author_info(raw_paper['authors'])
            year_str = str(raw_paper['year'])
            title = raw_paper['title']

            # some journal may contain &; replace it. e.g. journal={IEEE Power & Energy Society General Meeting}
            journal = raw_paper['venue'].replace("&", "\\&")
            if not journal:
                journal = "arXiv preprint"

            paper_id = extract_paper_id(last_name, year_str, title).lower()
            link = externalIds2link(raw_paper['externalIds'])

            if tldr and raw_paper['tldr'] is not None:
                abstract = raw_paper['tldr']['text']
            else:
                abstract = remove_newlines(raw_paper['abstract'])

            # some papers have no embeddings; handle this case
            embeddings_dict = raw_paper.get('embedding')
            if embeddings_dict is None:
                continue
            else:
                embeddings = raw_paper['embedding']['vector']
            result = {
                "paper_id": paper_id,
                "title": title,
                "abstract": abstract,
                "link": link,
                "authors": authors_str,
                "year": year_str,
                "journal": journal,
                "embeddings": embeddings
            }
            papers_ss.append(result)
        return papers_ss

    raw_results = ss_search(keyword, limit=counts)
    if raw_results is not None:
        search_results = raw_results.get("data")
        if search_results is None:
            search_results = []
    else:
        search_results = []
    results = parse_search_results(search_results)
    return results


######################################################################################################################
# References Class
######################################################################################################################

class References:
    def __init__(self,
                 title: str,
                 load_papers: Optional[str] = None,
                 load_bibtex: Optional[str] = None,
                 description: str = ""
                 ):
        self.papers = {}
        if load_bibtex is not None:
            self.papers["load_from_bibtex"] = load_papers_from_bibtex(load_bibtex)
        if load_papers is not None:
            self.papers["load_from_text"] = load_papers_from_text(load_papers)

        self.title = title
        self.description = description

    def generate_keywords_dict(self) -> Dict[str, int]:
        keywords_dict = {}
        for k in self.papers:
            keywords_dict[k] = len(self.papers[k])
        return keywords_dict

    def collect_papers(self, keywords_dict: Dict[str, int], tldr: bool = False) -> None:
        """
        Collect as many papers as possible

        keywords_dict:
            {"machine learning": 5, "language model": 2};
            the first is the keyword, the second is how many references are needed.
        """
        keywords = list(keywords_dict)
        comb_keywords = list(itertools.combinations(keywords, 2))
        for comb_keyword in comb_keywords:
            keywords.append(" ".join(comb_keyword))
        for key in keywords:
            self.papers[key] = _collect_papers_ss(key, 10, tldr)

    def to_bibtex(self, path_to_bibtex: str = "ref.bib") -> List[str]:
        """
        Turn the saved paper list into bibtex file "ref.bib". Return a list of all `paper_id`.
        """
        papers = self._get_papers(keyword="_all")

        num_papers = len(papers)
        print(f"{num_papers} papers will be added to `ref.bib`.")
        # clear the bibtex file
        with open(path_to_bibtex, "w", encoding="utf-8") as file:
            file.write("")

        bibtex_entries = []
        paper_ids = []
        seen = set()
        for paper in papers:
            if paper["paper_id"] in seen:
                continue
            else:
                seen.add(paper["paper_id"])
            bibtex_entry = f"""@article{{{paper["paper_id"]},
          title = {{{paper["title"]}}},
          author = {{{paper["authors"]}}}, 
          journal={{{paper["journal"]}}}, 
          year = {{{paper["year"]}}}, 
          url = {{{paper["link"]}}}
        }}"""
            bibtex_entries.append(bibtex_entry)
            paper_ids.append(paper["paper_id"])
            # Save the generated BibTeX entries to a file
            with open(path_to_bibtex, "a", encoding="utf-8") as file:
                file.write(bibtex_entry)
                file.write("\n\n")
                # print(f'{paper["paper_id"]} has been added to `ref.bib`.')
        return paper_ids

    def _get_papers(self, keyword="_all"):
        if keyword == "_all":
            papers = []
            for k, v in self.papers.items():
                papers = papers + v
        else:
            papers = self.papers["keyword"]
        return papers

    def to_prompts(self, keyword: str = "_all", max_tokens: int = 2048):
        # `prompts`:
        #   {"paper1_bibtex_id": "paper_1_abstract", "paper2_bibtex_id": "paper2_abstract"}
        #   this will be used to instruct GPT model to cite the correct bibtex entry.

        # two steps:
        #   1. Sort everything from most relevant to less relevant
        #   2. Add paper to prompts until max_tokens
        json_path = str(uuid.uuid1()) + ".json"
        papers_json = self.to_json()
        with open(json_path, "w") as f:
            json.dump(papers_json, f)
        try:
            # Use external API to obtain the most relevant papers
            title = self.title
            description = self.description
            result = get_top_k(papers_json, title, description)
            result = [item for key, item in result.items()]
        except Exception as e:
            print(f"Error occurs during calling external API: {e}\n")
            print("Use default method instead!")
            result = self._get_papers(keyword)
        prompts = {}
        tokens = 0
        for paper in result:
            abstract = paper.get("abstract")
            if abstract is not None and isinstance(abstract, str):
                prompts[paper["paper_id"]] = paper["abstract"]
                tokens += tiktoken_len(paper["abstract"])
            else:
                prompts[paper["paper_id"]] = " "
            if tokens >= max_tokens:
                break
        return prompts

    def to_json(self, keyword: str = "_all"):
        papers = self._get_papers(keyword)
        papers_json = {}
        for paper in papers:
            papers_json[paper["paper_id"]] = paper
        return papers_json

if __name__ == "__main__":
    ref = References("Play Atari", load_papers="Atari Game Using Reinforcement Lear4ning")
    ref.collect_papers({"Reinforcemetn Learning": 10}, tldr=True)