mirror of https://github.com/itflow-org/itflow
You can now Test AI Rewording Functionality right from the AI Settings Screen
This commit is contained in:
parent
726b023592
commit
a1d642c54b
43
blank.php
43
blank.php
|
|
@ -1,5 +1,4 @@
|
|||
<?php require_once "inc_all.php";
|
||||
?>
|
||||
<?php require_once "inc_all.php"; ?>
|
||||
|
||||
<!-- Breadcrumbs-->
|
||||
<ol class="breadcrumb">
|
||||
|
|
@ -26,47 +25,7 @@ echo "<H1>$start_date</H1>";
|
|||
|
||||
<?php echo randomString(100); ?>
|
||||
<br>
|
||||
<form id="myForm">
|
||||
<textarea id="Body" name="body" rows="4" cols="50"></textarea>
|
||||
<br>
|
||||
<button type="submit">Submit</button>
|
||||
<button type="button" id="rewordButton">Reword</button>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
|
||||
document.getElementById('rewordButton').addEventListener('click', function() {
|
||||
const textarea = document.getElementById('Body');
|
||||
const textToReword = textarea.value;
|
||||
|
||||
// Replace 'YOUR_API_KEY' with your actual OpenAI API key
|
||||
const apiKey = '<?php echo $config_ai_api_key; ?>';
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${apiKey}`
|
||||
};
|
||||
|
||||
// Prepare the API request payload
|
||||
const data = {
|
||||
model: "<?php echo $config_ai_model; ?>", // or the latest available model
|
||||
prompt: `Reword the following text: "${textToReword}"`,
|
||||
temperature: 0.7,
|
||||
max_tokens: 1024,
|
||||
};
|
||||
|
||||
// Make the API call to OpenAI to reword the text
|
||||
axios.post('<?php echo $config_ai_url; ?>/v1/completions', data, {headers: headers})
|
||||
.then(response => {
|
||||
textarea.value = response.data.choices[0].text.trim();
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('There was an error rewording the text:', error);
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<script>toastr.success('Have Fun Wozz!!')</script>
|
||||
|
||||
<?php require_once "footer.php";
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
});
|
||||
});
|
||||
3
post.php
3
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";
|
||||
|
||||
|
||||
?>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - GET/POST request handler for AI Functions
|
||||
*/
|
||||
|
||||
if (isset($_GET['ai_reword'])) {
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Collecting the input data from the AJAX request.
|
||||
$inputJSON = file_get_contents('php://input');
|
||||
$input = json_decode($inputJSON, TRUE); // Convert JSON into array.
|
||||
|
||||
// Preparing the data for the OpenAI Chat API request.
|
||||
$data = [
|
||||
"model" => "$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.']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -60,6 +60,16 @@ require_once "inc_all_settings.php";
|
|||
<button type="submit" name="edit_ai_settings" class="btn btn-primary text-bold"><i class="fa fa-check mr-2"></i>Save</button>
|
||||
|
||||
</form>
|
||||
|
||||
<div class="mt-5">
|
||||
<h1>Test Input Text to Reword</h1>
|
||||
<textarea id="textInput" class="form-control mb-3" rows="10"></textarea>
|
||||
<button id="rewordButton" class="btn btn-primary"><i class="fas fa-fw fa-robot mr-2"></i>Reword</button>
|
||||
<button id="undoButton" class="btn btn-secondary" style="display:none;"><i class="fas fa-fw fa-redo-alt mr-2"></i>Undo</button>
|
||||
</div>
|
||||
|
||||
<script src="js/ai_reword.js"></script>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue