Test Single Combination Analysis";
echo "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$attr1 = $_POST['attr1'] ?? '';
$choice1 = $_POST['choice1'] ?? '';
$attr2 = $_POST['attr2'] ?? '';
$choice2 = $_POST['choice2'] ?? '';
if ($attr1 && $choice1 && $attr2 && $choice2) {
echo "
Testing Combination
";
echo "Analyzing: $attr1 = $choice1 + $attr2 = $choice2
";
try {
$result = analyzeAttributeCombination($attr1, $choice1, $attr2, $choice2);
echo "";
echo "
Result: " . ($result['is_impossible'] ? 'IMPOSSIBLE' : 'POSSIBLE') . "
";
echo "
Reasoning: " . htmlspecialchars($result['reasoning']) . "
";
echo "
";
echo "Raw GPT Response:
";
echo "" . htmlspecialchars($result['raw_response']) . "
";
} catch (Exception $e) {
echo "";
echo "
Error
";
echo "
" . htmlspecialchars($e->getMessage()) . "
";
echo "
";
}
}
}
// Get some sample combinations for testing
$sampleQuery = $db->query("
SELECT attribute1_name, choice1, attribute2_name, choice2
FROM panel_directives
WHERE llm_checked = 0
LIMIT 5
");
echo "Manual Test Form
";
echo "";
if ($sampleQuery && $sampleQuery->num_rows > 0) {
echo "Sample Combinations from Database:
";
echo "Click to test:
";
while ($sample = $sampleQuery->fetch_assoc()) {
$onclick = "document.querySelector('input[name=\"attr1\"]').value='" . addslashes($sample['attribute1_name']) . "'; ";
$onclick .= "document.querySelector('input[name=\"choice1\"]').value='" . addslashes($sample['choice1']) . "'; ";
$onclick .= "document.querySelector('input[name=\"attr2\"]').value='" . addslashes($sample['attribute2_name']) . "'; ";
$onclick .= "document.querySelector('input[name=\"choice2\"]').value='" . addslashes($sample['choice2']) . "';";
echo "
";
}
}
function analyzeAttributeCombination($attr1Name, $choice1, $attr2Name, $choice2) {
$prompt = "Analyze this demographic combination for real-world possibility:
Attribute 1: {$attr1Name}
Choice 1: {$choice1}
Attribute 2: {$attr2Name}
Choice 2: {$choice2}
Question: Is it realistically possible for a person to have both '{$choice1}' for {$attr1Name} AND '{$choice2}' for {$attr2Name} simultaneously in the real world?
Consider factors like:
- Biological/physical constraints (age, gender, physical abilities)
- Legal/social constraints (marriage laws, education requirements)
- Logical impossibilities (contradictory states)
- Age-related restrictions (employment, marriage, education)
- Geographic/cultural factors
Respond with:
1. POSSIBLE or IMPOSSIBLE
2. Brief reasoning (1-2 sentences explaining why)
Format your response as:
VERDICT: [POSSIBLE/IMPOSSIBLE]
REASON: [Your reasoning]";
$messages = [
[
"role" => "system",
"content" => "You are a demographic analysis expert. Analyze attribute combinations for real-world possibility with practical reasoning. Be conservative - only mark as IMPOSSIBLE if truly incompatible in real world."
],
[
"role" => "user",
"content" => $prompt
]
];
$response = GptHelper::makeRequest($messages, 'gpt-4', 0.3);
if (!$response['success']) {
throw new Exception($response['error'] ?? 'LLM request failed');
}
$content = $response['response'];
// Parse response
$isImpossible = false;
$reasoning = "Unable to parse LLM response";
if (preg_match('/VERDICT:\s*(POSSIBLE|IMPOSSIBLE)/i', $content, $matches)) {
$verdict = strtoupper($matches[1]);
$isImpossible = ($verdict === 'IMPOSSIBLE');
}
if (preg_match('/REASON:\s*(.+?)(?=\n\n|\n*$)/s', $content, $matches)) {
$reasoning = trim($matches[1]);
}
return [
'is_impossible' => $isImpossible,
'reasoning' => $reasoning,
'raw_response' => $content
];
}
?>