Skip to main content

OpenAI Load Balancer

Description

The OpenAI Load Balancer is a Python library designed to distribute API requests across multiple endpoints (supports both OpenAI and Azure). It implements a round-robin mechanism for load balancing and includes exponential backoff for retrying failed requests.

Supported OpenAI functions: ChatCompletion, Embedding, Completion (will be deprecated soon)

Features

  • Round Robin Load Balancing: Distributes requests evenly across a set of API endpoints.
  • Exponential Backoff: Includes retry logic with exponential backoff for each API call.
  • Failure Detection: Temporarily removes failed endpoints based on configurable thresholds.
  • Flexible Configuration: Customizable settings for endpoints, failure thresholds, cooldown periods, and more.
  • Easy Integration: Designed to be easily integrated into projects that use OpenAI's API.
  • Fallback: If OpenAI's endpoint goes down, if your Azure endpoint is still up, then your service stays up, and vice versa.

Installation

To install the OpenAI Load Balancer, run the following command:

pip install openai-load-balancer

Usage

First, setup your OpenAI API Endpoints. The API keys and base_url will be read from your env variable.

# Example configuration

ENDPOINTS = [
    {
        "api_type": "azure",
        "base_url": "AZURE_API_BASE_URL_1",
        "api_key_env": "AZURE_API_KEY_1",
        "version": "2023-05-15"
    },
    {
        "api_type": "open_ai",
        "base_url": "https://api.openai.com/v1",
        "api_key_env": "OPENAI_API_KEY_1",
        "version": None
    }
    # Add more configurations as needed
]

If you are using both Azure and OpenAI, specify the mapping of the OpenAI's model name to your Azure's engine name

MODEL_ENGINE_MAPPING = {
    "gpt-4": "gpt4",
    "gpt-3.5-turbo": "gpt-35-turbo",
    "text-embedding-ada-002": "text-embedding-ada-002"
    # Add more mappings as needed
}

Import and initialize the load balancer with the endpoints and mapping:

from openai_load_balancer import initialize_load_balancer

openai_load_balancer = initialize_load_balancer(
    endpoints=ENDPOINTS, model_engine_mapping=MODEL_ENGINE_MAPPING)

Making API Calls

Simply replace openai with openai_load_balancer in your function calls!:

response = openai_load_balancer.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello! This is a request."}
    ],
    # Additional parameters...
)

Additional configurations

You can also configure the load balancer with the following variables

# The number of consecutive failures of a request to an endpoint before the endpoint is temporarily marked as inactive
FAILURE_THRESHOLD = 5
# The minimum amount of time an endpoint is marked as inactive before it is reset to active.
COOLDOWN_PERIOD = timedelta(minutes=10)
# Whether or not to enable load balancing. If disabled, the first active endpoint will always be used, and other endpoints will only be used in case the first one fails.
LOAD_BALANCING_ENABLED = True

Contributing

Contributions to the OpenAI Load Balancer are welcome!

License

This project is licensed under the MIT License.

Metadata

Release files for openai-load-balancer 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-load-balancer 0.1.1
File Size Uploaded
openai-load-balancer-0.1.1.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-load-balancer 0.1.1
File Interpreter ABI Platform
openai_load_balancer-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / openai-load-balancer-0.1.1.tar.gz

Download URL openai-load-balancer-0.1.1.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7175818bcd50581b35e1aaac9321efbd27a810c4a0bda0083f8484fdbe3d06da
BLAKE2b-256 checksum
How to use checksums
b89868b98a05545fd4128a3b56f36915a3c4ea2eb2e518a707c68ca6cc4f9af4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release files / openai_load_balancer-0.1.1-py3-none-any.whl

Download URL openai_load_balancer-0.1.1-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5bf29699f63cc84852df866208e406587cec9857ee8aab41a8055a20af71ad74
BLAKE2b-256 checksum
How to use checksums
a69ee66439534bf11ce787805d281c50eebae358f5b5ddc101c830133998eba9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page