-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * remove async * minor fix * robust timing test * modifies file names * modifies config list * updates faq * updates llm config doc * mock openai calls * make windows tests happy * clean up * undo change in doc --------- Co-authored-by: Chi Wang <[email protected]> Co-authored-by: Jack Gerrits <[email protected]>
- Loading branch information
1 parent
4dfe2fb
commit feef9d4
Showing
6 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import time | ||
from typing import Protocol | ||
|
||
|
||
class RateLimiter(Protocol): | ||
def sleep(self, *args, **kwargs): ... | ||
|
||
|
||
class TimeRateLimiter: | ||
"""A class to implement a time-based rate limiter. | ||
This rate limiter ensures that a certain operation does not exceed a specified frequency. | ||
It can be used to limit the rate of requests sent to a server or the rate of any repeated action. | ||
""" | ||
|
||
def __init__(self, rate: float): | ||
""" | ||
Args: | ||
rate (int): The frequency of the time-based rate limiter (NOT time interval). | ||
""" | ||
self._time_interval_seconds = 1.0 / rate | ||
self._last_time_called = 0.0 | ||
|
||
def sleep(self, *args, **kwargs): | ||
"""Synchronously waits until enough time has passed to allow the next operation. | ||
If the elapsed time since the last operation is less than the required time interval, | ||
this method will block the execution by sleeping for the remaining time. | ||
""" | ||
if self._elapsed_time() < self._time_interval_seconds: | ||
time.sleep(self._time_interval_seconds - self._elapsed_time()) | ||
|
||
self._last_time_called = time.perf_counter() | ||
|
||
def _elapsed_time(self): | ||
return time.perf_counter() - self._last_time_called |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import time | ||
|
||
import pytest | ||
|
||
from autogen.oai.rate_limiters import TimeRateLimiter | ||
|
||
|
||
@pytest.mark.parametrize("execute_n_times", range(5)) | ||
def test_time_rate_limiter(execute_n_times): | ||
current_time_seconds = time.time() | ||
|
||
rate = 1 | ||
rate_limiter = TimeRateLimiter(rate) | ||
|
||
n_loops = 2 | ||
for _ in range(n_loops): | ||
rate_limiter.sleep() | ||
|
||
total_time = time.time() - current_time_seconds | ||
min_expected_time = (n_loops - 1) / rate | ||
assert total_time >= min_expected_time |
This file contains 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
This file contains 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