Key rotator, don't get dizzy.
Project description
Gemini API Key Rotator for LlamaIndex
This utility monkey-patches google.auth.api_key.Credentials to rotate multiple Google API keys, useful with LlamaIndex to manage Gemini API request limits.
How to Use
- Install the package: run this in command prompt
pip install gemini-rotator. - Import and Patch: Before initializing your Gemini LLM, call the
patchfunction with your API keys. The script modifies the Google authentication library at runtime. Whenpatchis called:- It extends the
Credentialsclass to cycle through your provided API keys.
- It extends the
patch(api_keys, api_main=None)
api_keys: A list of your Google API key strings.api_main: (Optional, but Recommended) An API key fromapi_keys. Rotation only occurs if the default key passed to LlamaIndex or generativeai.configure() matchesapi_main. This prevents interference with other Google services using different keys. IfNone, keys rotate on every use (use with caution as it might affect other Google clients).
restore()
Call restore() to revert the monkey-patching and return the google.auth.api_key.Credentials class to its original state.
Example with LlamaIndex
# Assuming the rotator script is saved as gemini_key_rotator.py
from gemini_key_rotator import patch
from llama_index.llms.gemini import Gemini
from llama_index.core.base.llms.types import ChatMessage
# --- Configuration ---
MY_GEMINI_API_KEYS = [
"YOUR_API_KEY_1", # Replace
"YOUR_API_KEY_2", # Replace
"YOUR_API_KEY_3", # Replace
]
MAIN_API_KEY = MY_GEMINI_API_KEYS[0]
# Monkey patch
patch(MY_GEMINI_API_KEYS, MAIN_API_KEY)
llm = Gemini(model="gemini-2.0-flash", api_key=MAIN_API_KEY) # Initialize LlamaIndex with the main key
messages = [ChatMessage(content="Hello Gemini", role="user")] # Example messages
response = llm.chat(messages=messages) # Send message to Gemini
print(response.message.content) # Print the response
The patch will take care of rotating the API keys for you. The Gemini class will use the main key for initialization, and the rotation will happen automatically when making requests.
Important Notes
api_main: Usingapi_mainis strongly advised to ensure rotation only affects the intended Gemini client.- Get API Keys: Obtain your API keys from the Google Cloud Console under "Credentials". You should have Gemini API enabled in your project and its limited to one api key per project. You can create multiple projects to get more keys.
- Risks: Abusing the api may lead to account suspension. Use responsibly and ensure compliance with Google's API usage policies.
- Use Cases: This is not for production use. It's a temporary solution for testing that requires slightly more thant gemini rate limits to effectively test and benchmark.
Disclaimer
Use at your own risk. Comply with Google's API terms.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gemini_rotator-0.1.0.tar.gz.
File metadata
- Download URL: gemini_rotator-0.1.0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4738f36a531f6679da8e2b8c21843b7c80c07667dfdf6eeb194451ae43a2760
|
|
| MD5 |
39e122a4b24fba774f37b54c62969a3c
|
|
| BLAKE2b-256 |
e8cef3f59b36171ecb10383d168d5335806af36095acb855a1eee99ddb7f5f17
|
File details
Details for the file gemini_rotator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gemini_rotator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79007f7722dd21cc5bc10df91c4cbc12ec520e3b3c23005b2176b3ed75525c39
|
|
| MD5 |
18cf4b93713f85b9ed84933dd6959a08
|
|
| BLAKE2b-256 |
eb5c119e9055c29c444654666a57a710a2a12c6bf083b6dc30abbc35ec69608a
|