闲来无事,糊了一个基于中转站key获取可用模型,测试以及查询余额的小脚本

逛了许久并未发现中转站key一体式信息查询,所以糊了一个

主打一个诚实!字体颜色部分与English部分由GPT生成

话不多说 先贴图!




重头戏:

运行库安装:

pip install requests tabulate

代码

双语版:

import requests
import random
import os
from tabulate import tabulate
import datetime
import time

# ANSI字体颜色代码 (仅前景色)
colors = [
    '\033[31m',  # 红色 (Red)
    '\033[32m',  # 绿色 (Green)
    '\033[33m',  # 黄色 (Yellow)
    '\033[34m',  # 蓝色 (Blue)
    '\033[35m',  # 紫色 (Magenta)
    '\033[36m',  # 青色 (Cyan)
    '\033[37m',  # 白色 (White)
    '\033[90m',  # 灰色 (Grey)
]

RESET = '\033[0m'

api_key = None
url = None
language = "中文"  # 默认语言 (Default language)

language_mapping = {
    "中文": {
        "menu_title": "请选择一个选项:",
        "option_1": "1. 获取额度",
        "option_2": "2. 获取模型",
        "option_3": "3. 模型测试",
        "option_4": "4. 选择语言",
        "option_0": "0. 退出",
        "enter_api_url": "请输入API URL(例如:https://api.openai.com):",
        "enter_api_key": "请输入您的API Key:",
        "back_to_menu": "回车返回菜单!!!",
        "invalid_choice": "无效的选择,请重新输入。",
        "current_api_url": "当前API URL:",
        "current_api_key": "当前API Key:",
        "select_language": "请选择语言 (1. 中文, 2. English): ",
        "language_selected": "语言已选择:",
        "test_model_prompt": "请输入测试模型(默认gpt-3.5-turbo),直接回车则使用默认模型",
        "model_input": "模型:",
        "user_call_model": "用户调用模型:",
        "actual_response_model": "实际响应模型:",
        "response_time": "响应时间:",
        "response_success": "响应成功",
        "response_content": "内容:",
        "request_failed": "请求失败,状态码:",
        "error_occurred": "请求时发生错误:",
        "balance_info": "当前账户余额信息:",
        "total_amount": "总额:",
        "used_amount": "已用:",
        "remaining_amount": "剩余:",
        "get_models_error": "获取模型时出错:",
        "unknown_model": "未知模型",
        "no_content": "无内容",
        "company": "公司",
        "model_list": "模型列表",
        "currency": "美元",
        "input_choice": "请输入选项 (1/2/3/4): ",
    },
    "English": {
        "menu_title": "Please select an option:",
        "option_1": "1. Get balance",
        "option_2": "2. Get models",
        "option_3": "3. Test model",
        "option_4": "4. Select language",
        "option_0": "0. Exit",
        "enter_api_url": "Please enter the API URL (e.g., https://api.openai.com):",
        "enter_api_key": "Please enter your API Key:",
        "back_to_menu": "Press Enter to return to the menu!!!",
        "invalid_choice": "Invalid choice, please re-enter.",
        "current_api_url": "Current API URL:",
        "current_api_key": "Current API Key:",
        "select_language": "Please select language (1. 中文, 2. English): ",
        "language_selected": "Language selected:",
        "test_model_prompt": "Please enter the model to test (default is gpt-3.5-turbo), press Enter to use the default model",
        "model_input": "Model:",
        "user_call_model": "User calls model:",
        "actual_response_model": "Actual response model:",
        "response_time": "Response time:",
        "response_success": "Response successful",
        "response_content": "Content:",
        "request_failed": "Request failed, status code:",
        "error_occurred": "Error occurred while requesting:",
        "balance_info": "Current account balance information:",
        "total_amount": "Total:",
        "used_amount": "Used:",
        "remaining_amount": "Remaining:",
        "get_models_error": "Error getting models:",
        "unknown_model": "Unknown Model",
        "no_content": "No Content",
        "company": "Company",
        "model_list": "Model List",
        "currency": "USD",
        "input_choice": "Please enter your choice (1/2/3/4): ",
    }
}

company_mapping = {
    "anthropic": {"中文": "Anthropic", "English": "Anthropic"},
    "openai": {"中文": "OpenAI", "English": "OpenAI"},
    "deepseek": {"中文": "DeepSeek", "English": "DeepSeek"},
    "doubao": {"中文": "豆包", "English": "Doubao"},
    "google": {"中文": "谷歌", "English": "Google"},
    "zhipu": {"中文": "智谱", "English": "Zhipu"},
    "Zhipu_4v": {"中文": "智谱", "English": "Zhipu"},
    "midjourney": {"中文": "Midjourney", "English": "Midjourney"},
    "moonshot": {"中文": "Moonshot", "English": "Moonshot"},
    "alibaba": {"中文": "阿里巴巴", "English": "Alibaba"},
    "iflytek": {"中文": "讯飞", "English": "iFlytek"},
    "baidu": {"中文": "百度", "English": "Baidu"},
    "tencent": {"中文": "腾讯", "English": "Tencent"},
    "microsoft": {"中文": "微软", "English": "Microsoft"},
    "huggingface": {"中文": "Hugging Face", "English": "Hugging Face"},
    "stabilityai": {"中文": "Stability AI", "English": "Stability AI"},
    "cohere": {"中文": "Cohere", "English": "Cohere"},
    "ai21": {"中文": "AI21 Labs", "English": "AI21 Labs"},
    "nvidia": {"中文": "NVIDIA", "English": "NVIDIA"},
    "apple": {"中文": "苹果", "English": "Apple"},
    "salesforce": {"中文": "Salesforce", "English": "Salesforce"},
    "xiaomi": {"中文": "小米", "English": "Xiaomi"},
    "meituan": {"中文": "美团", "English": "Meituan"},
    "jd": {"中文": "京东", "English": "JD"},
    "bytedance": {"中文": "字节跳动", "English": "ByteDance"},
    "facebook": {"中文": "Facebook", "English": "Facebook"},
    "amazon": {"中文": "亚马逊", "English": "Amazon"},
    "oracle": {"中文": "甲骨文", "English": "Oracle"},
    "intel": {"中文": "英特尔", "English": "Intel"},
    "qualcomm": {"中文": "高通", "English": "Qualcomm"},
    "Custom": {"中文": "其他", "English": "Custom"}
}

def generate_random_colors(num_colors):
    random.shuffle(colors)
    return colors[:num_colors]

def categorize_and_color_models(models):
    categorized_models = {}
    company_names = {company_mapping.get(model['owned_by'].lower(), {"中文": model['owned_by'], "English": model['owned_by']})[language] for model in models}
    company_colors = {company: color for company, color in zip(company_names, generate_random_colors(len(company_names)))}
    
    for model in models:
        model_name = model['id']
        owned_by = model.get('owned_by', 'unknown').lower()
        company_name = company_mapping.get(owned_by, {"中文": owned_by, "English": owned_by})[language]
        
        color = company_colors.get(company_name, RESET)
        colored_model = f"{color}{model_name}{RESET}"
        
        if company_name not in categorized_models:
            categorized_models[company_name] = []
        categorized_models[company_name].append(colored_model)
    
    return categorized_models

def print_categorized_models(categorized_models):
    table = []
    for company, models in categorized_models.items():
        table.append([f"\033[1m{company.capitalize()}\033[0m", "\n".join(models)])
    
    print(f"\033[34m{tabulate(table, headers=[language_mapping[language]['company'], language_mapping[language]['model_list']], tablefmt='grid')}{RESET}")

def get_available_models():
    try:
        response = requests.get(url + '/v1/models', headers={'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"})
        response.raise_for_status()
        return response.json().get('data', [])
    except requests.exceptions.RequestException as e:
        print(f"{colors[0]}{language_mapping[language]['get_models_error']}{e}{RESET}")
        return []

def get_balance():
    headers = {'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"}

    subscription_url = "/v1/dashboard/billing/subscription"
    subscription_response = requests.get(url + subscription_url, headers=headers)
    if subscription_response.status_code == 200:
        data = subscription_response.json()
        total = data.get("hard_limit_usd")
    else:
        print(f"{colors[0]}{subscription_response.text}{RESET}")
        total = 0

    start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime("%Y-%m-%d")
    end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    billing_url = f"{url}/v1/dashboard/billing/usage?start_date={start_date}&end_date={end_date}"
    billing_response = requests.get(billing_url, headers=headers)
    if billing_response.status_code == 200:
        data = billing_response.json()
        total_usage = data.get("total_usage") / 100
    else:
        print(f"{colors[0]}{billing_response.text}{RESET}")
        total_usage = 0

    remaining = total - total_usage
    print(f"{colors[1]}{language_mapping[language]['balance_info']}{RESET}")
    print(f"{colors[2]}{language_mapping[language]['total_amount']}\t{total:.4f}{language_mapping[language]['currency']}{RESET}")
    print(f"{colors[3]}{language_mapping[language]['used_amount']}\t{total_usage:.4f}{language_mapping[language]['currency']}{RESET}")
    print(f"{colors[4]}{language_mapping[language]['remaining_amount']}\t{remaining:.4f}{language_mapping[language]['currency']}{RESET}")

def test_model():
    print(f"{colors[2]}{language_mapping[language]['test_model_prompt']}{RESET}")
    model = input(f"{colors[3]}{language_mapping[language]['model_input']}{RESET}").strip() or "gpt-3.5-turbo"
    os.system('cls' if os.name == 'nt' else 'clear')
    data = {
        "model": model,
        "messages": [
            {"role": "user", "content": "say this is text!"}
        ]
    }
    print(f"{colors[1]}{language_mapping[language]['user_call_model']}{model}{RESET}")
    try:
        start_time = time.time()
        response = requests.post(url + "/v1/chat/completions", headers={'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"}, json=data)
        end_time = time.time()
        response_time = end_time - start_time
        if response.status_code == 200:
            response_json = response.json()
            model_name = response_json.get('model', language_mapping[language]['unknown_model'])
            content = response_json.get('choices', [{}])[0].get('message', {}).get('content', language_mapping[language]['no_content'])
            
            print(f"{colors[1]}{language_mapping[language]['actual_response_model']}{model_name}{RESET}")
            print(f"{colors[1]}{language_mapping[language]['response_time']}{response_time:.2f} 秒{RESET}")
            print(f"{colors[1]}{language_mapping[language]['response_success']}{RESET}")
            print(f"{colors[1]}{language_mapping[language]['response_content']}{content}{RESET}")
        else:
            print(f"{colors[0]}{language_mapping[language]['request_failed']}{response.status_code}{RESET}")
            print(f"{colors[0]}{response.text}{RESET}")

    except requests.exceptions.RequestException as e:
        print(f"{colors[0]}{language_mapping[language]['error_occurred']}{e}{RESET}")

def get_user_credentials():
    global url, api_key
    url = input(f"{colors[2]}{language_mapping[language]['enter_api_url']}{RESET}").strip()
    api_key = input(f"{colors[2]}{language_mapping[language]['enter_api_key']}{RESET}").strip()

def select_language():
    global language
    print(f"{colors[2]}{language_mapping[language]['select_language']}{RESET}")
    lang_choice = input().strip()
    if lang_choice == "1":
        language = "中文"
    elif lang_choice == "2":
        language = "English"
    else:
        print(f"{colors[0]}{language_mapping[language]['invalid_choice']}{RESET}")
        select_language()
    print(f"{colors[2]}{language_mapping[language]['language_selected']}{language}{RESET}")

def menu():
    os.system('cls' if os.name == 'nt' else 'clear')
    
    if not api_key or not url:
        get_user_credentials()
        os.system('cls' if os.name == 'nt' else 'clear')

    print(f"{colors[3]}{language_mapping[language]['current_api_url']}{url}{RESET}")
    print(f"{colors[3]}{language_mapping[language]['current_api_key']}{api_key}{RESET}")
    print()
    
    print(f"{colors[5]}{language_mapping[language]['menu_title']}{RESET}")
    print(f"{colors[2]}{language_mapping[language]['option_1']}{RESET}")
    print(f"{colors[3]}{language_mapping[language]['option_2']}{RESET}")
    print(f"{colors[4]}{language_mapping[language]['option_3']}{RESET}")
    print(f"{colors[4]}{language_mapping[language]['option_4']}{RESET}")
    print(f"{colors[0]}{language_mapping[language]['option_0']}{RESET}")
    
    choice = input(f"{colors[3]}{language_mapping[language]['input_choice']}{RESET}")
    
    if choice == '1':
        os.system('cls' if os.name == 'nt' else 'clear')
        get_balance()
        print()
        input(f"{colors[4]}{language_mapping[language]['back_to_menu']}{RESET}")
        menu()
    elif choice == '2':
        os.system('cls' if os.name == 'nt' else 'clear')
        models = get_available_models()
        categorized_models = categorize_and_color_models(models)
        print_categorized_models(categorized_models)
        print()
        input(f"{colors[4]}{language_mapping[language]['back_to_menu']}{RESET}")
        menu()
    elif choice == '3':
        os.system('cls' if os.name == 'nt' else 'clear')
        test_model()
        print()
        input(f"{colors[4]}{language_mapping[language]['back_to_menu']}{RESET}")
        menu()
    elif choice == '4':
        os.system('cls' if os.name == 'nt' else 'clear')
        select_language()
        menu()
    elif choice == '0':
        os.system('cls' if os.name == 'nt' else 'clear')
        exit()
    else:
        print(f"{colors[0]}{language_mapping[language]['invalid_choice']}{RESET}")
        menu()

if __name__ == "__main__":
    os.system('cls' if os.name == 'nt' else 'clear')
    select_language()
    menu()

中文版:

import requests
import random
import os
from tabulate import tabulate
import datetime
import time

# ANSI字体颜色代码 (仅前景色)
colors = [
    '\033[31m',  # 红色 (Red)
    '\033[32m',  # 绿色 (Green)
    '\033[33m',  # 黄色 (Yellow)
    '\033[34m',  # 蓝色 (Blue)
    '\033[35m',  # 紫色 (Magenta)
    '\033[36m',  # 青色 (Cyan)
    '\033[37m',  # 白色 (White)
    '\033[90m',  # 灰色 (Grey)
]

RESET = '\033[0m'

api_key = None
url = None

company_mapping = {
    "anthropic": {"中文": "Anthropic"},
    "openai": {"中文": "OpenAI"},
    "deepseek": {"中文": "DeepSeek"},
    "doubao": {"中文": "豆包"},
    "google": {"中文": "谷歌"},
    "zhipu": {"中文": "智谱"},
    "Zhipu_4v": {"中文": "智谱"},
    "midjourney": {"中文": "Midjourney"},
    "moonshot": {"中文": "Moonshot"},
    "alibaba": {"中文": "阿里巴巴"},
    "iflytek": {"中文": "讯飞"},
    "baidu": {"中文": "百度"},
    "tencent": {"中文": "腾讯"},
    "microsoft": {"中文": "微软"},
    "huggingface": {"中文": "Hugging Face"},
    "stabilityai": {"中文": "Stability AI"},
    "cohere": {"中文": "Cohere"},
    "ai21": {"中文": "AI21 Labs"},
    "nvidia": {"中文": "NVIDIA"},
    "apple": {"中文": "苹果"},
    "salesforce": {"中文": "Salesforce"},
    "xiaomi": {"中文": "小米"},
    "meituan": {"中文": "美团"},
    "jd": {"中文": "京东"},
    "bytedance": {"中文": "字节跳动"},
    "facebook": {"中文": "Facebook"},
    "amazon": {"中文": "亚马逊"},
    "oracle": {"中文": "甲骨文"},
    "intel": {"中文": "英特尔"},
    "qualcomm": {"中文": "高通"},
    "Custom": {"中文": "其他"}
}

def generate_random_colors(num_colors):
    random.shuffle(colors)
    return colors[:num_colors]

def categorize_and_color_models(models):
    categorized_models = {}
    company_names = {company_mapping.get(model['owned_by'].lower(), {"中文": model['owned_by']})["中文"] for model in models}
    company_colors = {company: color for company, color in zip(company_names, generate_random_colors(len(company_names)))}
    
    for model in models:
        model_name = model['id']
        owned_by = model.get('owned_by', 'unknown').lower()
        company_name = company_mapping.get(owned_by, {"中文": owned_by})["中文"]
        
        color = company_colors.get(company_name, RESET)
        colored_model = f"{color}{model_name}{RESET}"
        
        if company_name not in categorized_models:
            categorized_models[company_name] = []
        categorized_models[company_name].append(colored_model)
    
    return categorized_models

def print_categorized_models(categorized_models):
    table = []
    for company, models in categorized_models.items():
        table.append([f"\033[1m{company.capitalize()}\033[0m", "\n".join(models)])
    
    print(f"\033[34m{tabulate(table, headers=['公司', '模型列表'], tablefmt='grid')}{RESET}")

def get_available_models():
    try:
        response = requests.get(url + '/v1/models', headers={'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"})
        response.raise_for_status()
        return response.json().get('data', [])
    except requests.exceptions.RequestException as e:
        print(f"{colors[0]}获取模型时出错:{e}{RESET}")
        return []

def get_balance():
    headers = {'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"}

    subscription_url = "/v1/dashboard/billing/subscription"
    subscription_response = requests.get(url + subscription_url, headers=headers)
    if subscription_response.status_code == 200:
        data = subscription_response.json()
        total = data.get("hard_limit_usd")
    else:
        print(f"{colors[0]}{subscription_response.text}{RESET}")
        total = 0

    start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime("%Y-%m-%d")
    end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    billing_url = f"{url}/v1/dashboard/billing/usage?start_date={start_date}&end_date={end_date}"
    billing_response = requests.get(billing_url, headers=headers)
    if billing_response.status_code == 200:
        data = billing_response.json()
        total_usage = data.get("total_usage") / 100
    else:
        print(f"{colors[0]}{billing_response.text}{RESET}")
        total_usage = 0

    remaining = total - total_usage
    print(f"{colors[1]}当前账户余额信息:{RESET}")
    print(f"{colors[2]}总额:\t{total:.4f}美元{RESET}")
    print(f"{colors[3]}已用:\t{total_usage:.4f}美元{RESET}")
    print(f"{colors[4]}剩余:\t{remaining:.4f}美元{RESET}")

def test_model():
    print(f"{colors[2]}请输入测试模型(默认gpt-3.5-turbo),直接回车则使用默认模型{RESET}")
    model = input(f"{colors[3]}模型:{RESET}").strip() or "gpt-3.5-turbo"
    os.system('cls' if os.name == 'nt' else 'clear')
    data = {
        "model": model,
        "messages": [
            {"role": "user", "content": "say this is text!"}
        ]
    }
    print(f"{colors[1]}用户调用模型:{model}{RESET}")
    try:
        start_time = time.time()
        response = requests.post(url + "/v1/chat/completions", headers={'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"}, json=data)
        end_time = time.time()
        response_time = end_time - start_time
        if response.status_code == 200:
            response_json = response.json()
            model_name = response_json.get('model', '未知模型')
            content = response_json.get('choices', [{}])[0].get('message', {}).get('content', '无内容')
            
            print(f"{colors[1]}实际响应模型:{model_name}{RESET}")
            print(f"{colors[1]}响应时间:{response_time:.2f} 秒{RESET}")
            print(f"{colors[1]}响应成功{RESET}")
            print(f"{colors[1]}内容:{content}{RESET}")
        else:
            print(f"{colors[0]}请求失败,状态码:{response.status_code}{RESET}")
            print(f"{colors[0]}{response.text}{RESET}")

    except requests.exceptions.RequestException as e:
        print(f"{colors[0]}请求时发生错误:{e}{RESET}")

def get_user_credentials():
    global url, api_key
    url = input(f"{colors[2]}请输入API URL(例如:https://api.openai.com):{RESET}").strip()
    api_key = input(f"{colors[2]}请输入您的API Key:{RESET}").strip()

def menu():
    os.system('cls' if os.name == 'nt' else 'clear')
    
    if not api_key or not url:
        get_user_credentials()
        os.system('cls' if os.name == 'nt' else 'clear')

    print(f"{colors[3]}当前API URL:{url}{RESET}")
    print(f"{colors[3]}当前API Key:{api_key}{RESET}")
    print()
    
    print(f"{colors[5]}请选择一个选项:{RESET}")
    print(f"{colors[2]}1. 获取额度{RESET}")
    print(f"{colors[3]}2. 获取模型{RESET}")
    print(f"{colors[4]}3. 模型测试{RESET}")
    print(f"{colors[0]}0. 退出{RESET}")
    
    choice = input(f"{colors[3]}请输入选项 (1/2/3/0): {RESET}")
    
    if choice == '1':
        os.system('cls' if os.name == 'nt' else 'clear')
        get_balance()
        print()
        input(f"{colors[4]}回车返回菜单!!!{RESET}")
        menu()
    elif choice == '2':
        os.system('cls' if os.name == 'nt' else 'clear')
        models = get_available_models()
        categorized_models = categorize_and_color_models(models)
        print_categorized_models(categorized_models)
        print()
        input(f"{colors[4]}回车返回菜单!!!{RESET}")
        menu()
    elif choice == '3':
        os.system('cls' if os.name == 'nt' else 'clear')
        test_model()
        print()
        input(f"{colors[4]}回车返回菜单!!!{RESET}")
        menu()
    elif choice == '0':
        os.system('cls' if os.name == 'nt' else 'clear')
        exit()
    else:
        print(f"{colors[0]}无效的选择,请重新输入。{RESET}")
        menu()

if __name__ == "__main__":
    os.system('cls' if os.name == 'nt' else 'clear')
    menu()

英语版

import requests
import random
import os
from tabulate import tabulate
import datetime
import time

# ANSI font color codes (foreground only)
colors = [
    '\033[31m',  # Red
    '\033[32m',  # Green
    '\033[33m',  # Yellow
    '\033[34m',  # Blue
    '\033[35m',  # Magenta
    '\033[36m',  # Cyan
    '\033[37m',  # White
    '\033[90m',  # Grey
]

RESET = '\033[0m'

api_key = None
url = None

company_mapping = {
    "anthropic": {"English": "Anthropic"},
    "openai": {"English": "OpenAI"},
    "deepseek": {"English": "DeepSeek"},
    "doubao": {"English": "Doubao"},
    "google": {"English": "Google"},
    "zhipu": {"English": "Zhipu"},
    "Zhipu_4v": {"English": "Zhipu"},
    "midjourney": {"English": "Midjourney"},
    "moonshot": {"English": "Moonshot"},
    "alibaba": {"English": "Alibaba"},
    "iflytek": {"English": "iFlytek"},
    "baidu": {"English": "Baidu"},
    "tencent": {"English": "Tencent"},
    "microsoft": {"English": "Microsoft"},
    "huggingface": {"English": "Hugging Face"},
    "stabilityai": {"English": "Stability AI"},
    "cohere": {"English": "Cohere"},
    "ai21": {"English": "AI21 Labs"},
    "nvidia": {"English": "NVIDIA"},
    "apple": {"English": "Apple"},
    "salesforce": {"English": "Salesforce"},
    "xiaomi": {"English": "Xiaomi"},
    "meituan": {"English": "Meituan"},
    "jd": {"English": "JD"},
    "bytedance": {"English": "ByteDance"},
    "facebook": {"English": "Facebook"},
    "amazon": {"English": "Amazon"},
    "oracle": {"English": "Oracle"},
    "intel": {"English": "Intel"},
    "qualcomm": {"English": "Qualcomm"},
    "Custom": {"English": "Custom"}
}

def generate_random_colors(num_colors):
    random.shuffle(colors)
    return colors[:num_colors]

def categorize_and_color_models(models):
    categorized_models = {}
    company_names = {company_mapping.get(model['owned_by'].lower(), {"English": model['owned_by']})["English"] for model in models}
    company_colors = {company: color for company, color in zip(company_names, generate_random_colors(len(company_names)))}
    
    for model in models:
        model_name = model['id']
        owned_by = model.get('owned_by', 'unknown').lower()
        company_name = company_mapping.get(owned_by, {"English": owned_by})["English"]
        
        color = company_colors.get(company_name, RESET)
        colored_model = f"{color}{model_name}{RESET}"
        
        if company_name not in categorized_models:
            categorized_models[company_name] = []
        categorized_models[company_name].append(colored_model)
    
    return categorized_models

def print_categorized_models(categorized_models):
    table = []
    for company, models in categorized_models.items():
        table.append([f"\033[1m{company.capitalize()}\033[0m", "\n".join(models)])
    
    print(f"\033[34m{tabulate(table, headers=['Company', 'Model List'], tablefmt='grid')}{RESET}")

def get_available_models():
    try:
        response = requests.get(url + '/v1/models', headers={'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"})
        response.raise_for_status()
        return response.json().get('data', [])
    except requests.exceptions.RequestException as e:
        print(f"{colors[0]}Error getting models: {e}{RESET}")
        return []

def get_balance():
    headers = {'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"}

    subscription_url = "/v1/dashboard/billing/subscription"
    subscription_response = requests.get(url + subscription_url, headers=headers)
    if subscription_response.status_code == 200:
        data = subscription_response.json()
        total = data.get("hard_limit_usd")
    else:
        print(f"{colors[0]}{subscription_response.text}{RESET}")
        total = 0

    start_date = (datetime.datetime.now() - datetime.timedelta(days=99)).strftime("%Y-%m-%d")
    end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    billing_url = f"{url}/v1/dashboard/billing/usage?start_date={start_date}&end_date={end_date}"
    billing_response = requests.get(billing_url, headers=headers)
    if billing_response.status_code == 200:
        data = billing_response.json()
        total_usage = data.get("total_usage") / 100
    else:
        print(f"{colors[0]}{billing_response.text}{RESET}")
        total_usage = 0

    remaining = total - total_usage
    print(f"{colors[1]}Current account balance information:{RESET}")
    print(f"{colors[2]}Total:\t{total:.4f} USD{RESET}")
    print(f"{colors[3]}Used:\t{total_usage:.4f} USD{RESET}")
    print(f"{colors[4]}Remaining:\t{remaining:.4f} USD{RESET}")

def test_model():
    print(f"{colors[2]}Please enter the model to test (default is gpt-3.5-turbo), press Enter to use the default model{RESET}")
    model = input(f"{colors[3]}Model:{RESET}").strip() or "gpt-3.5-turbo"
    os.system('cls' if os.name == 'nt' else 'clear')
    data = {
        "model": model,
        "messages": [
            {"role": "user", "content": "say this is text!"}
        ]
    }
    print(f"{colors[1]}User calls model: {model}{RESET}")
    try:
        start_time = time.time()
        response = requests.post(url + "/v1/chat/completions", headers={'Authorization': f'Bearer {api_key}', "Content-Type": "application/json"}, json=data)
        end_time = time.time()
        response_time = end_time - start_time
        if response.status_code == 200:
            response_json = response.json()
            model_name = response_json.get('model', 'Unknown Model')
            content = response_json.get('choices', [{}])[0].get('message', {}).get('content', 'No Content')
            
            print(f"{colors[1]}Actual response model: {model_name}{RESET}")
            print(f"{colors[1]}Response time: {response_time:.2f} seconds{RESET}")
            print(f"{colors[1]}Response successful{RESET}")
            print(f"{colors[1]}Content: {content}{RESET}")
        else:
            print(f"{colors[0]}Request failed, status code: {response.status_code}{RESET}")
            print(f"{colors[0]}{response.text}{RESET}")

    except requests.exceptions.RequestException as e:
        print(f"{colors[0]}Error occurred while requesting: {e}{RESET}")

def get_user_credentials():
    global url, api_key
    url = input(f"{colors[2]}Please enter the API URL (e.g., https://api.openai.com):{RESET}").strip()
    api_key = input(f"{colors[2]}Please enter your API Key:{RESET}").strip()

def menu():
    os.system('cls' if os.name == 'nt' else 'clear')
    
    if not api_key or not url:
        get_user_credentials()
        os.system('cls' if os.name == 'nt' else 'clear')

    print(f"{colors[3]}Current API URL: {url}{RESET}")
    print(f"{colors[3]}Current API Key: {api_key}{RESET}")
    print()
    
    print(f"{colors[5]}Please select an option:{RESET}")
    print(f"{colors[2]}1. Get balance{RESET}")
    print(f"{colors[3]}2. Get models{RESET}")
    print(f"{colors[4]}3. Test model{RESET}")
    print(f"{colors[0]}0. Exit{RESET}")
    
    choice = input(f"{colors[3]}Please enter your choice (1/2/3/0): {RESET}")
    
    if choice == '1':
        os.system('cls' if os.name == 'nt' else 'clear')
        get_balance()
        print()
        input(f"{colors[4]}Press Enter to return to the menu!!!{RESET}")
        menu()
    elif choice == '2':
        os.system('cls' if os.name == 'nt' else 'clear')
        models = get_available_models()
        categorized_models = categorize_and_color_models(models)
        print_categorized_models(categorized_models)
        print()
        input(f"{colors[4]}Press Enter to return to the menu!!!{RESET}")
        menu()
    elif choice == '3':
        os.system('cls' if os.name == 'nt' else 'clear')
        test_model()
        print()
        input(f"{colors[4]}Press Enter to return to the menu!!!{RESET}")
        menu()
    elif choice == '0':
        os.system('cls' if os.name == 'nt' else 'clear')
        exit()
    else:
        print(f"{colors[0]}Invalid choice, please re-enter.{RESET}")
        menu()

if __name__ == "__main__":
    os.system('cls' if os.name == 'nt' else 'clear')
    menu()

有什么问题可以提我及时修复,部分中转站可能不适配,大部分都适配的!!!

20 个赞

前排~mark

1 个赞

好脚本, 学习了。 能不能研究个部署到 py.space 上的版本呢?

1 个赞

没用过py.space诶

1 个赞

感谢分享这个代码

当前账户余额信息:
总额: 100000000.0000美元
已用: 0.0000美元
剩余: 100000000.0000美元

这个不对啊

1 个赞

这个是设定了无限额度吧

1 个赞

实际上是10$

1 个赞

很有用,感谢大佬

1 个赞

牛的牛的 :smiling_face_with_three_hearts: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

令牌额度无限制会出现这种问题

后期我可能还会增加官key检测 使用fc
以及各种模型的调用检测,但基本会围绕openai的文档去完善
有特殊模型可以提供调用方式以及模型名之类的
识别是否伪造这一项等找到合适的方法了可能会添加

1 个赞

有想法的也可以提 共同完善,我晚点把这个上传到github上面

哥哥威武,很实用的功能呢

支持,感谢大佬的分享! :smiling_face_with_three_hearts:

dalao nb.

两种语言的版本我也拆分出来了,小白也看的更直观了

运行代码就报错:

E:\software\anaconda3\python.exe "D:/software/pycharm/PyCharm 2023.2.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path F:\shenduxuexi\pycharm-01project\api\一键查询所有的api余额\select_keys.py 
Testing started at 16:31 ...
Launching pytest with arguments F:\shenduxuexi\pycharm-01project\api\一键查询所有的api余额\select_keys.py --no-header --no-summary -q in F:\shenduxuexi\pycharm-01project\api\一键查询所有的api余额

============================= test session starts =============================
collecting ... collected 1 item

select_keys.py::test_model FAILED                                        [100%]请输入测试模型(默认gpt-3.5-turbo),直接回车则使用默认模型
模型:
select_keys.py:217 (test_model)
def test_model():
        print(f"{colors[2]}{language_mapping[language]['test_model_prompt']}{RESET}")
>       model = input(f"{colors[3]}{language_mapping[language]['model_input']}{RESET}").strip() or "gpt-3.5-turbo"

select_keys.py:220: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_pytest.capture.DontReadFromInput object at 0x000001CFB12E18E0>
args = ()

    def read(self, *args):
>       raise OSError(
            "pytest: reading from stdin while output is captured!  Consider using `-s`."
        )
E       OSError: pytest: reading from stdin while output is captured!  Consider using `-s`.

E:\software\anaconda3\lib\site-packages\_pytest\capture.py:217: OSError


======================== 1 failed, 1 warning in 0.46s =========================

Process finished with exit code 1

啥系统啊 我能运行诶

windows