richardmfan commited on
Commit
1912e89
·
unverified ·
2 Parent(s): ca3e69d 486f1fd

Merge pull request #16 from AutoLLM/no-email-in-config

Browse files
Files changed (4) hide show
  1. advanced_usage.md +4 -4
  2. config.yaml +0 -11
  3. src/action.py +19 -19
  4. src/utils.py +7 -1
advanced_usage.md CHANGED
@@ -61,8 +61,8 @@ An alternative way to get started using this repository is to:
61
  - `MAIL_CONNECTION` (see above)
62
  - `MAIL_PASSWORD` (only if you don't have `MAIL_CONNECTION` set)
63
  - `MAIL_USERNAME` (only if you don't have `MAIL_CONNECTION` set)
64
- - `FROM_EMAIL` (only if you don't have it set in `config.yaml`)
65
- - `TO_EMAIL` (only if you don't have it set in `config.yaml`)
66
  6. Manually trigger the action or wait until the scheduled action takes place.
67
 
68
  ### Running as a github action without emails
@@ -84,8 +84,8 @@ If you do not wish to fork this repository, and would prefer to clone and run it
84
  5. Set the following secrets as environment variables:
85
  - `OPENAI_API_KEY`
86
  - `SENDGRID_API_KEY` (only if using SendGrid)
87
- - `FROM_EMAIL` (only if using SendGrid and if you don't have it set in `config.yaml`. Note that this value must match the email you used to create the SendGrid Api Key.)
88
- - `TO_EMAIL` (only if using SendGrid and if you don't have it set in `config.yaml`)
89
  6. Run `python action.py`.
90
  7. If you are not using SendGrid, the html of the digest will be written to `digest.html`. You can then use your favorite webbrowser to view it.
91
 
 
61
  - `MAIL_CONNECTION` (see above)
62
  - `MAIL_PASSWORD` (only if you don't have `MAIL_CONNECTION` set)
63
  - `MAIL_USERNAME` (only if you don't have `MAIL_CONNECTION` set)
64
+ - `FROM_EMAIL`
65
+ - `TO_EMAIL`
66
  6. Manually trigger the action or wait until the scheduled action takes place.
67
 
68
  ### Running as a github action without emails
 
84
  5. Set the following secrets as environment variables:
85
  - `OPENAI_API_KEY`
86
  - `SENDGRID_API_KEY` (only if using SendGrid)
87
+ - `FROM_EMAIL` (only if using SendGrid. Note that this value must match the email you used to create the SendGrid Api Key.)
88
+ - `TO_EMAIL` (only if using SendGrid)
89
  6. Run `python action.py`.
90
  7. If you are not using SendGrid, the html of the digest will be written to `digest.html`. You can then use your favorite webbrowser to view it.
91
 
config.yaml CHANGED
@@ -5,17 +5,6 @@ topic: "Computer Science"
5
  # Including more categories will result in more calls to the large language model
6
  categories: ["Artificial Intelligence", "Computation and Language"]
7
 
8
- # The email address that the digest will be sent from. must be the address matching
9
- # your sendgrid api key.
10
- # Leaving this empty will cause the script to use the
11
- # FROM_EMAIL environment variable instead
12
- from_email: ""
13
-
14
- # The email address you are going to send the digest to
15
- # Leaving this empty will cause the script to use the
16
- # TO_EMAIL environment variable instead
17
- to_email: ""
18
-
19
  # Relevance score threshold. abstracts that receive a score less than this from the large language model
20
  # will have their papers filtered out.
21
  #
 
5
  # Including more categories will result in more calls to the large language model
6
  categories: ["Artificial Intelligence", "Computation and Language"]
7
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Relevance score threshold. abstracts that receive a score less than this from the large language model
9
  # will have their papers filtered out.
10
  #
src/action.py CHANGED
@@ -116,27 +116,27 @@ if __name__ == "__main__":
116
 
117
  topic = config["topic"]
118
  categories = config["categories"]
119
- from_email = config.get("from_email") or os.environ.get("FROM_EMAIL")
120
- to_email = config.get("to_email") or os.environ.get("TO_EMAIL")
121
  threshold = config["threshold"]
122
  interest = config["interest"]
 
123
  with open("digest.html", "w") as f:
124
- body = generate_body(topic, categories, interest, threshold)
125
  f.write(body)
126
- if os.environ.get('SENDGRID_API_KEY', None):
127
- sg = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
128
- from_email = Email(from_email) # Change to your verified sender
129
- to_email = To(to_email)
130
- subject = date.today().strftime("Personalized arXiv Digest, %d %b %Y")
131
- content = Content("text/html", body)
132
- mail = Mail(from_email, to_email, subject, content)
133
- mail_json = mail.get()
134
-
135
- # Send an HTTP POST request to /mail/send
136
- response = sg.client.mail.send.post(request_body=mail_json)
137
- if response.status_code >= 200 and response.status_code <= 300:
138
- print("Send test email: Success!")
139
- else:
140
- print("Send test email: Failure ({response.status_code}, {response.text})")
141
  else:
142
- print("No sendgrid api key found. Skipping email")
 
 
 
116
 
117
  topic = config["topic"]
118
  categories = config["categories"]
119
+ from_email = os.environ.get("FROM_EMAIL")
120
+ to_email = os.environ.get("TO_EMAIL")
121
  threshold = config["threshold"]
122
  interest = config["interest"]
123
+ body = generate_body(topic, categories, interest, threshold)
124
  with open("digest.html", "w") as f:
 
125
  f.write(body)
126
+ if os.environ.get('SENDGRID_API_KEY', None):
127
+ sg = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
128
+ from_email = Email(from_email) # Change to your verified sender
129
+ to_email = To(to_email)
130
+ subject = date.today().strftime("Personalized arXiv Digest, %d %b %Y")
131
+ content = Content("text/html", body)
132
+ mail = Mail(from_email, to_email, subject, content)
133
+ mail_json = mail.get()
134
+
135
+ # Send an HTTP POST request to /mail/send
136
+ response = sg.client.mail.send.post(request_body=mail_json)
137
+ if response.status_code >= 200 and response.status_code <= 300:
138
+ print("Send test email: Success!")
 
 
139
  else:
140
+ print("Send test email: Failure ({response.status_code}, {response.text})")
141
+ else:
142
+ print("No sendgrid api key found. Skipping email")
src/utils.py CHANGED
@@ -96,6 +96,8 @@ def openai_completion(
96
  ):
97
  batch_decoding_args = copy.deepcopy(decoding_args) # cloning the decoding_args
98
 
 
 
99
  while True:
100
  try:
101
  shared_kwargs = dict(
@@ -115,7 +117,7 @@ def openai_completion(
115
  completion_batch = openai.Completion.create(prompt=prompt_batch, **shared_kwargs)
116
 
117
  choices = completion_batch.choices
118
-
119
  for choice in choices:
120
  choice["total_tokens"] = completion_batch.usage.total_tokens
121
  completions.extend(choices)
@@ -125,7 +127,11 @@ def openai_completion(
125
  if "Please reduce your prompt" in str(e):
126
  batch_decoding_args.max_tokens = int(batch_decoding_args.max_tokens * 0.8)
127
  logging.warning(f"Reducing target length to {batch_decoding_args.max_tokens}, Retrying...")
 
 
 
128
  else:
 
129
  logging.warning("Hit request rate limit; retrying...")
130
  time.sleep(sleep_time) # Annoying rate limit on requests.
131
 
 
96
  ):
97
  batch_decoding_args = copy.deepcopy(decoding_args) # cloning the decoding_args
98
 
99
+ backoff = 3
100
+
101
  while True:
102
  try:
103
  shared_kwargs = dict(
 
117
  completion_batch = openai.Completion.create(prompt=prompt_batch, **shared_kwargs)
118
 
119
  choices = completion_batch.choices
120
+
121
  for choice in choices:
122
  choice["total_tokens"] = completion_batch.usage.total_tokens
123
  completions.extend(choices)
 
127
  if "Please reduce your prompt" in str(e):
128
  batch_decoding_args.max_tokens = int(batch_decoding_args.max_tokens * 0.8)
129
  logging.warning(f"Reducing target length to {batch_decoding_args.max_tokens}, Retrying...")
130
+ elif not backoff:
131
+ logging.error("Hit too many failures, exiting")
132
+ raise e
133
  else:
134
+ backoff -= 1
135
  logging.warning("Hit request rate limit; retrying...")
136
  time.sleep(sleep_time) # Annoying rate limit on requests.
137