File size: 1,050 Bytes
d5c679f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import functools
import time

from groq import RateLimitError
from httpx import ReadTimeout
from loguru import logger


def rate_limit_bypass(sleep_time: int = 1, max_retries: int = 10):
    """Bypass rate limit for groq

    Parameters
    ----------
    sleep_time : int, optional, default 1
    max_retries : int, optional, default 10"""

    def decorate_rate_limit(func):
        @functools.wraps(func)
        def wrapper_rate_limit(*args, **kwargs):
            retries = 0
            while True:
                try:
                    result = func(*args, **kwargs)
                except (RateLimitError, ReadTimeout) as e:
                    logger.info(f"Rate limit exceeded, sleeping for {sleep_time} seconds")
                    logger.debug(repr(e))
                    time.sleep(sleep_time)
                    retries += 1
                    if retries > max_retries:
                        raise e
                    continue

                return result

        return wrapper_rate_limit

    return decorate_rate_limit