├── .gitignore ├── .gitattributes ├── README_CN.md ├── LICENSE ├── README.md └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | apikey.txt 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # OpenAI-Checker 2 | 3 | ## 简介 4 | `OpenAI-Checker` 是一个 Python 脚本,用于检查 OpenAI API 密钥的账单和使用情况,并确定是否可以访问 GPT-4 模型。 5 | 6 | ## 依赖 7 | - Python 3.6+ 8 | - requests 模块 (可以通过 `pip install requests` 命令进行安装) 9 | - 可以访问 `api.openai.com` 10 | 11 | ## 使用 12 | 1. 将此仓库克隆或下载至本地。 13 | 2. 安装所需的依赖项。 14 | 3. 将您的 OpenAI API 密钥添加到一个名为 `apikey.txt` 的文件中,每行一个KEY。 15 | 4. 在终端中键入 `python main.py` 命令运行脚本。 16 | 17 | ## 输出 18 | 脚本将输出 `apikey.txt` 文件中每个密钥的总金额、总使用量、剩余金额以及 API 密钥是否可以访问 GPT-4 模型。 19 | 20 | ## 示例 21 | 22 | ```shell 23 | $ python main.py 24 | [*]Total amount: 5.00 USD 25 | [*]Total usage: 0.00 USD 26 | [*]Remaining amount: 5.00 USD 27 | [-]API Key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx cannot access GPT-4 28 | ``` 29 | 30 | 31 | ## 许可证 32 | 本项目基于 MIT 许可证发布 - 请参见 LICENSE 文件获取详情。 33 | 34 | ## 参考文献 35 | 本项目包括使用 OpenAI 基于 GPT-3.5 架构开发的语言模型 ChatGPT 的帮助开发的代码。 36 | 37 | 以下项目在本项目的开发过程中被引用: 38 | 39 | - [openai-billing](https://github.com/ClarenceDan/openai-billing) - [用于余额查询代码的参考] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 endercatone 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenAI-Checker 2 | 3 | [English](https://github.com/endercatone/OpenAI-Checker) 4 | 5 | [中文](https://github.com/endercatone/OpenAI-Checker/blob/main/README_CN.md) 6 | 7 | ## Introduction 8 | `OpenAI-Checker` is a Python script that checks the billing and usage of an OpenAI API key and determines whether it can access the GPT-4 model. 9 | 10 | ## Dependencies 11 | - Python 3.6+ 12 | - requests module (can be installed via `pip install requests`) 13 | - Can access `api.openai.com` 14 | 15 | ## Usage 16 | 1. Clone or download this repository to your local machine. 17 | 2. Install the required dependencies. 18 | 3. Add your OpenAI API keys to a file named `apikey.txt`, with each key on a separate line. 19 | 4. Run the script by typing `python main.py` in your terminal. 20 | 21 | ## Output 22 | The script will output the total amount, total usage, remaining amount, and whether the API key can access the GPT-4 model for each key in the `apikey.txt` file. 23 | 24 | ## Example 25 | 26 | ```shell 27 | $ python main.py 28 | [*]Total amount: 5.00 USD 29 | [*]Total usage: 0.00 USD 30 | [*]Remaining amount: 5.00 USD 31 | [-]API Key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx cannot access GPT-4 32 | ``` 33 | 34 | 35 | 36 | ## License 37 | This project is licensed under the MIT License - see the LICENSE file for details. 38 | 39 | 40 | 41 | 42 | ## References 43 | 44 | This project includes code that was developed with the assistance of ChatGPT, a language model developed by OpenAI based on the GPT-3.5 architecture. 45 | 46 | 47 | 48 | The following projects were referenced during the development of this project: 49 | 50 | - [openai-billing](https://github.com/ClarenceDan/openai-billing) - [Reference was made to this project for the balance inquiry code] 51 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import requests 3 | 4 | def check_billing(api_key): 5 | now = datetime.datetime.now() 6 | start_date = (now - datetime.timedelta(days=90)).strftime("%Y-%m-%d") 7 | end_date = (now + datetime.timedelta(days=1)).strftime("%Y-%m-%d") 8 | sub_date = datetime.datetime(now.year, now.month, 1).strftime("%Y-%m-%d") 9 | 10 | headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} 11 | 12 | try: 13 | response = requests.get(f"https://api.openai.com/v1/dashboard/billing/subscription", headers=headers) 14 | response.raise_for_status() 15 | total_amount = response.json()["hard_limit_usd"] 16 | 17 | if total_amount > 20: 18 | start_date, url_usage = sub_date, f"https://api.openai.com/v1/dashboard/billing/usage?start_date={sub_date}&end_date={end_date}" 19 | response = requests.get(url_usage, headers=headers) 20 | response.raise_for_status() 21 | total_usage = response.json()["total_usage"] / 100 22 | else: 23 | total_usage = 0 24 | 25 | remaining = total_amount - total_usage 26 | 27 | url_models = "https://api.openai.com/v1/models" 28 | response = requests.get(url_models, headers=headers) 29 | response.raise_for_status() 30 | models = response.json()["data"] 31 | can_access = any(model["id"] == "gpt-4" for model in models) 32 | 33 | print(f"[*]Total amount: {total_amount:.2f} USD\n[*]Total usage: {total_usage:.2f} USD\n[*]Remaining amount: {remaining:.2f} USD\n{'[-]' if not can_access else '[+]'}API Key: {api_key} {'can' if can_access else 'cannot'} access GPT-4\n") 34 | 35 | return [total_amount, total_usage, remaining, can_access] 36 | except Exception as e: 37 | print(f"Query failed: {e}") 38 | return [None, None, None, False] 39 | 40 | if __name__ == '__main__': 41 | with open('apikey.txt', 'r') as f: 42 | api_keys = f.read().splitlines() 43 | for api_key in api_keys: 44 | check_billing(api_key) 45 | --------------------------------------------------------------------------------