# 直接调试API密钥比较问题 import os from dotenv import load_dotenv # 加载环境变量 load_dotenv() # 从.env获取的密钥 api_key_from_env = os.getenv('AI_API_KEY', '') # 测试用的密钥(与前端和测试脚本中使用的相同) test_api_key = 'sk-0944c102849e45d9ab3f24d5169de289' print(f"API Key from .env: '{api_key_from_env}'") print(f"Test API Key: '{test_api_key}'") print(f"Match: {api_key_from_env == test_api_key}") # 检查长度 print(f"\nLength from .env: {len(api_key_from_env)}") print(f"Length test key: {len(test_api_key)}") # 检查不可见字符 print(f"\nHex from .env: {[hex(ord(c)) for c in api_key_from_env]}") print(f"Hex test key: {[hex(ord(c)) for c in test_api_key]}") # 检查是否有前后空格 print(f"\nStrip from .env: '{api_key_from_env.strip()}'") print(f"Strip test key: '{test_api_key.strip()}'") print(f"Match after strip: {api_key_from_env.strip() == test_api_key.strip()}") # 检查是否有换行符或回车符 print(f"\nHas newline from .env: {'\n' in api_key_from_env}") print(f"Has newline test key: {'\n' in test_api_key}") print(f"Has carriage return from .env: {'\r' in api_key_from_env}") print(f"Has carriage return test key: {'\r' in test_api_key}") # 清除所有空白字符后比较 import re clean_env_key = re.sub(r'\s', '', api_key_from_env) clean_test_key = re.sub(r'\s', '', test_api_key) print(f"\nClean from .env: '{clean_env_key}'") print(f"Clean test key: '{clean_test_key}'") print(f"Match after clean: {clean_env_key == clean_test_key}")