#!/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 ===")