Spaces:
Sleeping
Sleeping
Update multiagents.py
Browse files- multiagents.py +52 -20
multiagents.py
CHANGED
|
@@ -59,32 +59,64 @@ dotenv.load_dotenv()
|
|
| 59 |
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
grok_api_key = os.getenv("groq_api")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
provider="groq",
|
| 67 |
api_key=grok_api_key,
|
| 68 |
-
model_id
|
| 69 |
)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
# print('1', output_test)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# My_Agent = client.chat.completions.create(
|
| 78 |
-
# model="qewn/qwen3-32b",
|
| 79 |
-
# messages=[
|
| 80 |
-
# {
|
| 81 |
-
# "role": "user",
|
| 82 |
-
# "content": "How many 'G's in 'huggingface'?"
|
| 83 |
-
# }
|
| 84 |
-
# ],
|
| 85 |
-
# )
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
def check_final_answer(final_answer, agent_memory) -> bool:
|
| 90 |
"""
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
+
# Rate limiting decorator
|
| 63 |
+
def rate_limit(calls_per_minute=15):
|
| 64 |
+
"""Rate limiting decorator that limits function calls per minute"""
|
| 65 |
+
def decorator(func):
|
| 66 |
+
call_times = []
|
| 67 |
+
|
| 68 |
+
@wraps(func)
|
| 69 |
+
def wrapper(*args, **kwargs):
|
| 70 |
+
now = time.time()
|
| 71 |
+
# Remove calls older than 1 minute
|
| 72 |
+
call_times[:] = [t for t in call_times if now - t < 60]
|
| 73 |
+
|
| 74 |
+
if len(call_times) >= calls_per_minute:
|
| 75 |
+
sleep_time = 60 - (now - call_times[0])
|
| 76 |
+
if sleep_time > 0:
|
| 77 |
+
mylog("rate_limit", f"Rate limit reached. Sleeping for {sleep_time:.2f} seconds")
|
| 78 |
+
time.sleep(sleep_time)
|
| 79 |
+
# Clean up old calls again after sleeping
|
| 80 |
+
now = time.time()
|
| 81 |
+
call_times[:] = [t for t in call_times if now - t < 60]
|
| 82 |
+
|
| 83 |
+
call_times.append(now)
|
| 84 |
+
return func(*args, **kwargs)
|
| 85 |
+
return wrapper
|
| 86 |
+
return decorator
|
| 87 |
+
|
| 88 |
+
# Rate-limited model wrapper
|
| 89 |
+
class RateLimitedModel:
|
| 90 |
+
def __init__(self, model, calls_per_minute=30):
|
| 91 |
+
self.model = model
|
| 92 |
+
self.calls_per_minute = calls_per_minute
|
| 93 |
+
self.call_times = []
|
| 94 |
+
|
| 95 |
+
def __call__(self, messages, **kwargs):
|
| 96 |
+
return self._rate_limited_call(messages, **kwargs)
|
| 97 |
+
|
| 98 |
+
@rate_limit(calls_per_minute=30) # Adjust as needed
|
| 99 |
+
def _rate_limited_call(self, messages, **kwargs):
|
| 100 |
+
return self.model(messages, **kwargs)
|
| 101 |
+
|
| 102 |
+
def __getattr__(self, name):
|
| 103 |
+
# Delegate other attributes to the wrapped model
|
| 104 |
+
return getattr(self.model, name)
|
| 105 |
+
|
| 106 |
+
###################
|
| 107 |
+
|
| 108 |
grok_api_key = os.getenv("groq_api")
|
| 109 |
|
| 110 |
+
|
| 111 |
+
# Base model
|
| 112 |
+
base_model = InferenceClientModel(
|
| 113 |
provider="groq",
|
| 114 |
api_key=grok_api_key,
|
| 115 |
+
model_id="qwen/qwen3-32b"
|
| 116 |
)
|
| 117 |
|
| 118 |
+
# Wrap with rate limiting
|
| 119 |
+
My_Agent = RateLimitedModel(base_model, calls_per_minute=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
def check_final_answer(final_answer, agent_memory) -> bool:
|
| 122 |
"""
|