isLoggedIn()) {
die('Unauthorized');
}
$selection_id = isset($_GET['selection_id']) ? (int)$_GET['selection_id'] : 29;
echo "
Test: Single Response Generation for Selection $selection_id
";
try {
$db = Database::getInstance();
// Get one member from the selection
$stmt = $db->prepare("
SELECT sm.panelist_id, pd.attribute_values
FROM selection_members sm
LEFT JOIN panel_data pd ON sm.panelist_id = pd.panelist_id
WHERE sm.selection_id = ?
LIMIT 1
");
$stmt->bind_param('i', $selection_id);
$stmt->execute();
$member = $stmt->get_result()->fetch_assoc();
if (!$member) {
die("No member found for selection $selection_id");
}
echo "Member: " . htmlspecialchars($member['panelist_id']) . "
";
// Get attributes
$attributes = [];
$attr_query = $db->query("SELECT id, name FROM attributes ORDER BY created_at ASC");
while ($attr = $attr_query->fetch_assoc()) {
$attributes[$attr['id']] = $attr['name'];
}
// Parse member profile
$attribute_values = json_decode($member['attribute_values'], true);
$profile_data = [];
foreach ($attribute_values as $attr_id => $value) {
if (isset($attributes[$attr_id])) {
if (is_array($value)) {
$profile_data[$attributes[$attr_id]] = implode(', ', $value);
} else {
$profile_data[$attributes[$attr_id]] = $value;
}
}
}
echo "Profile:
";
echo "";
foreach ($profile_data as $attr => $value) {
echo "- $attr: $value
";
}
echo "
";
// Get a few questions
$stmt = $db->prepare("
SELECT sq.id, sq.question_text, sq.question_type, sq.options, sq.config
FROM selections s
JOIN projects p ON s.project_id = p.id
JOIN project_surveys ps ON p.id = ps.project_id
JOIN survey_questions sq ON ps.survey_id = sq.survey_id
WHERE s.id = ? AND sq.question_type NOT IN ('section_header', 'descriptive_text', 'page_break')
ORDER BY sq.question_order ASC
LIMIT 5
");
$stmt->bind_param('i', $selection_id);
$stmt->execute();
$questions = [];
while ($q = $stmt->get_result()->fetch_assoc()) {
if ($q['options']) {
$q['options'] = json_decode($q['options'], true) ?? [];
} else {
$q['options'] = [];
}
if ($q['config']) {
$q['config'] = json_decode($q['config'], true) ?? [];
} else {
$q['config'] = [];
}
$questions[] = $q;
}
if (empty($questions)) {
die("No questions found");
}
echo "Questions (" . count($questions) . "):
";
foreach ($questions as $i => $q) {
echo "Q" . ($i+1) . ": " . htmlspecialchars($q['question_text']) . "
";
if (!empty($q['options'])) {
echo "Options: " . implode(', ', $q['options']) . "
";
}
}
// Create prompt
$prompt = "You are a survey respondent with this demographic profile:\n";
foreach ($profile_data as $attr => $value) {
$prompt .= "• $attr: $value\n";
}
$prompt .= "\nRespond to each question below as this person would. Give ONLY your direct answer for each question - no explanations or extra text.\n\n";
foreach ($questions as $i => $question) {
$q_num = $i + 1;
$prompt .= "$q_num. " . $question['question_text'] . "\n";
if ($question['question_type'] == 'single_choice' || $question['question_type'] == 'dropdown') {
if (!empty($question['options'])) {
$prompt .= " Choose from: " . implode(" | ", $question['options']) . "\n";
}
}
}
$prompt .= "\nIMPORTANT: Answer in this exact format:\n";
$prompt .= "1. [Your answer to question 1]\n";
$prompt .= "2. [Your answer to question 2]\n";
$prompt .= "And so on. Give only the answer, nothing else.";
echo "GPT Prompt:
";
echo "" . htmlspecialchars($prompt) . "
";
// Call GPT
echo "Calling GPT...
";
$messages = [
['role' => 'system', 'content' => 'You are a survey respondent. You must respond to exactly ' . count($questions) . ' questions. Format each response as: "1. [answer]", "2. [answer]", etc. Give only direct answers - no explanations, introductions, or extra text.'],
['role' => 'user', 'content' => $prompt]
];
$gpt_response = GptHelper::makeRequest($messages);
echo "GPT Response:
";
echo "" . htmlspecialchars($gpt_response) . "
";
// Parse response
if (is_array($gpt_response)) {
if (isset($gpt_response['content'])) {
$gpt_response = $gpt_response['content'];
} else {
$gpt_response = json_encode($gpt_response);
}
}
$response_lines = explode("\n", trim($gpt_response));
$responses = [];
foreach ($response_lines as $i => $line) {
$line = trim($line);
if (empty($line)) continue;
$line = preg_replace('/^(\d+[\.\)]\s*|[\*\-\+]\s*|[A-Za-z][\.\)]\s*)/', '', $line);
if (!empty($line) && strlen($line) > 1) {
$responses[] = $line;
}
}
echo "Parsed Responses (" . count($responses) . "):
";
foreach ($responses as $i => $response) {
echo "A" . ($i+1) . ": " . htmlspecialchars($response) . "
";
}
if (count($responses) < count($questions)) {
echo "Warning: Only got " . count($responses) . " responses for " . count($questions) . " questions!
";
} else {
echo "Success: Got responses for all questions!
";
}
} catch (Exception $e) {
echo "Error: " . htmlspecialchars($e->getMessage()) . "
";
}
echo "
";
echo "Back to Debug | ";
echo "Re-generate All Responses
";
?>