petrov826 commited on
Commit
f0cfbf0
·
verified ·
1 Parent(s): 2dabae8

create rate_limiter.py

Browse files
Files changed (1) hide show
  1. rate_limiter.py +22 -0
rate_limiter.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from smolagents import CodeAgent, ActionStep
3
+
4
+ class RateLimiter:
5
+ """
6
+ APIの呼び出し回数を数えて、一定回数の呼び出しが行われたら一定秒数待機する
7
+ """
8
+ def __init__(self, calls_per_wait: int = 10, seconds_to_wait: int = 60):
9
+ self.calls_per_wait = calls_per_wait
10
+ self.seconds_to_wait = seconds_to_wait
11
+ self._call_count = 0
12
+
13
+ def increment_and_sleep_if_needed(self, memory_step: ActionStep, agent: CodeAgent):
14
+ """
15
+ 呼び出し回数をインクリメントし、一定回数の呼び出しが行われたら一定秒数待機する
16
+ """
17
+ self._call_count += 1
18
+
19
+ if self._call_count >= self.calls_per_wait:
20
+ print(f"呼び出し回数が{self.calls_per_wait}回を超えたので{self.seconds_to_wait}秒待機します")
21
+ time.sleep(self.seconds_to_wait)
22
+ self._call_count = 0