$answer) { $formatted_answers[] = "Q: " . $questions[$index] . "\nA: " . $answer; } $prompt = <<<'EOD' You are a burnout analysis expert. A user has completed a burnout survey. The user answered on a scale of 1-5 where 1 is 'Never' and 5 is 'Always'. Analyze the following survey answers and provide a detailed analysis. Return the response as a JSON object. The JSON object must have the following keys: "scores", "analysis", "recommendations", and "nextSteps". - "scores" should be an object with keys "Exhaustion", "Cynicism", and "Inefficacy", each with a numeric score from 0 to 5. - "analysis" should be an object with keys "overallSummary", "exhaustionSummary", "cynicismSummary", and "inefficacySummary", each containing a string with the corresponding analysis. - "recommendations" should be an array of objects, where each object has a "title" and a "description". - "nextSteps" should be an array of strings. Answers: EOD; $prompt .= implode("\n\n", $formatted_answers); $ai_response = LocalAIApi::createResponse([ 'input' => [ ['role' => 'system', 'content' => 'You are a burnout analysis expert.'], ['role' => 'user', 'content' => $prompt], ], ]); if (!empty($ai_response['success'])) { $decoded_response = LocalAIApi::decodeJsonFromResponse($ai_response); $_SESSION['results'] = $decoded_response; if (isset($_SESSION['user_id'])) { try { $pdo = db(); $stmt = $pdo->prepare('INSERT INTO survey_results (user_id, results_json) VALUES (:user_id, :results_json)'); $stmt->execute([ ':user_id' => $_SESSION['user_id'], ':results_json' => json_encode($decoded_response) ]); } catch (PDOException $e) { // Optionally handle or log the database error } } } else { // Handle AI API error, maybe provide default results $_SESSION['results'] = [ 'scores' => ['Exhaustion' => 0, 'Cynicism' => 0, 'Inefficacy' => 0], 'recommendations' => ['Could not analyze results at this time. Please try again later.'] ]; } $response['redirect'] = 'results.php'; } echo json_encode($response);