A Python library for validating OpenAI API keys, and listing available models and verifying model access, and retrieving detailed account usage metrics.
Project description
OpenAI API Key Verifier
This Python library allows you to verify the validity of an OpenAI API key by making a test request to the OpenAI API and checking the response status. It is a useful tool for both individual developers and platforms that use OpenAI's API to ensure API keys are working as expected before making actual API calls.
Why Use This Library?
While you can verify an OpenAI API key by simply making an API call yourself, this library offers several benefits:
- Convenience: It provides an easy, reusable way to verify keys with a single function call, handling potential errors and logging them properly.
- Model Access Verification: Can determine if your key has access to specific models like GPT-4, which is a paid feature. This is particularly useful for open source tools that require GPT-4 access.
- Rate Limit Check: It provides additional options to get information about rate limits, available models, and account usage.
- Integration for Open Source Tools: If you're developing an open-source tool that allows users to provide their own OpenAI API key, this library makes it easier to validate those keys and verify model access without duplicating code.
- Security and Consistency: This library adds a layer of consistent validation for any OpenAI API key by using regex format validation followed by an actual API call.
- Detailed Usage Analysis: Provides comprehensive monthly token usage statistics with cost breakdowns for input and output tokens.
Security and Transparency
- Open Source: The library is fully open source, and you can inspect the code yourself to ensure that it is secure. You can also compile it directly from the source if you prefer.
- Minimal Data Handling: The library only sends your API key directly to OpenAI's official endpoints to verify validity. It does not store, log, or send API keys to any third party.
- No Data Farming: The library is not intended to farm or steal API keys. It was created in response to the need for a reliable, easy way to validate keys and verify model access.
Prerequisites
- Python 3: You can download it from python.org
Installation
To install the OpenAI API Key Verifier library using pip, run:
pip3 install openai_api_key_verifier
Usage
Python Example
from openai_api_key_verifier import verify_api_key, check_model_access, list_models, get_account_usage
# Replace with your actual API key
api_key = "your_actual_api_key_here"
# Verify if the API key is valid
is_valid = verify_api_key(api_key)
if is_valid:
print("API key is valid!")
# Check for GPT-4 access
if check_model_access(api_key, "gpt-4"):
print("This key has GPT-4 access!")
# List all available models
list_models(api_key)
# Get usage statistics
get_account_usage(api_key)
else:
print("API key is invalid.")
Command Line Usage
You can verify an API key directly from the command line with various options:
# Basic verification
python3 verify_openai_api_key YOUR_API_KEY
# Check model access
python3 verify_openai_api_key YOUR_API_KEY --check-model gpt-4
# List available models
python3 verify_openai_api_key YOUR_API_KEY --list-models
# Show usage statistics
python3 verify_openai_api_key YOUR_API_KEY --show-usage
# Enable debug logging
python3 verify_openai_api_key YOUR_API_KEY --debug
Available Functions
verify_api_key(api_key)
: Checks the validity of the API key by making a minimal request to the OpenAI API.check_model_access(api_key, model_name)
: Verifies if the key has access to a specific model.list_models(api_key)
: Lists available models for the given API key.get_account_usage(api_key)
: Retrieves detailed account usage statistics, including token counts and costs.
Key Features
- Enhanced Error Handling: Better handling of rate limits and API errors with automatic retries.
- Detailed Usage Statistics: Monthly token usage breakdown with separate tracking for input and output tokens.
- Cost Analysis: Calculation of costs based on current OpenAI pricing for both input and output tokens.
- Debug Mode: Additional logging options for troubleshooting with the
--debug
flag. - Improved Console Output: Better formatted console output with clear visual indicators for success and failure.
Contributing
If you'd like to contribute to this project, please fork the repository and create a pull request with your changes. We welcome feedback and improvements from the community to make this tool more useful.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
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 openai_api_key_verifier-1.0.2.tar.gz
.
File metadata
- Download URL: openai_api_key_verifier-1.0.2.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23b405f6cc24d92a1addcc7358bf7d99b5b8eea144adaf06b6bffdf374f54d32 |
|
MD5 | 27a6b7a0b72e53c850a6c0a3f47a17f9 |
|
BLAKE2b-256 | c7d9b95b3f0d5fc930552636cc05711cc668e6d97134ced022e6a8c378fd6fa0 |
File details
Details for the file openai_api_key_verifier-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: openai_api_key_verifier-1.0.2-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1445ddda6fe44d7356ffeaabe5a3a235bc601b07bde44b733ac717be3af7df3 |
|
MD5 | 2e9a79e420561261b8361068701e5fdb |
|
BLAKE2b-256 | 92e14deb128614835c49ace5b90f43646bd015b84dffccf7440d97ff645425c6 |