Model Rotator
Model Rotator is a Python library for managing multiple LLM (Large Language Model) instances with rate limits and priorities. It dynamically schedules requests to models based on their rate limits, usage, and priority levels, ensuring optimal utilization of available resources.
Features
- Rate-Limit Management: Automatically tracks and enforces rate limits per model.
- Priority-Based Scheduling: Prioritizes high-priority models over medium and low-priority ones.
- Dynamic Updates: Tracks model usage in real-time and prunes stale usage data.
- Stateful: Maintains state for each model's usage across calls.
- Customizable: Easily configure models with different rate limits and priorities.
Installation
Install the package from PyPI:
pip install model-rotator
Usage
- Define Your Models Provide a list of model configurations:
Note: Models listed first will have higher priority if models have same priority given.
from model_rotator import ModelRotator
models = [
{"name": "groq/llama-3.1-70b-versatile", "priority": "high", "limit": 30},
{"name": "groq/llama-3.1-70b-specdec", "priority": "high", "limit": 30},
{"name": "groq/llama-3.1-8b-instant", "priority": "medium", "limit": 30},
{"name": "groq/llama-3.2-1b-preview", "priority": "low", "limit": 30},
{"name": "gemini/gemini-1.5-flash", "priority": "medium", "limit": 30},
{"name": "gemini/gemini-1.5-pro", "priority": "high", "limit": 15},
{"name": "gemini/gemini-exp-1114", "priority": "high", "limit": 2},
]
- Initialize the Scheduler
rotator = ModelRotator(models)
- Schedule Requests Use get_next_model() to get the next available model for processing:
for _ in range(50): # Simulate 50 requests
model = rotator.get_next_model()
if model:
print(f"Using model: {model}")
else:
print("All models exhausted, retry later.")
- Check Model States Inspect the current state of all models:
print(rotator.get_state())
Example Output
Copy code
Request 1: Using model: groq/llama-3.1-70b-versatile
Request 2: Using model: groq/llama-3.1-70b-specdec
...
Request 50: All models exhausted, retry later.
Model States:
[
{"name": "groq/llama-3.1-70b-versatile", "priority": "high", "limit": 30, "current_usage": 30},
{"name": "groq/llama-3.1-70b-specdec", "priority": "high", "limit": 30, "current_usage": 30},
...
]
API
ModelRotator(models:Model)
Initializes the scheduler.
models: A list of dictionaries. Each dictionary must include:name(str): The model name.priority(str): Priority level (high,medium,low).limit(int): Maximum allowed requests per minute.
get_next_model()
Returns the name of the next available model based on priority and rate limits.
- Returns:
str: The model name, orNoneif no models are available.
get_state()
Returns the current state of all models, including their usage.
- Returns:
list: A list of dictionaries with the following fields:name(str): Model name.priority(str): Priority level.limit(int): Rate limit.current_usage(int): Current number of requests within the last minute.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
Metadata
Release files for model-rotator 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| model_rotator-0.1.3.tar.gz | 3.7 kB | Without provenance | Details for model_rotator-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| model_rotator-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for model_rotator-0.1.3-py3-none-any.whl |
Total release size: 8.2 kB
Release files / model_rotator-0.1.3.tar.gz
| Download URL | model_rotator-0.1.3.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cb53a82c7c00c93970586373deea028d6d785d577af183a164590f2039920c3f
|
|
BLAKE2b-256 checksum How to use checksums |
a610d3c55b67ae2d30bb1c45249bc92f6bc0c8007a264e5cc2f0985f26bd0581
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.12.2 Linux/6.8.0-49-generic
|
Release files / model_rotator-0.1.3-py3-none-any.whl
| Download URL | model_rotator-0.1.3-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b612b71777197d1bb0bf3004b4f95d455a2feb28f80fe8b1064cee3acbad8a37
|
|
BLAKE2b-256 checksum How to use checksums |
91851488faa1bd3568b9c667dce2bb320f6f8e5322bea952f1ea6826b0ae28a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.12.2 Linux/6.8.0-49-generic
|
Security for model-rotator 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.