diff --git a/blank.php b/blank.php index e2313dbd..fb9687e0 100644 --- a/blank.php +++ b/blank.php @@ -1,5 +1,4 @@ - +
- - diff --git a/js/ai_reword.js b/js/ai_reword.js new file mode 100644 index 00000000..43ab587e --- /dev/null +++ b/js/ai_reword.js @@ -0,0 +1,36 @@ +document.getElementById('rewordButton').addEventListener('click', function() { + var textInput = document.getElementById('textInput'); + var rewordButton = document.getElementById('rewordButton'); + var undoButton = document.getElementById('undoButton'); + var previousText = textInput.value; // Store the current text + + // Disable the Reword button and show loading state + rewordButton.disabled = true; + rewordButton.innerText = 'Processing...'; + + fetch('post.php?ai_reword', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ text: textInput.value }), + }) + .then(response => response.json()) + .then(data => { + textInput.value = data.rewordedText || 'Error: Could not reword the text.'; + rewordButton.disabled = false; + rewordButton.innerText = 'Reword'; // Reset button text + undoButton.style.display = 'inline'; // Show the Undo button + + // Set up the Undo button to revert to the previous text + undoButton.onclick = function() { + textInput.value = previousText; + this.style.display = 'none'; // Hide the Undo button again + }; + }) + .catch(error => { + console.error('Error:', error); + rewordButton.disabled = false; + rewordButton.innerText = 'Reword'; // Reset button text + }); +}); diff --git a/post.php b/post.php index 96854117..40a878d0 100644 --- a/post.php +++ b/post.php @@ -81,6 +81,9 @@ require_once "post/vendor.php"; require_once "post/budget.php"; +require_once "post/ai.php"; + require_once "post/misc.php"; + ?> diff --git a/post/ai.php b/post/ai.php new file mode 100644 index 00000000..965556d1 --- /dev/null +++ b/post/ai.php @@ -0,0 +1,51 @@ + "$config_ai_model", // Specify the model + "messages" => [ + ["role" => "user", "content" => $input['text']] + ], + "temperature" => 0.7 + ]; + + // Initialize cURL session to the OpenAI Chat API. + $ch = curl_init("$config_ai_url/v1/chat/completions"); + + // Set cURL options for the request. + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $config_ai_api_key, + ]); + + // Execute the cURL session and capture the response. + $response = curl_exec($ch); + curl_close($ch); + + // Decode the JSON response. + $responseData = json_decode($response, true); + + // Check if the response contains the expected data and return it. + if (isset($responseData['choices'][0]['message']['content'])) { + echo json_encode(['rewordedText' => trim($responseData['choices'][0]['message']['content'])]); + } else { + // Handle errors or unexpected response structure. + echo json_encode(['rewordedText' => 'Failed to get a response from the OpenAI API.']); + } + +} \ No newline at end of file diff --git a/settings_ai.php b/settings_ai.php index 79ddf3f8..dc8ee6c9 100644 --- a/settings_ai.php +++ b/settings_ai.php @@ -60,6 +60,16 @@ require_once "inc_all_settings.php"; + +