Metadata-Version: 2.3
Name: kani-ratelimits
Version: 1.0.0
Summary: A wrapper engine to enforce request-based, token-based, and concurrency-based limits on kani engines.
Project-URL: Homepage, https://github.com/zhudotexe/kani-ratelimits
Project-URL: Bug Tracker, https://github.com/zhudotexe/kani-ratelimits/issues
Author-email: Andrew Zhu <andrew@zhu.codes>
License: MIT License
        
        Copyright (c) 2023-present Andrew Zhu
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: aiolimiter<2.0.0,>=1.0.0
Requires-Dist: kani<2.0.0,>=1.0.0rc0
Description-Content-Type: text/markdown

# kani-ratelimits

This is a simple, small package to to enforce request-per-minute (RPM), token-per-minute (TPM), and/or max-concurrency
ratelimits before making requests to an underlying engine.

## Installation

```shell
pip install kani-ratelimits
```

## Usage

```python
from kani.ext.ratelimits import RatelimitedEngine

# limit requests to 10 req/min and 30k tokens/min
inner_engine = ...  # your engine here, e.g. `OpenAIEngine(..., model="gpt-4")`
engine = RatelimitedEngine(inner_engine, rpm_limit=10, tpm_limit=30_000)
```

The `RatelimitedEngine` takes the following parameters:

- `engine`: The engine to wrap.
- `max_concurrency` (int): The maximum number of concurrent requests to serve at once (default unlimited).
- `rpm_limit` (float): The maximum number of requests to serve per *rpm_period* (default unlimited).
- `rpm_period` (float): The duration, in seconds, of the time period in which to limit the rate. Note that up to
  *rpm_limit* requests are allowed within this time period in a burst (default 60s).
- `tpm_limit` (float): The maximum number of tokens to send in requests per *tpm_period* (default unlimited).
- `tpm_period` (float): The duration, in seconds, of the time period in which to limit the rate. Note that up to
  *tpm_limit* tokens are allowed within this time period in a burst (default 60s).

The ratelimiter will ensure that all conditions are met before forwarding the request to the wrapped engine.