AI Feature Ticket Summary: Summarieze an entire ticket

This commit is contained in:
johnnyq 2024-12-10 14:03:43 -05:00
parent 501c5fce15
commit 2ac32ab0c5
3 changed files with 111 additions and 0 deletions

14
js/ai_ticket_summary.js Normal file
View File

@ -0,0 +1,14 @@
$('#summaryModal').on('shown.bs.modal', function (e) {
// Perform AJAX request to get the summary
$.ajax({
url: 'post.php?ai_ticket_summary',
method: 'POST',
data: { ticket_id: <?php echo $ticket_id; ?> },
success: function(response) {
$('#summaryContent').html(response);
},
error: function() {
$('#summaryContent').html('Error generating summary.');
}
});
});

View File

@ -57,3 +57,73 @@ if (isset($_GET['ai_reword'])) {
}
}
if (isset($_GET['ai_ticket_summary'])) {
// Retrieve the ticket_id from POST
$ticket_id = intval($_POST['ticket_id']);
// Query the database for ticket details
// (You can reuse code from ticket.php or write a simplified query here)
$sql = mysqli_query($mysqli, "
SELECT ticket_subject, ticket_details
FROM tickets
WHERE ticket_id = $ticket_id
LIMIT 1
");
$row = mysqli_fetch_assoc($sql);
$ticket_subject = $row['ticket_subject'];
$ticket_details = strip_tags($row['ticket_details']); // strip HTML for cleaner prompt
// Get ticket replies
$sql_replies = mysqli_query($mysqli, "
SELECT ticket_reply, ticket_reply_type
FROM ticket_replies
WHERE ticket_reply_ticket_id = $ticket_id
AND ticket_reply_archived_at IS NULL
ORDER BY ticket_reply_id ASC
");
$all_replies_text = "";
while ($reply = mysqli_fetch_assoc($sql_replies)) {
$reply_type = $reply['ticket_reply_type'];
$reply_text = strip_tags($reply['ticket_reply']);
$all_replies_text .= "\n[$reply_type]: $reply_text";
}
// Craft a prompt for ChatGPT
$prompt = "Summarize the following ticket and its responses in a concise and clear way. The summary should be short, highlight the main issue, the actions taken, and any resolution steps:\n\nTicket Subject: $ticket_subject\nTicket Details: $ticket_details\nReplies:$all_replies_text\n\nShort Summary:";
// Prepare the POST data
$post_data = [
"model" => "$config_ai_model",
"messages" => [
["role" => "system", "content" => "You are a helpful assistant."],
["role" => "user", "content" => $prompt]
],
"temperature" => 0.7
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config_ai_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $config_ai_api_key,
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "Error: " . curl_error($ch);
exit;
}
curl_close($ch);
$response_data = json_decode($response, true);
$summary = $response_data['choices'][0]['message']['content'] ?? "No summary available.";
// Print the summary
echo nl2br(htmlentities($summary));
}

View File

@ -379,6 +379,12 @@ if (isset($_GET['ticket_id'])) {
<div class="card-tools d-print-none">
<div class="btn-toolbar">
<?php if($config_ai_enable == 1) { ?>
<button class="btn btn-info btn-sm ml-3" data-toggle="modal" data-target="#summaryModal">
<i class="fas fa-fw fa-lightbulb mr-2"></i>Summarize
</button>
<? } ?>
<?php if ($config_module_enable_accounting && $ticket_billable == 1 && empty($invoice_id) && lookupUserPermission("module_sales") >= 2) { ?>
<a href="#" class="btn btn-light btn-sm ml-3" href="#" data-toggle="modal" data-target="#addInvoiceFromTicketModal">
<i class="fas fa-fw fa-file-invoice mr-2"></i>Invoice
@ -1180,6 +1186,25 @@ require_once "footer.php";
?>
<!-- Summary Modal -->
<div class="modal fade" id="summaryModal" tabindex="-1" role="dialog" aria-labelledby="summaryModalTitle" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content bg-dark">
<div class="modal-header">
<h5 class="modal-title" id="summaryModalTitle">Ticket Summary</h5>
<button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
<span>&times;</span>
</button>
</div>
<div class="modal-body bg-white">
<div id="summaryContent" class="text-center">
<i class="fas fa-spinner fa-spin"></i> Generating summary...
</div>
</div>
</div>
</div>
</div>
<script src="js/show_modals.js"></script>
<?php if (empty($ticket_closed_at)) { ?>
@ -1191,3 +1216,5 @@ require_once "footer.php";
<?php } ?>
<script src="js/pretty_content.js"></script>
<script src="js/ai_ticket_summary.js"></script>