You can now Test AI Rewording Functionality right from the AI Settings Screen

This commit is contained in:
johnnyq
2024-02-10 21:15:05 -05:00
parent 726b023592
commit a1d642c54b
5 changed files with 101 additions and 42 deletions

51
post/ai.php Normal file
View File

@@ -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.']);
}
}