mirror of
https://github.com/itflow-org/itflow
synced 2026-02-28 02:44:53 +00:00
Finished UI work for AI Providers and Models, AI is set to use the new AI Provider and Models
This commit is contained in:
@@ -1,137 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - GET/POST request handler for AI Providers ('ai_providers')
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
if (isset($_POST['add_ai_provider'])) {
|
||||
|
||||
validateCSRFToken($_GET['csrf_token']);
|
||||
|
||||
$provider = sanitizeInput($_POST['provider']);
|
||||
$url = sanitizeInput($_POST['url']);
|
||||
$model = sanitizeInput($_POST['model']);
|
||||
$api_key = sanitizeInput($_POST['api_key']);
|
||||
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO ai_providers SET ai_provider_name = '$name', ai_provider_url = '$url', ai_provider_api_key = '$api_key'");
|
||||
|
||||
$ai_provider_id = mysqli_insert_id($mysqli);
|
||||
|
||||
if ($model) {
|
||||
mysqli_query($mysqli,"INSERT INTO ai_models SET ai_model_name = '$model'");
|
||||
}
|
||||
|
||||
// Logging
|
||||
logAction("AI Provider", "Create", "$session_name created AI Provider $provider");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$provider</strong> created";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['edit_ai_provider'])) {
|
||||
|
||||
validateCSRFToken($_GET['csrf_token']);
|
||||
|
||||
$provider_id intval($_POST['provider_id'])
|
||||
$provider = sanitizeInput($_POST['provider']);
|
||||
$url = sanitizeInput($_POST['url']);
|
||||
$api_key = sanitizeInput($_POST['api_key']);
|
||||
|
||||
mysqli_query($mysqli,"UPDATE ai_providers SET ai_provider_name = '$name', ai_provider_url = '$url', ai_provider_api_key = '$api_key' WHERE ai_provider_id = $provider_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Provider", "Edit", "$session_name edited AI Provider $provider");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$provider</strong> edited";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_ai_provider'])) {
|
||||
|
||||
$provider_id = intval($_GET['delete_ai_provider']);
|
||||
|
||||
$sql = mysqli_query($mysqli,"SELECT ai_provider_name FROM ai_providers WHERE ai_provider_id = $provider_id");
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$provider_name = sanitizeInput($row['ai_provider_name']);
|
||||
|
||||
mysqli_query($mysqli,"DELETE FROM ai_providers WHERE ai_provider_id = $provider_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Provider", "Delete", "$session_name deleted AI Provider $provider_name");
|
||||
|
||||
$_SESSION['alert_type'] = "error";
|
||||
$_SESSION['alert_message'] = "AI Provider <strong>$provider_name</strong> deleted";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['add_ai_model'])) {
|
||||
|
||||
validateCSRFToken($_GET['csrf_token']);
|
||||
|
||||
$provider_id = intval($_POST['provider_id']);
|
||||
$model = sanitizeInput($_POST['model']);
|
||||
$prompt = sanitizeInput($_POST['prompt']);
|
||||
$use_case = sanitizeInput($_POST['use_case']);
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO ai_models SET ai_model_name = '$model', ai_model_prompt = '$prompt', ai_model_use_case = '$use_case', ai_model_ai_provider_id = $provider_id");
|
||||
|
||||
$ai_model_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// Logging
|
||||
logAction("AI Model", "Create", "$session_name created AI Model $model");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$model</strong> created";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['edit_ai_model'])) {
|
||||
|
||||
validateCSRFToken($_GET['csrf_token']);
|
||||
|
||||
$model_id = intval($_POST['model_id']);
|
||||
$model = sanitizeInput($_POST['model']);
|
||||
$prompt = sanitizeInput($_POST['prompt']);
|
||||
$use_case = sanitizeInput($_POST['use_case']);
|
||||
|
||||
mysqli_query($mysqli,"UPDATE ai_models SET ai_model_name = '$model', ai_model_prompt = '$prompt', ai_model_use_case = '$use_case' WHERE ai_model_id = $model_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Model", "Edit", "$session_name edited AI Model $model");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$model</strong> edited";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_ai_model'])) {
|
||||
|
||||
$model_id = intval($_GET['delete_ai_model']);
|
||||
|
||||
$sql = mysqli_query($mysqli,"SELECT ai_model_name FROM ai_models WHERE ai_model_id = $model_id");
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$model_name = sanitizeInput($row['ai_model_name']);
|
||||
|
||||
mysqli_query($mysqli,"DELETE FROM ai_models WHERE ai_model_id = $model_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Model", "Delete", "$session_name deleted AI Model $model_name");
|
||||
|
||||
$_SESSION['alert_type'] = "error";
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$model_name</strong> deleted";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
71
post/admin/admin_ai_model.php
Normal file
71
post/admin/admin_ai_model.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - GET/POST request handler for AI Models ('ai_model')
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
if (isset($_POST['add_ai_model'])) {
|
||||
|
||||
validateCSRFToken($_POST['csrf_token']);
|
||||
|
||||
$provider_id = intval($_POST['provider']);
|
||||
$model = sanitizeInput($_POST['model']);
|
||||
$prompt = sanitizeInput($_POST['prompt']);
|
||||
$use_case = sanitizeInput($_POST['use_case']);
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO ai_models SET ai_model_name = '$model', ai_model_prompt = '$prompt', ai_model_use_case = '$use_case', ai_model_ai_provider_id = $provider_id");
|
||||
|
||||
$ai_model_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// Logging
|
||||
logAction("AI Model", "Create", "$session_name created AI Model $model");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$model</strong> created";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['edit_ai_model'])) {
|
||||
|
||||
validateCSRFToken($_POST['csrf_token']);
|
||||
|
||||
$model_id = intval($_POST['model_id']);
|
||||
$model = sanitizeInput($_POST['model']);
|
||||
$prompt = sanitizeInput($_POST['prompt']);
|
||||
$use_case = sanitizeInput($_POST['use_case']);
|
||||
|
||||
mysqli_query($mysqli,"UPDATE ai_models SET ai_model_name = '$model', ai_model_prompt = '$prompt', ai_model_use_case = '$use_case' WHERE ai_model_id = $model_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Model", "Edit", "$session_name edited AI Model $model");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$model</strong> edited";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_ai_model'])) {
|
||||
|
||||
validateCSRFToken($_GET['csrf_token']);
|
||||
|
||||
$model_id = intval($_GET['delete_ai_model']);
|
||||
|
||||
$sql = mysqli_query($mysqli,"SELECT ai_model_name FROM ai_models WHERE ai_model_id = $model_id");
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$model_name = sanitizeInput($row['ai_model_name']);
|
||||
|
||||
mysqli_query($mysqli,"DELETE FROM ai_models WHERE ai_model_id = $model_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Model", "Delete", "$session_name deleted AI Model $model_name");
|
||||
|
||||
$_SESSION['alert_type'] = "error";
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$model_name</strong> deleted";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
72
post/admin/admin_ai_provider.php
Normal file
72
post/admin/admin_ai_provider.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - GET/POST request handler for AI Providers ('ai_provider')
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
|
||||
if (isset($_POST['add_ai_provider'])) {
|
||||
|
||||
validateCSRFToken($_POST['csrf_token']);
|
||||
|
||||
$provider = sanitizeInput($_POST['provider']);
|
||||
$url = sanitizeInput($_POST['url']);
|
||||
$model = sanitizeInput($_POST['model']);
|
||||
$api_key = sanitizeInput($_POST['api_key']);
|
||||
|
||||
|
||||
mysqli_query($mysqli,"INSERT INTO ai_providers SET ai_provider_name = '$provider', ai_provider_api_url = '$url', ai_provider_api_key = '$api_key'");
|
||||
|
||||
$ai_provider_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// Logging
|
||||
logAction("AI Provider", "Create", "$session_name created AI Provider $provider");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$provider</strong> created";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_POST['edit_ai_provider'])) {
|
||||
|
||||
validateCSRFToken($_POST['csrf_token']);
|
||||
|
||||
$provider_id = intval($_POST['provider_id']);
|
||||
$provider = sanitizeInput($_POST['provider']);
|
||||
$url = sanitizeInput($_POST['url']);
|
||||
$api_key = sanitizeInput($_POST['api_key']);
|
||||
|
||||
mysqli_query($mysqli,"UPDATE ai_providers SET ai_provider_name = '$provider', ai_provider_api_url = '$url', ai_provider_api_key = '$api_key' WHERE ai_provider_id = $provider_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Provider", "Edit", "$session_name edited AI Provider $provider");
|
||||
|
||||
$_SESSION['alert_message'] = "AI Model <strong>$provider</strong> edited";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
|
||||
if (isset($_GET['delete_ai_provider'])) {
|
||||
|
||||
validateCSRFToken($_GET['csrf_token']);
|
||||
|
||||
$provider_id = intval($_GET['delete_ai_provider']);
|
||||
|
||||
$sql = mysqli_query($mysqli,"SELECT ai_provider_name FROM ai_providers WHERE ai_provider_id = $provider_id");
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$provider_name = sanitizeInput($row['ai_provider_name']);
|
||||
|
||||
mysqli_query($mysqli,"DELETE FROM ai_providers WHERE ai_provider_id = $provider_id");
|
||||
|
||||
// Logging
|
||||
logAction("AI Provider", "Delete", "$session_name deleted AI Provider $provider_name");
|
||||
|
||||
$_SESSION['alert_type'] = "error";
|
||||
$_SESSION['alert_message'] = "AI Provider <strong>$provider_name</strong> deleted";
|
||||
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
|
||||
}
|
||||
42
post/ai.php
42
post/ai.php
@@ -10,17 +10,23 @@ if (isset($_GET['ai_reword'])) {
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM ai_models LEFT JOIN ai_providers ON ai_model_ai_provider_id = ai_provider_id WHERE ai_model_use_case = 'General' LIMIT 1");
|
||||
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$model_name = $row['ai_model_name'];
|
||||
$promptText = $row['ai_model_prompt'];
|
||||
$url = $row['ai_provider_api_url'];
|
||||
$key = $row['ai_provider_api_key'];
|
||||
|
||||
// Collecting the input data from the AJAX request.
|
||||
$inputJSON = file_get_contents('php://input');
|
||||
$input = json_decode($inputJSON, TRUE); // Convert JSON into array.
|
||||
|
||||
$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.
|
||||
$data = [
|
||||
"model" => "$config_ai_model", // Specify the model
|
||||
"model" => "$model_name", // Specify the model
|
||||
"messages" => [
|
||||
["role" => "system", "content" => $promptText],
|
||||
["role" => "user", "content" => $userText],
|
||||
@@ -29,7 +35,7 @@ if (isset($_GET['ai_reword'])) {
|
||||
];
|
||||
|
||||
// Initialize cURL session to the OpenAI Chat API.
|
||||
$ch = curl_init("$config_ai_url");
|
||||
$ch = curl_init("$url");
|
||||
|
||||
// Set cURL options for the request.
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
@@ -37,7 +43,7 @@ if (isset($_GET['ai_reword'])) {
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Bearer ' . $config_ai_api_key,
|
||||
'Authorization: Bearer ' . $key,
|
||||
]);
|
||||
|
||||
// Execute the cURL session and capture the response.
|
||||
@@ -72,6 +78,13 @@ if (isset($_GET['ai_reword'])) {
|
||||
|
||||
if (isset($_GET['ai_ticket_summary'])) {
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM ai_models LEFT JOIN ai_providers ON ai_model_ai_provider_id = ai_provider_id WHERE ai_model_use_case = 'General' LIMIT 1");
|
||||
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$model_name = $row['ai_model_name'];
|
||||
$url = $row['ai_provider_api_url'];
|
||||
$key = $row['ai_provider_api_key'];
|
||||
|
||||
// Retrieve the ticket_id from POST
|
||||
$ticket_id = intval($_POST['ticket_id']);
|
||||
|
||||
@@ -108,7 +121,7 @@ if (isset($_GET['ai_ticket_summary'])) {
|
||||
|
||||
// Prepare the POST data
|
||||
$post_data = [
|
||||
"model" => "$config_ai_model",
|
||||
"model" => "$model_name",
|
||||
"messages" => [
|
||||
["role" => "system", "content" => "You are a helpful assistant."],
|
||||
["role" => "user", "content" => $prompt]
|
||||
@@ -117,12 +130,12 @@ if (isset($_GET['ai_ticket_summary'])) {
|
||||
];
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $config_ai_url);
|
||||
curl_setopt($ch, CURLOPT_URL, $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
|
||||
'Authorization: Bearer ' . $key
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
|
||||
|
||||
@@ -145,6 +158,13 @@ if (isset($_GET['ai_create_document_template'])) {
|
||||
|
||||
header('Content-Type: text/html; charset=UTF-8');
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT * FROM ai_models LEFT JOIN ai_providers ON ai_model_ai_provider_id = ai_provider_id WHERE ai_model_use_case = 'General' LIMIT 1");
|
||||
|
||||
$row = mysqli_fetch_array($sql);
|
||||
$model_name = $row['ai_model_name'];
|
||||
$url = $row['ai_provider_api_url'];
|
||||
$key = $row['ai_provider_api_key'];
|
||||
|
||||
$prompt = $_POST['prompt'] ?? '';
|
||||
|
||||
// Basic validation
|
||||
@@ -158,7 +178,7 @@ if (isset($_GET['ai_create_document_template'])) {
|
||||
$user_message = "Create an HTML formatted IT documentation template based on the following request:\n\n\"$prompt\"\n\nThe template should be structured, professional, and useful for IT staff. Include relevant sections, instructions, prerequisites, and best practices.";
|
||||
|
||||
$post_data = [
|
||||
"model" => "$config_ai_model",
|
||||
"model" => "$model_name",
|
||||
"messages" => [
|
||||
["role" => "system", "content" => $system_message],
|
||||
["role" => "user", "content" => $user_message]
|
||||
@@ -167,12 +187,12 @@ if (isset($_GET['ai_create_document_template'])) {
|
||||
];
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $config_ai_url);
|
||||
curl_setopt($ch, CURLOPT_URL, $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
|
||||
'Authorization: Bearer ' . $key
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user