Jayant-Kernel commited on
fix: retry on rate limit in generate_distractors, 21s sleep for free-tier 3 RPM
Browse files- 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 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 126 |
-
time.sleep(
|
| 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)
|