File size: 4,039 Bytes
becc8f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""
Quick diagnostic script to check CogniChat configuration and identify issues.
"""
import os
import sys
from pathlib import Path

print("=== CogniChat Diagnostic Tool ===")
print()

# Check if we're in the right directory
current_dir = Path.cwd()
print(f"Current directory: {current_dir}")

# Check for required files
required_files = [
    'app.py',
    'rag_processor.py',
    'requirements.txt',
    '.env.example'
]

missing_files = []
for file in required_files:
    if (current_dir / file).exists():
        print(f"βœ“ Found: {file}")
    else:
        print(f"βœ— Missing: {file}")
        missing_files.append(file)

print()

# Check .env file
env_file = current_dir / '.env'
if env_file.exists():
    print("βœ“ .env file exists")
    try:
        with open(env_file, 'r') as f:
            content = f.read()
        
        if 'GROQ_API_KEY=' in content:
            if 'your_groq_api_key_here' in content:
                print("⚠ .env file contains placeholder API key - needs to be updated!")
            else:
                print("βœ“ GROQ_API_KEY appears to be set in .env")
        else:
            print("βœ— GROQ_API_KEY not found in .env file")
    except Exception as e:
        print(f"βœ— Error reading .env file: {e}")
else:
    print("βœ— .env file missing - copy from .env.example and update with your API key")

print()

# Check environment variables and detect HF Spaces
is_hf_spaces = bool(os.getenv("SPACE_ID") or os.getenv("SPACES_ZERO_GPU"))
print(f"Environment: {'Hugging Face Spaces' if is_hf_spaces else 'Local Development'}")

if is_hf_spaces:
    print(f"Space ID: {os.getenv('SPACE_ID', 'Not detected')}")

groq_key = os.getenv('GROQ_API_KEY')
if groq_key:
    if groq_key == 'your_groq_api_key_here':
        print("⚠ GROQ_API_KEY is set but contains placeholder value")
    else:
        print("βœ“ GROQ_API_KEY environment variable is set")
        if is_hf_spaces:
            print("  (Loaded from Hugging Face Spaces secrets)")
else:
    print("βœ— GROQ_API_KEY environment variable not set")

print()

# Load dotenv if available
try:
    from dotenv import load_dotenv
    load_dotenv()
    groq_key_after_dotenv = os.getenv('GROQ_API_KEY')
    
    if groq_key_after_dotenv:
        if groq_key_after_dotenv == 'your_groq_api_key_here':
            print("⚠ After loading .env: GROQ_API_KEY still contains placeholder")
        else:
            print("βœ“ After loading .env: GROQ_API_KEY is properly set")
    else:
        print("βœ— After loading .env: GROQ_API_KEY still not available")
        
except ImportError:
    print("βœ— python-dotenv not available - install with: pip install python-dotenv")

print()

# Recommendations
print("=== Recommendations ===")

if missing_files:
    print("1. Ensure you're in the correct CogniChat directory")

if is_hf_spaces:
    if not groq_key or groq_key == 'your_groq_api_key_here':
        print("2. FOR HUGGING FACE SPACES - Set API key in Space Secrets:")
        print("   - Go to your Space Settings")
        print("   - Navigate to 'Repository Secrets'")
        print("   - Add new secret: GROQ_API_KEY")
        print("   - Get your key from: https://console.groq.com/keys")
        print("   - Restart your Space after adding the secret")
else:
    if not env_file.exists():
        print("2. Copy .env.example to .env:")
        print("   cp .env.example .env")

    if env_file.exists() and 'your_groq_api_key_here' in env_file.read_text():
        print("3. Update .env file with your actual GROQ API key:")
        print("   - Visit: https://console.groq.com/keys")
        print("   - Create an API key")
        print("   - Replace 'your_groq_api_key_here' in .env with your key")

if not groq_key or groq_key == 'your_groq_api_key_here':
    print("4. The main issue is likely the GROQ API key configuration")
    print("   This would cause the 400 error you're seeing in /chat endpoint")

print("\n5. After fixing the API key, restart the application")
print("\n=== End of Diagnostic ===")