πŸ” Debug GPT Usage"; echo ""; try { $auth = new Auth(); if (!$auth->isLoggedIn()) { die("
❌ Please log in first
"); } $db = Database::getInstance(); // Test 1: Check API Key Configuration echo "
"; echo "

πŸ”‘ API Key Configuration

"; if (defined('OPENAI_API_KEY')) { $keyLength = strlen(OPENAI_API_KEY); $keyPreview = substr(OPENAI_API_KEY, 0, 7) . '...' . substr(OPENAI_API_KEY, -4); echo "
βœ… OPENAI_API_KEY is defined
"; echo "
Key length: $keyLength characters
"; echo "
Key preview: $keyPreview
"; if ($keyLength < 40) { echo "
⚠️ API key seems too short - check if it's complete
"; } } else { echo "
❌ OPENAI_API_KEY is not defined
"; } echo "
"; // Test 2: Check GptHelper Class echo "
"; echo "

πŸ€– GptHelper Class

"; if (!class_exists('GptHelper')) { require_once 'includes/GptHelper.php'; } if (class_exists('GptHelper')) { echo "
βœ… GptHelper class loaded
"; // Check if methods exist if (method_exists('GptHelper', 'makeRequest')) { echo "
βœ… makeRequest method exists
"; } else { echo "
❌ makeRequest method missing
"; } if (method_exists('GptHelper', 'canMakeRequest')) { echo "
βœ… canMakeRequest method exists
"; } else { echo "
❌ canMakeRequest method missing
"; } } else { echo "
❌ GptHelper class not found
"; } echo "
"; // Test 3: Check Recent Database Records echo "
"; echo "

πŸ“Š Recent Database Analysis

"; $recentQuery = $db->query(" SELECT id, attribute1_name, choice1, attribute2_name, choice2, llm_checked, is_impossible, llm_reasoning, updated_at FROM panel_directives WHERE llm_checked = 1 ORDER BY updated_at DESC LIMIT 5 "); if ($recentQuery && $recentQuery->num_rows > 0) { echo "
βœ… Found recent processed combinations:
"; echo ""; echo ""; while ($row = $recentQuery->fetch_assoc()) { $result = $row['is_impossible'] ? 'IMPOSSIBLE' : 'POSSIBLE'; $reasoning = substr($row['llm_reasoning'], 0, 100) . '...'; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
CombinationResultReasoningUpdated
{$row['attribute1_name']}={$row['choice1']} + {$row['attribute2_name']}={$row['choice2']}$result" . htmlspecialchars($reasoning) . "{$row['updated_at']}
"; // Check if reasoning looks like GPT output $recentQuery->data_seek(0); $hasDetailedReasoning = false; while ($row = $recentQuery->fetch_assoc()) { if (strlen($row['llm_reasoning']) > 20 && (strpos($row['llm_reasoning'], 'combination') !== false || strpos($row['llm_reasoning'], 'possible') !== false || strpos($row['llm_reasoning'], 'demographic') !== false)) { $hasDetailedReasoning = true; break; } } if ($hasDetailedReasoning) { echo "
βœ… Reasoning appears to be from GPT (detailed explanations)
"; } else { echo "
⚠️ Reasoning appears simple - might not be from GPT
"; } } else { echo "
⚠️ No recent processed combinations found
"; } echo "
"; // Test 4: Live GPT Test echo "
"; echo "

πŸ§ͺ Live GPT API Test

"; echo ""; echo "
"; echo "
"; // Test 5: Check Processing Logs echo "
"; echo "

πŸ“‹ Check for API Call Logs

"; // Check if any error logs mention OpenAI if (file_exists('error_log')) { $errorLog = file_get_contents('error_log'); $openaiMentions = substr_count(strtolower($errorLog), 'openai'); $apiMentions = substr_count(strtolower($errorLog), 'api'); $gptMentions = substr_count(strtolower($errorLog), 'gpt'); echo "
Error log mentions: OpenAI ($openaiMentions), API ($apiMentions), GPT ($gptMentions)
"; if ($openaiMentions > 0) { echo "
⚠️ OpenAI mentioned in error log - check for API issues
"; } } echo "
"; // Test 6: Manual Processing Test echo "
"; echo "

πŸ”§ Manual Processing Test

"; echo ""; echo "
"; echo "
"; // Test 7: Possible Issues echo "
"; echo "

🚨 Possible Issues

"; $issues = []; // Check if using cached/pre-existing data $totalProcessed = $db->query("SELECT COUNT(*) as count FROM panel_directives WHERE llm_checked = 1")->fetch_assoc()['count']; $recentProcessed = $db->query("SELECT COUNT(*) as count FROM panel_directives WHERE llm_checked = 1 AND updated_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)")->fetch_assoc()['count']; if ($totalProcessed > 0 && $recentProcessed == 0) { $issues[] = "❓ All processed combinations are old - might be using cached data"; } // Check API key format if (defined('OPENAI_API_KEY') && !str_starts_with(OPENAI_API_KEY, 'sk-')) { $issues[] = "❌ API key doesn't start with 'sk-' - invalid format"; } // Check if rate limiting is preventing requests if (class_exists('GptHelper') && method_exists('GptHelper', 'canMakeRequest')) { if (!GptHelper::canMakeRequest()) { $issues[] = "⏸️ Rate limiting is active - requests may be blocked"; } } if (empty($issues)) { echo "
βœ… No obvious issues detected
"; } else { foreach ($issues as $issue) { echo "
$issue
"; } } echo "
"; } catch (Exception $e) { echo "
❌ Error: " . htmlspecialchars($e->getMessage()) . "
"; } ?> 'user', 'content' => 'Respond with exactly: "GPT API test successful at ' . date('H:i:s') . '"' ] ]; $response = GptHelper::makeRequest($messages, 'gpt-4', 0.1); if ($response['success']) { echo json_encode([ 'success' => true, 'response' => $response['response'], 'model' => 'gpt-4', 'usage' => $response['usage'] ?? ['prompt_tokens' => 'unknown', 'completion_tokens' => 'unknown'] ]); } else { echo json_encode([ 'success' => false, 'error' => $response['error'] ]); } } catch (Exception $e) { echo json_encode([ 'success' => false, 'error' => $e->getMessage() ]); } exit; } if ($_GET['action'] === 'manual_process_test') { try { if (!class_exists('GptHelper')) { require_once 'includes/GptHelper.php'; } // Get one unprocessed combination $query = $db->query(" SELECT id, attribute1_name, choice1, attribute2_name, choice2 FROM panel_directives WHERE llm_checked = 0 LIMIT 1 "); if (!$query || $query->num_rows == 0) { throw new Exception('No unprocessed combinations found'); } $combo = $query->fetch_assoc(); $prompt = "Analyze: {$combo['attribute1_name']}={$combo['choice1']} + {$combo['attribute2_name']}={$combo['choice2']}. Respond with POSSIBLE or IMPOSSIBLE and brief reason."; $messages = [ [ 'role' => 'system', 'content' => 'You are a demographic analyst.' ], [ 'role' => 'user', 'content' => $prompt ] ]; $response = GptHelper::makeRequest($messages, 'gpt-4', 0.1); if ($response['success']) { $content = trim($response['response']); $isImpossible = stripos($content, 'IMPOSSIBLE') === 0 ? 1 : 0; $reasoning = $content; // Update database $stmt = $db->prepare("UPDATE panel_directives SET llm_checked = 1, is_impossible = ?, llm_reasoning = ?, updated_at = NOW() WHERE id = ?"); $stmt->bind_param('isi', $isImpossible, $reasoning, $combo['id']); $stmt->execute(); echo json_encode([ 'success' => true, 'combination' => "{$combo['attribute1_name']}={$combo['choice1']} + {$combo['attribute2_name']}={$combo['choice2']}", 'result' => $isImpossible ? 'IMPOSSIBLE' : 'POSSIBLE', 'reasoning' => $reasoning, 'api_called' => true ]); } else { echo json_encode([ 'success' => false, 'error' => $response['error'], 'api_called' => false ]); } } catch (Exception $e) { echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'api_called' => false ]); } exit; } } ?>