Load balancer for asynchroneous requests to the APIs of OpenAI and Azure (if configured) for ChatGPT
Project description
Load Balancing ChatGPT (LBGPT)
Enhance your ChatGPT API experience with the LoadBalancing ChatGPT (LBGPT), a wrapper around OpenAI's API designed to boost performance, enable caching, and provide seamless integration with Azure's OpenAI API.
This tool significantly optimizes single request response times by asynchronously interacting with the OpenAI API and efficiently caching results. It also offers automatic retries in the event of API errors and the option to balance requests between OpenAI and Azure for an even more robust AI experience.
Installation
You can easily install LoadBalancing ChatGPT via pip:
pip install lbgpt
Usage
Basic
Initiate asynchronous calls to the ChatGPT API using the following basic example:
import lbgpt
import asyncio
chatgpt = lbgpt.ChatGPT(api_key="YOUR_API_KEY")
res = asyncio.run(chatgpt.chat_completion_list([ "your list of prompts" ]))
The chat_completion_list
function expects a list of dictionaries with fully-formed OpenAI ChatCompletion API requests. Refer to the OpenAI API definition for more details. You can also use the chat_completion
function for single requests.
By default, LBGPT processes five requests in parallel, but you can adjust this by setting the max_concurrent_requests
parameter in the constructor.
Caching
Take advantage of request caching to avoid redundant calls:
import lbgpt
import asyncio
import diskcache
cache = diskcache.Cache("cache_dir")
chatgpt = lbgpt.ChatGPT(api_key="YOUR_API_KEY", cache=cache)
res = asyncio.run(chatgpt.chat_completion_list([ "your list of prompts" ]))
While LBGPT is tested with diskcache, it should work seamlessly with any cache that implements the __getitem__
and __setitem__
methods.
Azure
For users with an Azure account and proper OpenAI services setup, lbgpt offers an interface for Azure, similar to the OpenAI API. Here's how you can use it:
import lbgpt
import asyncio
chatgpt = lbgpt.AzureGPT(api_key="YOUR_API_KEY", azure_api_base="YOUR AZURE API BASE", azure_model_map={"OPENAI_MODEL_NAME": "MODEL NAME IN AZURE"})
res = asyncio.run(chatgpt.chat_completion_list([ "your list of prompts" ]))
To ensure interchangeability, map OpenAI model names to Azure model names using the azure_model_map
parameter in the constructor (see https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/switching-endpoints for details).
Load Balacing OpenAI and Azure
For optimal performance and reliability, it's recommended to set up the LoadBalancedGPT
:
import lbgpt
import asyncio
chatgpt = lbgpt.LoadBalancedGPT(
openai_api_key="YOUR_OPENAI_API_KEY",
azure_api_key="YOUR_AZURE_API_KEY",
azure_api_base="YOUR AZURE API BASE",
azure_model_map={"OPENAI_MODEL_NAME": "MODEL NAME IN AZURE"})
res = asyncio.run(chatgpt.chat_completion_list([ "your list of prompts" ]))
By default, 75% of requests are routed to the Azure API, while 25% go to the OpenAI API. You can customize this ratio by setting the ratio_openai_to_azure
parameter in the constructor, taking into account that the Azure API is considerably faster.
How to Get API Keys
To obtain your OpenAI API key, visit the official OpenAI site. For Azure API key acquisition, please refer to the official Azure documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file lbgpt-0.0.3.tar.gz
.
File metadata
- Download URL: lbgpt-0.0.3.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4da0278b6787a0ec19ccbcf179c391c4e4f48a0b6fc52e49f59e7b4bf2ca8adc |
|
MD5 | 8ec2b62b5dd653146b72d3add7ba3a0f |
|
BLAKE2b-256 | a3439b778d94e999c6fac60b6d463b0bd018756dc5092a14797924360b568d75 |
File details
Details for the file lbgpt-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: lbgpt-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a77382fcb9184d4cacaa250623a7facc2c113ba374fdc579bbf7f3e68bded05 |
|
MD5 | c79b823a5bab293a200d21311b471b32 |
|
BLAKE2b-256 | 5106bb22235a332a08827b7e23ccb7c065df7f8592fe75659941f49d0268bb9f |