curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_TIMEOUT, 2); // Set a timeout of 2 seconds curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $httpCode === 200; } $response = null; try { if ($_SERVER['REQUEST_METHOD'] === 'POST') { $userMessage = trim($_POST['message'] ?? ''); if (empty($userMessage)) { throw new Exception("Message cannot be empty."); } $_SESSION['chat_history'][] = ['sender' => 'User', 'message' => $userMessage]; $aiPrompt = "Chat with the user to understand their English level. Respond naturally and keep the conversation going. Do not evaluate yet.\n\nConversation:\n"; foreach ($_SESSION['chat_history'] as $chat) { $aiPrompt .= "{$chat['sender']}: {$chat['message']}\n"; } $aiEndpoint = 'https://onlineacademy.com.ly/ai-proxy.php'; // Send JSON body with both model and prompt $payload = json_encode([ 'model' => 'llama3', 'prompt' => $aiPrompt ]); $ch = curl_init($aiEndpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($httpCode !== 200 || $curlError) { throw new Exception("Error communicating with AI. HTTP Code: $httpCode. cURL Error: $curlError"); } $decodedResponse = json_decode($response, true); if (empty($decodedResponse['response'])) { throw new Exception("No response from AI. Raw: " . $response); } $_SESSION['chat_history'][] = ['sender' => 'AI', 'message' => $decodedResponse['response']]; // Check if the user wants to end the chat and evaluate if (stripos($userMessage, 'evaluate my level') !== false) { $evaluationPrompt = "Based on the following conversation, evaluate the user's English level. Respond with one of these labels: A1, A2, B1, B2, C1, or C2.\n\nConversation:\n"; foreach ($_SESSION['chat_history'] as $chat) { $evaluationPrompt .= "{$chat['sender']}: {$chat['message']}\n"; } // Send evaluation request to the active server list($httpCode, $curlError) = sendAiRequest($activeEndpoint, $evaluationPrompt, $response); if ($httpCode !== 200 || $curlError) { throw new Exception("Error communicating with AI for evaluation. HTTP Code: $httpCode. cURL Error: $curlError"); } $evaluationData = json_decode($response, true); $evaluationResult = $evaluationData['response'] ?? 'Unable to evaluate.'; $_SESSION['chat_history'][] = ['sender' => 'AI', 'message' => "Your English level is: $evaluationResult"]; } } } catch (Exception $e) { $response = "Error: " . $e->getMessage(); $_SESSION['chat_history'][] = ['sender' => 'System', 'message' => $response]; } // Extract evaluation result if present $evaluationResult = null; foreach ($_SESSION['chat_history'] as $chat) { if (str_starts_with($chat['message'], "Your English level is: ")) { $evaluationResult = $chat['message']; } } ?> AI English Level Chat

AI English Level Chat

Loading...

Please wait while the AI responds...

link.href = URL.createObjectURL(blob); link.download = "chat_history.txt"; link.click(); }); // Enable "Enter" key to send message document.getElementById('message').addEventListener('keydown', function (event) { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); document.getElementById('chatForm').submit(); } }); // Show loading indicator on form submission document.getElementById('chatForm').addEventListener('submit', function () { document.getElementById('loadingIndicator').style.display = 'block'; });