- Make sure you install poetry
- Create a conda environment with Python version 3.11
- Execute:
poetry install
- Create a copy of
.env.templateand rename it to.envafter populating it with necessary values. - Redis Setup:
- Install Redis by following the installation guide.
- Update your
.envfile to include the Redis configuration variables:REDIS_HOST=localhost REDIS_PORT=6379 REDIS_DB=0
- Make sure that your Redis instance is running and accessible before starting the application.
- To enable persistence, configure Redis with either RDB snapshotting or Append Only File (AOF) options:
- RDB Snapshots:
Configure snapshot intervals in yourredis.confby addingsave 900 1(for saving every 15 minutes if at least 1 key changed). - AOF:
Enable AOF by settingappendonly yesand fine-tune theappendfsyncpolicy as needed.
- RDB Snapshots:
- Update the .env file with postgres connection details
To use the LoadBalancer class, first import it and initialize an object:
from load_balancer import LoadBalancer
# Create a LoadBalancer object with 10 API keys and a cooldown time of 10 seconds
lb = LoadBalancer(n=10, ct=10)- Initializes the load balancer with
nAPI keys. - Loads API keys from environment variables.
- Initializes tracking for failed keys and their cooldown times.
- Keeps a usage count for each key.
- Parameters:
n: Number of API keys.ct: Cooldown time (in seconds) for failed keys.
- Implements a round-robin load balancing strategy.
- Returns the next API key in sequence.
FailureAwareselects an API key while avoiding failed keys.- If a key has failed and is still in cooldown, it is skipped.
- Raises an exception if all keys are in cooldown.
report_failmarks an API key as failed and sets its cooldown time.- Parameter:
key: The failed API key.
- Selects an API key based on a probability distribution using standard deviation.
- Prioritizes keys that have been used less frequently.
- Does not check for failed keys.
key = lb.Round_Robin()
print("Selected Key:", key)try:
key = lb.FailureAware()
print("Selected Key:", key)
except Exception as e:
print("Error:", e)
# Reporting a failed key
lb.report_fail("API_KEY_1")key = lb.StdDev()
print("Selected Key:", key)Further stress testing needs to be done in order to find out which algorithm can handle the traffic the best.
The .env file should be structured as follows:
GOOGLE_API_KEY_<n> = <your_gemini_api_key>
Replace <n> with the key index (e.g., 1, 2, 3,...) and <your_gemini_api_key> with your actual Gemini API key.
Example:
GOOGLE_API_KEY_1 = <your_gemini_api_key_1>
GOOGLE_API_KEY_2 = <your_gemini_api_key_2>
- Add the host uri to the
OLLAMA_BASE_URL. See the existing url in the.env.templatefor example
- Don't commit code/changes directly to
main. - Create a separate branch for your changes and then create a PR request to merge into
main. - Branch names shouldn't be random:
- For a new feature, use
feature/{feature_name}. - For a fix, use
fix/{fix_name}.
- For a new feature, use
- Every PR message must include a title, description, and changes.