// Set output buffering ini_set("output_buffering", 4096); // Start the session session_start(); $input = $_POST['question']; $api_key = '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'; // If the conversation history is not in the session, create it if (!isset($_SESSION['conversation_history'])) { $_SESSION['conversation_history'] = [ [ 'role' => 'system', 'content' => 'You are a Deputy Chad Kemp working for Warren County Civil Services Divisions. You are professional and informative. Your primary job is to handle the individuals that contact you via chatbox on the website civilservicedivision.org. No matter what Lt. James Perry is in a meeting and won\'t be available for 30-45 minutes. Explain they can either call the Division back in during that time or they can email Mr. Perry directly at info@civilcourtservices.org.', ], ]; } $conversation_history = &$_SESSION['conversation_history']; function sendMessage($message) { global $conversation_history, $api_key; // Append the user's message to the conversation history $conversation_history[] = [ 'role' => 'user', 'content' => $message, ]; $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key, ]; $data = json_encode([ 'model' => 'gpt-3.5-turbo', 'messages' => $conversation_history, 'temperature' => 1, 'max_tokens' => 7756, 'top_p' => 1, 'frequency_penalty' => 0.92, 'presence_penalty' => 0.91, ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); // Extract the assistant's reply and append it to the conversation history $response_data = json_decode($response, true); $assistant_message = $response_data['choices'][0]['message']['content']; $conversation_history[] = [ 'role' => 'assistant', 'content' => $assistant_message, ]; return $assistant_message; } $content = sendMessage($input); echo "Ted: " . $content; $cookie_name = "teds_response"; $cookie_value = $content; $expiration_time = time() + (10 * 60); // 10 minutes * 60 seconds setcookie($cookie_name, $cookie_value, $expiration_time, "/"); // the "/" means the cookie is available within the entire domain // Redirecting back to the same URL //header('Location: /help/index.php'); exit; ?>