|
|
|
|
|
""" |
|
|
Environment setup script for OpenAI API configuration |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
|
|
|
|
|
|
def check_openai_config(): |
|
|
"""Check current OpenAI configuration""" |
|
|
print("🔍 检查当前OpenAI配置") |
|
|
print("-" * 40) |
|
|
|
|
|
api_key = os.getenv("OPENAI_API_KEY") |
|
|
base_url = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1") |
|
|
model = os.getenv("OPENAI_MODEL", "gpt-4o-mini") |
|
|
|
|
|
print(f"🔑 OPENAI_API_KEY: {'✅ 已设置' if api_key else '❌ 未设置'}") |
|
|
print(f"🌐 OPENAI_BASE_URL: {base_url}") |
|
|
print(f"🤖 OPENAI_MODEL: {model}") |
|
|
|
|
|
if api_key: |
|
|
|
|
|
masked_key = f"{api_key[:4]}...{api_key[-4:]}" if len(api_key) > 8 else "***" |
|
|
print(f" (API Key: {masked_key})") |
|
|
return True |
|
|
else: |
|
|
return False |
|
|
|
|
|
|
|
|
def show_setup_guide(): |
|
|
"""显示设置指南""" |
|
|
print("\n🚀 OpenAI API 设置指南") |
|
|
print("=" * 50) |
|
|
|
|
|
print("1. 获取OpenAI API Key:") |
|
|
print(" • 访问: https://platform.openai.com/api-keys") |
|
|
print(" • 登录或注册OpenAI账户") |
|
|
print(" • 点击 'Create new secret key'") |
|
|
print(" • 复制生成的API key") |
|
|
|
|
|
print("\n2. 设置环境变量:") |
|
|
if sys.platform.startswith('win'): |
|
|
print(" Windows (Command Prompt):") |
|
|
print(" set OPENAI_API_KEY=your_api_key_here") |
|
|
print(" Windows (PowerShell):") |
|
|
print(" $env:OPENAI_API_KEY=\"your_api_key_here\"") |
|
|
else: |
|
|
print(" macOS/Linux:") |
|
|
print(" export OPENAI_API_KEY=your_api_key_here") |
|
|
|
|
|
print("\n3. 可选配置:") |
|
|
print(" • 自定义API基础URL (如果使用代理或其他OpenAI兼容服务):") |
|
|
if sys.platform.startswith('win'): |
|
|
print(" set OPENAI_BASE_URL=https://your-proxy.com/v1") |
|
|
else: |
|
|
print(" export OPENAI_BASE_URL=https://your-proxy.com/v1") |
|
|
|
|
|
print(" • 自定义模型 (默认: gpt-4o-mini):") |
|
|
if sys.platform.startswith('win'): |
|
|
print(" set OPENAI_MODEL=gpt-3.5-turbo") |
|
|
else: |
|
|
print(" export OPENAI_MODEL=gpt-3.5-turbo") |
|
|
|
|
|
print("\n4. 重启应用:") |
|
|
print(" python app.py") |
|
|
|
|
|
|
|
|
def show_cost_info(): |
|
|
"""显示费用信息""" |
|
|
print("\n💰 OpenAI API 费用说明") |
|
|
print("=" * 30) |
|
|
print("• gpt-4o-mini (推荐):") |
|
|
print(" - 输入: $0.15/1M tokens") |
|
|
print(" - 输出: $0.60/1M tokens") |
|
|
print("• gpt-3.5-turbo:") |
|
|
print(" - 输入: $0.50/1M tokens") |
|
|
print(" - 输出: $1.50/1M tokens") |
|
|
print("• gpt-4:") |
|
|
print(" - 输入: $30.00/1M tokens") |
|
|
print(" - 输出: $60.00/1M tokens") |
|
|
|
|
|
print("\n📊 预估使用量:") |
|
|
print("• 每次challenge评分查询: ~100-200 tokens") |
|
|
print("• 每次计划生成: ~200-300 tokens") |
|
|
print("• 预计每1000次查询成本: $0.02-0.10 (使用gpt-4o-mini)") |
|
|
|
|
|
|
|
|
def show_troubleshooting(): |
|
|
"""显示故障排除指南""" |
|
|
print("\n🔧 故障排除") |
|
|
print("=" * 20) |
|
|
print("如果遇到问题:") |
|
|
print("1. 确认API key正确设置且有效") |
|
|
print("2. 检查网络连接") |
|
|
print("3. 确认OpenAI账户有足够余额") |
|
|
print("4. 如果使用代理,确认OPENAI_BASE_URL正确") |
|
|
print("5. 查看应用日志中的详细错误信息") |
|
|
|
|
|
|
|
|
def create_env_file(): |
|
|
"""创建.env文件示例""" |
|
|
env_content = """# OpenAI API Configuration |
|
|
# 在此处设置您的OpenAI API Key |
|
|
OPENAI_API_KEY=your_api_key_here |
|
|
|
|
|
# 可选: 自定义API基础URL (用于代理或其他兼容服务) |
|
|
# OPENAI_BASE_URL=https://api.openai.com/v1 |
|
|
|
|
|
# 可选: 自定义模型 (默认: gpt-4o-mini) |
|
|
# OPENAI_MODEL=gpt-4o-mini |
|
|
""" |
|
|
|
|
|
try: |
|
|
with open('.env.example', 'w', encoding='utf-8') as f: |
|
|
f.write(env_content) |
|
|
print("✅ 已创建 .env.example 文件") |
|
|
print(" 请复制为 .env 并填入您的API key") |
|
|
except Exception as e: |
|
|
print(f"❌ 创建 .env.example 失败: {e}") |
|
|
|
|
|
|
|
|
def main(): |
|
|
print("🤖 AI Agent - OpenAI API 配置助手") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
has_api_key = check_openai_config() |
|
|
|
|
|
if has_api_key: |
|
|
print("\n✅ OpenAI API 已配置完成!") |
|
|
print("您可以直接运行应用: python app.py") |
|
|
else: |
|
|
print("\n❌ OpenAI API 未配置") |
|
|
show_setup_guide() |
|
|
|
|
|
|
|
|
show_cost_info() |
|
|
|
|
|
|
|
|
print("\n📝 创建配置文件示例") |
|
|
print("-" * 30) |
|
|
create_env_file() |
|
|
|
|
|
|
|
|
show_troubleshooting() |
|
|
|
|
|
print("\n" + "=" * 50) |
|
|
print("配置完成后,请运行: python app.py") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|