-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_api_key.py
More file actions
72 lines (65 loc) · 2.57 KB
/
Copy pathcheck_api_key.py
File metadata and controls
72 lines (65 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Helper script to check and configure OpenAI API key.
"""
import os
import sys
# Set UTF-8 encoding for Windows
if sys.platform == 'win32':
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# Try to load .env file
try:
from dotenv import load_dotenv
load_dotenv()
print("[OK] Loaded .env file")
except ImportError:
print("[INFO] python-dotenv not installed. Install with: pip install python-dotenv")
except Exception as e:
print(f"[INFO] Could not load .env file: {e}")
# Check for API key in various places
api_key = None
sources_checked = []
# Check environment variable
api_key = os.getenv("OPENAI_API_KEY")
if api_key:
sources_checked.append("Environment variable (OPENAI_API_KEY)")
print(f"[OK] Found API key in environment variable")
print(f" Key starts with: {api_key[:10]}...")
else:
print("[X] No API key found in environment variable")
# Check .env file directly
try:
env_path = os.path.join(os.path.dirname(__file__), ".env")
if os.path.exists(env_path):
with open(env_path, 'r') as f:
for line in f:
if line.startswith("OPENAI_API_KEY="):
key_value = line.split("=", 1)[1].strip().strip('"').strip("'")
if key_value and key_value != "your-api-key-here":
api_key = key_value
sources_checked.append(".env file")
print(f"[OK] Found API key in .env file")
print(f" Key starts with: {key_value[:10]}...")
break
except Exception as e:
print(f"[INFO] Could not read .env file: {e}")
print("\n" + "="*60)
if api_key:
print("[SUCCESS] API KEY FOUND")
print(f" Source: {', '.join(sources_checked)}")
print(f" Key preview: {api_key[:15]}...{api_key[-4:]}")
print("\nYou're all set! AI features should work.")
else:
print("[WARNING] NO API KEY FOUND")
print("\nTo set up your API key, choose one of these options:")
print("\n1. Create a .env file:")
print(" Create a file named '.env' in this directory with:")
print(" OPENAI_API_KEY=your-actual-api-key-here")
print("\n2. Set environment variable (PowerShell):")
print(" $env:OPENAI_API_KEY='your-actual-api-key-here'")
print("\n3. Set environment variable (CMD):")
print(" set OPENAI_API_KEY=your-actual-api-key-here")
print("\nGet your API key from: https://platform.openai.com/api-keys")
print("\nNote: The system will work without AI (using pattern matching)")
print(" if no API key is found.")
print("="*60)