Spaces:
Sleeping
Sleeping
Create src/utils.py
Browse files- src/utils.py +51 -0
src/utils.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from abc import ABC, abstractmethod
|
| 3 |
+
from collections import deque
|
| 4 |
+
from typing import Any
|
| 5 |
+
|
| 6 |
+
SECONDS_IN_MINUTE = 60
|
| 7 |
+
|
| 8 |
+
class BaseAgent(ABC):
|
| 9 |
+
@abstractmethod
|
| 10 |
+
def run(self, question: str, file_name: str = "", file_content: str = "") -> Any:
|
| 11 |
+
"""Override this method in your agent implementation"""
|
| 12 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 13 |
+
return "BaseAgent default answer"
|
| 14 |
+
|
| 15 |
+
class InputTokenRateLimiter:
|
| 16 |
+
"""Ensures we don’t exceed tokens per minute limits for LLMs"""
|
| 17 |
+
_instance = None
|
| 18 |
+
|
| 19 |
+
def __new__(cls):
|
| 20 |
+
if cls._instance is None:
|
| 21 |
+
cls._instance = super().__new__(cls)
|
| 22 |
+
return cls._instance
|
| 23 |
+
|
| 24 |
+
def __init__(self, max_tpm=50000):
|
| 25 |
+
self.max_tpm = max_tpm
|
| 26 |
+
if not hasattr(self, "_initialized"):
|
| 27 |
+
self.token_window = deque() # stores (timestamp, tokens_used)
|
| 28 |
+
self._initialized = True
|
| 29 |
+
|
| 30 |
+
def _update_queue(self, time_now):
|
| 31 |
+
while self.token_window and time_now - self.token_window[0][0] > SECONDS_IN_MINUTE:
|
| 32 |
+
self.token_window.popleft()
|
| 33 |
+
|
| 34 |
+
def tokens_used_last_minute(self):
|
| 35 |
+
now = time.time()
|
| 36 |
+
self._update_queue(now)
|
| 37 |
+
return sum(tokens for _, tokens in self.token_window)
|
| 38 |
+
|
| 39 |
+
def maybe_wait(self, tokens_expected_to_use):
|
| 40 |
+
ctr = 0
|
| 41 |
+
while self.tokens_used_last_minute() + tokens_expected_to_use > self.max_tpm:
|
| 42 |
+
if ctr % 10 == 0:
|
| 43 |
+
print("Sleeping 0.5s to respect token limits...")
|
| 44 |
+
time.sleep(0.5)
|
| 45 |
+
self._update_queue(time.time())
|
| 46 |
+
ctr += 1
|
| 47 |
+
|
| 48 |
+
def add_tokens(self, tokens):
|
| 49 |
+
now = time.time()
|
| 50 |
+
self.token_window.append((now, tokens))
|
| 51 |
+
self._update_queue(now)
|