fix: correct type comparison in rate_limit_sleep causing excessive sleep on TPM rate limit#1174
Open
octo-patch wants to merge 5 commits into
Open
Conversation
This reverts commit 17923847f40863e977588d1a81bec2ae16e9801f.
|
Zvonimir Sabljic seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
90f59f5 to
a372904
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1121
Problem
When OpenAI returns a 429 rate limit error due to tokens-per-minute (TPM) exhaustion, GPT Pilot sleeps for an excessively long time (commonly 7200 or 9600 seconds) instead of the short wait time indicated in the API response (e.g. 11 seconds).
Root cause: In
core/llm/openai_client.py, therate_limit_sleepmethod reads thex-ratelimit-remaining-tokensresponse header as a string, then compares it to the integer0:Because
"0" == 0is alwaysFalse, the code never readsx-ratelimit-reset-tokens(which holds the short per-minute token reset time). It always readsx-ratelimit-reset-requestsinstead, which can be set to a much longer duration (e.g. 2 hours = 7200 s) when the requests-per-day limit is the binding constraint.Solution
Cast
remaining_tokenstointbefore the comparison:This ensures that when token usage is at the limit, the code correctly reads
x-ratelimit-reset-tokens(the short, per-minute reset time) rather thanx-ratelimit-reset-requests.Testing
Manually reviewed the logic. The fix is a one-character type cast that makes the comparison semantically correct and matches the documented OpenAI rate limit header behavior.