client = OpenAI::client(env('OPENAI_API_KEY')); $this->client = OpenAI::client(env('OPENAI_API_KEY')); } public function generateQueryBasedMainContent($prompt, $mainContent, $chatHistory) { // Load file JSON $jsonPath = public_path('templates/contentTemplatePrompt.json'); // Sesuaikan path $jsonData = json_decode(file_get_contents($jsonPath), true); // Periksa apakah kategori ada dalam JSON if (!isset($jsonData[$mainContent])) { return "Template prompt tidak ditemukan."; } // Ambil template berdasarkan kategori $promptTemplate = $jsonData[$mainContent]['prompt']; // Menyusun pesan untuk OpenAI $messages = [ ['role' => 'system', 'content' => $promptTemplate], ]; // Menambahkan chat history sebagai konteks foreach ($chatHistory as $chat) { if (isset($chat['user'])) { $messages[] = ['role' => 'user', 'content' => $chat['user']]; } if (isset($chat['rawBotResponse'])) { $messages[] = ['role' => 'assistant', 'content' => $chat['rawBotResponse']]; } } // Tambahkan prompt terbaru user $messages[] = ['role' => 'user', 'content' => $prompt]; // Kirim request ke OpenAI API $response = $this->client->chat()->create([ 'model' => 'gpt-4o-mini', 'messages' => $messages, ]); return trim($response['choices'][0]['message']['content'] ?? 'No response'); } // public function generateQueryBasedMainContent($prompt, $mainContent, $chatHistory) // { // // Load file JSON // $jsonPath = public_path('templates/contentTemplatePrompt.json'); // Sesuaikan path // $jsonData = json_decode(file_get_contents($jsonPath), true); // // Periksa apakah kategori ada dalam JSON // if (!isset($jsonData[$mainContent])) { // return "Template prompt tidak ditemukan."; // } // // Ambil template berdasarkan kategori // $promptTemplate = $jsonData[$mainContent]['prompt']; // $response = $this->client->chat()->create([ // 'model' => 'gpt-4o-mini', // 'messages' => [ // ['role' => 'system', 'content' => $promptTemplate], // ['role' => 'user', 'content' => $prompt], // ], // ]); // return trim($response['choices'][0]['message']['content'] ?? 'No response'); // } public function validateSyntaxQuery($queryResponse) { $response = $this->client->chat()->create([ 'model' => 'gpt-4o-mini', 'messages' => [ [ 'role' => 'system', 'content' => "You are a MariaDB SQL expert. Your task is to validate the syntax of an SQL query to ensure it follows proper MariaDB syntax rules. Guidelines: - Check for any syntax errors, missing keywords, or incorrect clause usage. - Ensure the query is well-structured and adheres to best practices. - Verify that all SQL keywords are used correctly and in the right order. - If the query is valid, respond with: \"VALID\". - If the query has issues, respond with: \"INVALID\". Always respond with either \"VALID\" or \"INVALID\"." ], ['role' => 'user', 'content' => $queryResponse], ], ]); return trim($response['choices'][0]['message']['content'] ?? 'No response'); } public function generateNLPFromQuery($inputUser, $resultQuery) { $response = $this->client->chat()->create([ 'model' => 'gpt-4o-mini', 'messages' => [ [ 'role' => 'system', 'content' => "You are an expert assistant. Your task is to analyze the database query results and transform them into a human-readable answer based on the user's question. Guidelines: - Understand the user's question and extract the key intent. - Summarize or format the query results to directly answer the user's question. - Ensure the response is clear, concise, and relevant. - If the query result is empty or does not match the question, provide a polite response indicating that no data is available. Always provide a well-structured response that makes sense based on the input question." ], ['role' => 'user', 'content' => "User's question: $inputUser \nDatabase result: $resultQuery"], ], ]); return trim($response['choices'][0]['message']['content'] ?? 'No response'); } public function generateFinalText($nlpResult) { $response = $this->client->chat()->create([ 'model' => 'gpt-4o-mini', 'messages' => [ [ 'role' => 'system', 'content' => "You are an expert text formatter. Your task is to take the given NLP result and format it into a structured, human-readable text suitable for rendering inside an HTML