Commit e92ced3
committed
fix: throttle Azure OpenAI embedding calls and back off on rate limits
The data loader previously fanned out all 4 contribution types
("Spoken", "Written", "Corrections", "Petitions") concurrently in a
TaskGroup, with each type spawning one task per page. With nothing
limiting concurrent embed_batch calls, the loader could trivially
exceed Azure's TPM quota (especially the default S0 tier at 350k TPM).
The retry decorator stop_after_attempt(3) had no wait, so it ignored
the Retry-After header.
Changes:
- Add an aiolimiter + semaphore around every embeddings.create call,
configurable via OPENAI_EMBED_RATE_PER_SECOND, OPENAI_EMBED_MAX_CONCURRENCY,
OPENAI_EMBED_BATCH_SIZE env vars. Defaults are tuned for the S0 tier.
- Switch tenacity to wait_random_exponential with jitter, retry only on
RateLimitError / APITimeoutError / APIConnectionError, up to 8 attempts.
- Pass max_retries=6 to AsyncAzureOpenAI so the SDK honours Retry-After
before tenacity takes over for longer waits.
- Reduce default batch_size from 100 to 32 (~10k tokens/call) to fit
the per-minute quota.
- Run load_all_contributions sequentially; pages within each type still
parallelise.1 parent 489cacb commit e92ced3
2 files changed
Lines changed: 70 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| 7 | + | |
| 8 | + | |
5 | 9 | | |
6 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
7 | 16 | | |
8 | 17 | | |
9 | 18 | | |
10 | 19 | | |
11 | 20 | | |
12 | 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 | + | |
13 | 47 | | |
14 | 48 | | |
15 | 49 | | |
16 | 50 | | |
17 | 51 | | |
18 | 52 | | |
19 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
20 | 57 | | |
21 | 58 | | |
22 | 59 | | |
| |||
27 | 64 | | |
28 | 65 | | |
29 | 66 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
35 | 73 | | |
36 | 74 | | |
37 | 75 | | |
38 | | - | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
39 | 82 | | |
40 | 83 | | |
41 | 84 | | |
42 | 85 | | |
43 | 86 | | |
44 | | - | |
| 87 | + | |
45 | 88 | | |
46 | 89 | | |
47 | 90 | | |
| |||
50 | 93 | | |
51 | 94 | | |
52 | 95 | | |
53 | | - | |
| 96 | + | |
54 | 97 | | |
55 | 98 | | |
56 | 99 | | |
| |||
59 | 102 | | |
60 | 103 | | |
61 | 104 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
67 | 111 | | |
68 | 112 | | |
69 | 113 | | |
70 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
71 | 119 | | |
72 | 120 | | |
73 | 121 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
237 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
238 | 242 | | |
239 | 243 | | |
240 | | - | |
241 | | - | |
242 | | - | |
| 244 | + | |
| 245 | + | |
243 | 246 | | |
244 | 247 | | |
245 | 248 | | |
| |||
0 commit comments