chatbot_server / support /get_key.py
kltn21110's picture
Upload 40 files
e5429bd verified
import random
import os
def get_random_api_key(file_name='api_key.txt'):
try:
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.abspath(os.path.join(current_dir, '..')) # lên 1 cấp từ support/
file_path = os.path.join(project_root, file_name)
with open(file_path, 'r', encoding='utf-8') as f:
keys = [line.strip() for line in f if line.strip()]
if not keys:
raise ValueError("File không chứa key hợp lệ.")
return random.choice(keys)
except FileNotFoundError:
raise FileNotFoundError(f"Không tìm thấy file: {file_path}")
except Exception as e:
raise Exception(f"Lỗi khi đọc file: {e}")