Jayant-Kernel commited on
Commit
26d82a2
·
unverified ·
1 Parent(s): f8a8477

fix: retry on rate limit in generate_distractors, 21s sleep for free-tier 3 RPM

Browse files
Files changed (1) hide show
  1. scripts/generate_distractors.py +18 -11
scripts/generate_distractors.py CHANGED
@@ -97,15 +97,22 @@ def main() -> None:
97
  if row["id"] in existing:
98
  continue
99
 
100
- try:
101
- distractors = _generate_distractors(client, row["question"], row["ground_truth"])
102
- except openai.AuthenticationError as e:
103
- raise RuntimeError(f"Unrecoverable API error (check OPENAI_API_KEY): {e}") from e
104
- except openai.RateLimitError as e:
105
- raise RuntimeError(f"Unrecoverable rate limit error: {e}") from e
106
- except Exception as e:
107
- print(f" ERROR on {row['id']}: {e} — skipping")
108
- time.sleep(1)
 
 
 
 
 
 
 
109
  continue
110
 
111
  output_rows.append({
@@ -122,8 +129,8 @@ def main() -> None:
122
  _save_rows(output_rows, LEVEL2_PATH)
123
  print(f" Progress: {iteration_count} seen / {new_count} new / {len(output_rows)} total saved")
124
 
125
- # Rate-limit sleep after successful API call
126
- time.sleep(1)
127
 
128
  # Final save
129
  _save_rows(output_rows, LEVEL2_PATH)
 
97
  if row["id"] in existing:
98
  continue
99
 
100
+ distractors = None
101
+ for attempt in range(3):
102
+ try:
103
+ distractors = _generate_distractors(client, row["question"], row["ground_truth"])
104
+ break
105
+ except openai.AuthenticationError as e:
106
+ raise RuntimeError(f"Unrecoverable API error (check OPENAI_API_KEY): {e}") from e
107
+ except openai.RateLimitError as e:
108
+ wait = 25
109
+ print(f" Rate limit on {row['id']} (attempt {attempt + 1}/3), waiting {wait}s...")
110
+ time.sleep(wait)
111
+ except Exception as e:
112
+ print(f" ERROR on {row['id']}: {e} — skipping")
113
+ break
114
+
115
+ if distractors is None:
116
  continue
117
 
118
  output_rows.append({
 
129
  _save_rows(output_rows, LEVEL2_PATH)
130
  print(f" Progress: {iteration_count} seen / {new_count} new / {len(output_rows)} total saved")
131
 
132
+ # Sleep between calls 21s keeps us under 3 RPM limit
133
+ time.sleep(21)
134
 
135
  # Final save
136
  _save_rows(output_rows, LEVEL2_PATH)