From 9f7c289e94ccdc9df37a4fb6fa0b98ac61097a8a Mon Sep 17 00:00:00 2001 From: johnnyq Date: Thu, 27 Mar 2025 22:25:01 -0400 Subject: [PATCH] Fix AI Promps --- ajax.php | 1 - post/ai.php | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ajax.php b/ajax.php index 96382acf..7e046691 100644 --- a/ajax.php +++ b/ajax.php @@ -599,4 +599,3 @@ if (isset($_POST['update_recurring_invoice_items_order'])) { echo json_encode(['status' => 'success']); exit; } - diff --git a/post/ai.php b/post/ai.php index a304c2e1..e5ced6c3 100644 --- a/post/ai.php +++ b/post/ai.php @@ -14,7 +14,8 @@ if (isset($_GET['ai_reword'])) { $inputJSON = file_get_contents('php://input'); $input = json_decode($inputJSON, TRUE); // Convert JSON into array. - $promptText = "reword"; + $promptText = "Rephrase this ticket response to be clear, professional, and polite. Maintain the original meaning without adding or omitting information. Do not invent details. Keep the tone neutral and professional. Avoid technical jargon unless necessary. Please return only the reworded text as clean, properly formatted HTML, without any code block markers, backticks, or the word 'html'. Do not include any other commentary or explanations."; + $userText = $input['text']; // Preparing the data for the OpenAI Chat API request. @@ -48,9 +49,20 @@ if (isset($_GET['ai_reword'])) { // Check if the response contains the expected data and return it. if (isset($responseData['choices'][0]['message']['content'])) { - // Remove any square brackets and their contents from the response. - $responseData['choices'][0]['message']['content'] = preg_replace('/\[.*?\]/', '', $responseData['choices'][0]['message']['content']); - echo json_encode(['rewordedText' => trim($responseData['choices'][0]['message']['content'])]); + // Get the response content. + $content = $responseData['choices'][0]['message']['content']; + + // Clean any leading "html" word or other unwanted text at the beginning. + $content = preg_replace('/^html/i', '', $content); // Remove any occurrence of 'html' at the start + + // Clean the response content to remove backticks or code block markers. + $cleanedContent = str_replace('```', '', $content); // Remove backticks if they exist. + + // Trim any leading/trailing whitespace. + $cleanedContent = trim($cleanedContent); + + // Return the cleaned response. + echo json_encode(['rewordedText' => $cleanedContent]); } else { // Handle errors or unexpected response structure. echo json_encode(['rewordedText' => 'Failed to get a response from the OpenAI API.']);