Mirrowel commited on
Commit
3d8b7d3
·
1 Parent(s): 7ba1fcd

docs(rotator_library): Load API keys dynamically from environment variables

Browse files
Files changed (2) hide show
  1. README.md +15 -11
  2. src/rotator_library/README.md +18 -6
README.md CHANGED
@@ -135,10 +135,10 @@ For the simplest experience, follow the **Easy Setup for Beginners** guide at th
135
 
136
  **B) Running from Source**
137
 
138
- Start the FastAPI server with `uvicorn`. The `--reload` flag will automatically restart the server when you make code changes.
139
 
140
  ```bash
141
- uvicorn src.proxy_app.main:app --reload
142
  ```
143
 
144
  The proxy is now running and available at `http://127.0.0.1:8000`.
@@ -229,21 +229,25 @@ The core of this project is the `RotatingClient` library. When a request is made
229
  - For **rate-limit or auth errors**, it records the failure, applies an escalating cooldown for that specific key-model pair, and the client immediately tries the next available key.
230
  4. **Tracks Usage & Releases Key**: On a successful request, it records usage stats. The key's lock is then released, notifying any waiting requests that it is available.
231
 
232
- ### Enabling Request Logging
233
 
234
- For debugging purposes, you can log the full request and response for every API call. To enable this, start the proxy with the `--enable-request-logging` flag:
235
 
236
- **When running from source:**
 
 
 
 
237
  ```bash
238
- uvicorn src.proxy_app.main:app --reload -- --enable-request-logging
239
  ```
240
 
241
- **When running the executable:**
242
- ```powershell
243
- ./proxy_app.exe --enable-request-logging
244
- ```
245
 
246
- Logs will be saved as JSON files in the `logs/` directory.
 
247
 
248
  ### Troubleshooting
249
 
 
135
 
136
  **B) Running from Source**
137
 
138
+ Start the server by running the `main.py` script directly.
139
 
140
  ```bash
141
+ python src/proxy_app/main.py
142
  ```
143
 
144
  The proxy is now running and available at `http://127.0.0.1:8000`.
 
229
  - For **rate-limit or auth errors**, it records the failure, applies an escalating cooldown for that specific key-model pair, and the client immediately tries the next available key.
230
  4. **Tracks Usage & Releases Key**: On a successful request, it records usage stats. The key's lock is then released, notifying any waiting requests that it is available.
231
 
232
+ ### Command-Line Arguments and Scripts
233
 
234
+ The proxy server can be configured at runtime using the following command-line arguments:
235
 
236
+ - `--host`: The IP address to bind the server to. Defaults to `0.0.0.0` (accessible from your local network).
237
+ - `--port`: The port to run the server on. Defaults to `8000`.
238
+ - `--enable-request-logging`: A flag to enable logging of full request and response payloads to the `logs/` directory. This is useful for debugging.
239
+
240
+ **Example:**
241
  ```bash
242
+ python src/proxy_app/main.py --host 127.0.0.1 --port 9999 --enable-request-logging
243
  ```
244
 
245
+ #### Windows Batch Scripts
246
+
247
+ For convenience on Windows, you can use the provided `.bat` scripts in the root directory to run the proxy with common configurations:
 
248
 
249
+ - **`start_proxy.bat`**: Starts the proxy on `0.0.0.0:8000` with default settings.
250
+ - **`start_proxy_debug_logging.bat`**: Starts the proxy and automatically enables request logging.
251
 
252
  ### Troubleshooting
253
 
src/rotator_library/README.md CHANGED
@@ -32,14 +32,26 @@ This is the main class for interacting with the library. It is designed to be a
32
  ### Initialization
33
 
34
  ```python
 
 
35
  from rotating_api_key_client import RotatingClient
36
- from typing import Dict, List
37
 
38
- # Define your API keys, grouped by provider
39
- api_keys: Dict[str, List[str]] = {
40
- "gemini": ["your_gemini_key_1", "your_gemini_key_2"],
41
- "openai": ["your_openai_key_1"],
42
- }
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  client = RotatingClient(
45
  api_keys=api_keys,
 
32
  ### Initialization
33
 
34
  ```python
35
+ import os
36
+ from dotenv import load_dotenv
37
  from rotating_api_key_client import RotatingClient
 
38
 
39
+ # Load environment variables from .env file
40
+ load_dotenv()
41
+
42
+ # Dynamically load all provider API keys from environment variables
43
+ api_keys = {}
44
+ for key, value in os.environ.items():
45
+ # This pattern finds keys like "GEMINI_API_KEY_1" or "OPENAI_API_KEY"
46
+ if (key.endswith("_API_KEY") or "_API_KEY_" in key) and key != "PROXY_API_KEY":
47
+ # Extracts "gemini" from "GEMINI_API_KEY_1"
48
+ provider = key.split("_API_KEY")[0].lower()
49
+ if provider not in api_keys:
50
+ api_keys[provider] = []
51
+ api_keys[provider].append(value)
52
+
53
+ if not api_keys:
54
+ raise ValueError("No provider API keys found in environment variables.")
55
 
56
  client = RotatingClient(
57
  api_keys=api_keys,