-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
main.py
180 lines (164 loc) · 7 KB
/
main.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
import arxiv
import argparse
import os
import sys
from dotenv import load_dotenv
load_dotenv(override=True)
os.environ["TOKENIZERS_PARALLELISM"] = "false"
from pyzotero import zotero
from recommender import rerank_paper
from construct_email import render_email, send_email
from tqdm import trange,tqdm
from loguru import logger
from gitignore_parser import parse_gitignore
from tempfile import mkstemp
from paper import ArxivPaper
from llm import set_global_llm
import feedparser
def get_zotero_corpus(id:str,key:str) -> list[dict]:
zot = zotero.Zotero(id, 'user', key)
collections = zot.everything(zot.collections())
collections = {c['key']:c for c in collections}
corpus = zot.everything(zot.items(itemType='conferencePaper || journalArticle || preprint'))
corpus = [c for c in corpus if c['data']['abstractNote'] != '']
def get_collection_path(col_key:str) -> str:
if p := collections[col_key]['data']['parentCollection']:
return get_collection_path(p) + ' / ' + collections[col_key]['data']['name']
else:
return collections[col_key]['data']['name']
for c in corpus:
paths = [get_collection_path(col) for col in c['data']['collections']]
c['paths'] = paths
return corpus
def filter_corpus(corpus:list[dict], pattern:str) -> list[dict]:
_,filename = mkstemp()
with open(filename,'w') as file:
file.write(pattern)
matcher = parse_gitignore(filename,base_dir='./')
new_corpus = []
for c in corpus:
match_results = [matcher(p) for p in c['paths']]
if not any(match_results):
new_corpus.append(c)
os.remove(filename)
return new_corpus
def get_arxiv_paper(query:str, debug:bool=False) -> list[ArxivPaper]:
client = arxiv.Client(num_retries=10,delay_seconds=10)
feed = feedparser.parse(f"https://rss.arxiv.org/atom/{query}")
if 'Feed error for query' in feed.feed.title:
raise Exception(f"Invalid ARXIV_QUERY: {query}.")
if not debug:
papers = []
all_paper_ids = [i.id.removeprefix("oai:arXiv.org:") for i in feed.entries if i.arxiv_announce_type == 'new']
bar = tqdm(total=len(all_paper_ids),desc="Retrieving Arxiv papers")
for i in range(0,len(all_paper_ids),50):
search = arxiv.Search(id_list=all_paper_ids[i:i+50])
batch = [ArxivPaper(p) for p in client.results(search)]
bar.update(len(batch))
papers.extend(batch)
bar.close()
else:
logger.debug("Retrieve 5 arxiv papers regardless of the date.")
search = arxiv.Search(query='cat:cs.AI', sort_by=arxiv.SortCriterion.SubmittedDate)
papers = []
for i in client.results(search):
papers.append(ArxivPaper(i))
if len(papers) == 5:
break
return papers
parser = argparse.ArgumentParser(description='Recommender system for academic papers')
def add_argument(*args, **kwargs):
def get_env(key:str,default=None):
# handle environment variables generated at Workflow runtime
# Unset environment variables are passed as '', we should treat them as None
v = os.environ.get(key)
if v == '' or v is None:
return default
return v
parser.add_argument(*args, **kwargs)
arg_full_name = kwargs.get('dest',args[-1][2:])
env_name = arg_full_name.upper()
env_value = get_env(env_name)
if env_value is not None:
#convert env_value to the specified type
if kwargs.get('type') == bool:
env_value = env_value.lower() in ['true','1']
else:
env_value = kwargs.get('type')(env_value)
parser.set_defaults(**{arg_full_name:env_value})
if __name__ == '__main__':
add_argument('--zotero_id', type=str, help='Zotero user ID')
add_argument('--zotero_key', type=str, help='Zotero API key')
add_argument('--zotero_ignore',type=str,help='Zotero collection to ignore, using gitignore-style pattern.')
add_argument('--send_empty', type=bool, help='If get no arxiv paper, send empty email',default=False)
add_argument('--max_paper_num', type=int, help='Maximum number of papers to recommend',default=100)
add_argument('--arxiv_query', type=str, help='Arxiv search query')
add_argument('--smtp_server', type=str, help='SMTP server')
add_argument('--smtp_port', type=int, help='SMTP port')
add_argument('--sender', type=str, help='Sender email address')
add_argument('--receiver', type=str, help='Receiver email address')
add_argument('--sender_password', type=str, help='Sender email password')
add_argument(
"--use_llm_api",
type=bool,
help="Use OpenAI API to generate TLDR",
default=False,
)
add_argument(
"--openai_api_key",
type=str,
help="OpenAI API key",
default=None,
)
add_argument(
"--openai_api_base",
type=str,
help="OpenAI API base URL",
default="https://api.openai.com/v1",
)
add_argument(
"--model_name",
type=str,
help="LLM Model Name",
default="gpt-4o",
)
parser.add_argument('--debug', action='store_true', help='Debug mode')
args = parser.parse_args()
assert (
not args.use_llm_api or args.openai_api_key is not None
) # If use_llm_api is True, openai_api_key must be provided
if args.debug:
logger.remove()
logger.add(sys.stdout, level="DEBUG")
logger.debug("Debug mode is on.")
else:
logger.remove()
logger.add(sys.stdout, level="INFO")
logger.info("Retrieving Zotero corpus...")
corpus = get_zotero_corpus(args.zotero_id, args.zotero_key)
logger.info(f"Retrieved {len(corpus)} papers from Zotero.")
if args.zotero_ignore:
logger.info(f"Ignoring papers in:\n {args.zotero_ignore}...")
corpus = filter_corpus(corpus, args.zotero_ignore)
logger.info(f"Remaining {len(corpus)} papers after filtering.")
logger.info("Retrieving Arxiv papers...")
papers = get_arxiv_paper(args.arxiv_query, args.debug)
if len(papers) == 0:
logger.info("No new papers found. Yesterday maybe a holiday and no one submit their work :). If this is not the case, please check the ARXIV_QUERY.")
if not args.send_empty:
exit(0)
else:
logger.info("Reranking papers...")
papers = rerank_paper(papers, corpus)
if args.max_paper_num != -1:
papers = papers[:args.max_paper_num]
if args.use_llm_api:
logger.info("Using OpenAI API as global LLM.")
set_global_llm(api_key=args.openai_api_key, base_url=args.openai_api_base, model=args.model_name)
else:
logger.info("Using Local LLM as global LLM.")
set_global_llm()
html = render_email(papers)
logger.info("Sending email...")
send_email(args.sender, args.receiver, args.sender_password, args.smtp_server, args.smtp_port, html)
logger.success("Email sent successfully! If you don't receive the email, please check the configuration and the junk box.")