Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limiter for OpenAI API call #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Savaw
Copy link

@Savaw Savaw commented Jan 17, 2023

Added a rate_limit function that can be used with OpenAI API calls to prevent "Rate Limit Reached" error.

@keerthanpg
Copy link
Owner

What is the default rate limit of open ai ? Wouldn't change make the app slower?

@Savaw
Copy link
Author

Savaw commented Jan 18, 2023

The default rate limit for free users is 20 rpm, as mentioned on the website. However, the error I faced yesterday said I have exceeded the limit of 60 rpm. (I was trying a paper with more than 60 rows in its dataframe.) It can make the app slower, but it is better than not working. The default min_interval_seconds can also be changed to change the speed according to rate limit.

Anyway, another option that I saw on the above link today and might be better would be using a retry mechanism, so the code performs normally at the beginning and slows down only if faced with an error:

from tenacity import retry, stop_after_attempt, wait_random_exponential
 
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def get_embedding_with_backoff(*args, **kwargs):
    return get_embedding(*args, **kwargs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants