|
|
|
|
|
""" |
|
|
Quick diagnostic script to check CogniChat configuration and identify issues. |
|
|
""" |
|
|
import os |
|
|
import sys |
|
|
from pathlib import Path |
|
|
|
|
|
print("=== CogniChat Diagnostic Tool ===") |
|
|
print() |
|
|
|
|
|
|
|
|
current_dir = Path.cwd() |
|
|
print(f"Current directory: {current_dir}") |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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 ===") |