File: /var/www/html/www.winghung.com/wp-content/plugins/upkyk-assistant-ai/admin/training-page.php
<?php
/**
* AssistantAI Training Page Template
*
* @package Upkyk_Assistant_AI
*/
// Direct access prevention
if (!defined('ABSPATH')) exit;
// Get license status and usage info for header display
$license_status = 'active'; // Always active now that license validation is removed
$usage_data = get_transient('upkyk_assistant_ai_usage_data');
// Generate and store nonce for AJAX operations
$ajax_nonce = wp_create_nonce('upkyk_assistant_ai_nonce');
// Check if training documents table exists
global $wpdb;
$documents_table = $wpdb->prefix . 'upkyk_training_documents';
// Check cache first
$training_tables_exist = wp_cache_get('upkyk_training_tables_exist');
if (false === $training_tables_exist) {
// Direct DB query necessary here as WordPress doesn't provide a function to check if a table exists
// @codingStandardsIgnoreStart
$training_tables_exist = ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $documents_table)) == $documents_table);
// @codingStandardsIgnoreEnd
wp_cache_set('upkyk_training_tables_exist', $training_tables_exist, '', HOUR_IN_SECONDS);
}
// Create the documents table if it doesn't exist
if (!$training_tables_exist) {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $documents_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
file_path varchar(255) NOT NULL,
file_type varchar(10) NOT NULL,
content longtext NOT NULL,
status varchar(20) NOT NULL DEFAULT 'pending',
date_added datetime NOT NULL,
date_indexed datetime DEFAULT NULL,
PRIMARY KEY (id)
) $charset_collate;";
dbDelta($sql);
$training_tables_exist = true;
}
// At the top of the file, indicate training is ready (no Firebase dependency)
$is_training_ready = true;
// Client ID no longer needed
$client_id = '';
// Get configuration settings with defaults
$sensitivity = get_option('upkyk_assistant_ai_intent_sensitivity', '0.7');
$confidence_threshold_enabled = get_option('upkyk_assistant_ai_confidence_threshold_enabled', '1');
$confidence_threshold = get_option('upkyk_assistant_ai_confidence_threshold', '50');
$max_context_length = get_option('upkyk_assistant_ai_max_context_length', '3');
$response_creativity = get_option('upkyk_assistant_ai_response_creativity', '0.5');
// Get the option and apply stripslashes before escaping for display
$raw_fallback_message = get_option('upkyk_assistant_ai_fallback_message', __("I am not sure I understand your question. Could you please rephrase it or ask something else?", 'upkyk-assistant-ai'));
$fallback_message = stripslashes($raw_fallback_message);
$training_status = get_option('upkyk_assistant_ai_training_status', 'not_trained');
$last_trained = get_option('upkyk_assistant_ai_last_trained', '');
?>
<div class="wrap upkyk-assistant-ai-admin">
<?php
// Display settings errors/notices
settings_errors('upkyk_training_config');
?>
<div class="upkyk-admin-header">
<a href="<?php echo esc_url('https://upkyk.com/assistant-ai'); ?>" title="<?php echo esc_attr__('Upkyk Assistant AI Product Page', 'upkyk-assistant-ai'); ?>" target="_blank" rel="noopener noreferrer">
<div class="upkyk-logo">
<img src="<?php echo esc_url(UPKYK_ASSISTANT_AI_URL . 'assets/images/upkyk-logo.svg'); ?>" alt="<?php esc_attr_e('Upkyk Logo', 'upkyk-assistant-ai'); ?>">
</div>
</a>
<?php if ($license_status === 'active' && $usage_data): ?>
<div class="upkyk-usage-info">
<div class="upkyk-plan"><?php echo esc_html($usage_data['plan'] ?? __('Standard Plan', 'upkyk-assistant-ai')); ?></div>
<div class="upkyk-usage-bar">
<div class="upkyk-usage-progress" style="width: <?php echo esc_attr(min(100, (($usage_data['used'] ?? 0) / ($usage_data['limit'] ?? 1)) * 100)); ?>%"></div>
</div>
<div class="upkyk-usage-text">
<?php
printf(
/* translators: 1: Number of messages used, 2: Total messages limit */
esc_html__('%1$d / %2$d messages used', 'upkyk-assistant-ai'),
esc_html($usage_data['used'] ?? 0),
esc_html($usage_data['limit'] ?? 1000)
);
?>
</div>
</div>
<?php endif; ?>
</div>
<!-- <h1 class="wp-heading-inline upkyk-page-title"><?php esc_html_e('Assistant AI Training', 'upkyk-assistant-ai'); ?></h1> -->
<div class="upkyk-training-container">
<!-- Training Status Panel -->
<div class="upkyk-training-status-panel">
<?php
// Check if any documents have 'pending' status
global $wpdb;
$documents_table = $wpdb->prefix . 'upkyk_training_documents';
$has_pending_documents = false;
$pending_count = 0;
// First, check for actual files in the training directory
$upload_dir = wp_upload_dir();
$training_dir = $upload_dir['basedir'] . '/upkyk-assistant-ai/training';
$actual_files = array();
if (is_dir($training_dir)) {
$files = glob($training_dir . '/*.*');
foreach ($files as $file_path) {
$extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
if (in_array($extension, array('txt', 'csv', 'json'))) {
$actual_files[] = $file_path;
}
}
}
// Direct DB query necessary here as WordPress doesn't provide a function to check if a table exists
// @codingStandardsIgnoreStart
if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $documents_table)) == $documents_table) {
// @codingStandardsIgnoreEnd
// Count only files that actually exist in the filesystem
$file_count = count($actual_files);
// Try to get processed files from cache
$processed_files = wp_cache_get('upkyk_processed_files');
if (false === $processed_files) {
// Direct DB query necessary here as we're working with a custom table
// @codingStandardsIgnoreStart
$safe_table_name = esc_sql($documents_table);
$processed_files = $wpdb->get_col($wpdb->prepare(
"SELECT file_path FROM `{$safe_table_name}` WHERE status = %s",
'processed'
));
// @codingStandardsIgnoreEnd
wp_cache_set('upkyk_processed_files', $processed_files, '', 5 * MINUTE_IN_SECONDS);
}
// Count how many actual files haven't been processed yet
$pending_count = 0;
foreach ($actual_files as $file_path) {
if (!in_array($file_path, $processed_files)) {
$pending_count++;
}
}
// Add debug information (visible to admins only)
if (current_user_can('manage_options')) {
$debug_info = sprintf(
'<div class="upkyk-debug-info" style="font-size: 12px; margin-top: 5px; color: #666;">Debug: %d files found, %d pending</div>',
$file_count,
$pending_count
);
}
$has_pending_documents = ($pending_count > 0);
} else {
// Table doesn't exist, log error
// Table doesn't exist, handle gracefully
if (current_user_can('manage_options')) {
$debug_info = '<div class="upkyk-debug-info" style="font-size: 12px; margin-top: 5px; color: #666;">Debug: Documents table does not exist</div>';
}
}
$trained_status = get_option('upkyk_assistant_ai_trained', '0');
// Set the CSS class based on training status and pending documents
$status_class = 'not_trained';
if ($trained_status === '1') {
$status_class = $has_pending_documents ? 'needs_training' : 'trained';
}
// Set display text based on status
if ($trained_status === '1') {
if ($has_pending_documents) {
$status_text = __('New documents need training', 'upkyk-assistant-ai');
} else {
$status_text = __('Assistant AI trained', 'upkyk-assistant-ai');
}
} else {
$status_text = __('Assistant AI not trained', 'upkyk-assistant-ai');
}
?>
<div class="upkyk-status-indicator <?php echo esc_attr($status_class); ?>">
<span class="upkyk-status-icon"></span>
<span class="upkyk-status-text">
<?php echo esc_html($status_text); ?>
<?php if ($has_pending_documents && $pending_count > 0): ?>
<span class="upkyk-pending-count">(<?php echo esc_html($pending_count); ?> <?php echo esc_html($pending_count == 1 ? __('document', 'upkyk-assistant-ai') : __('documents', 'upkyk-assistant-ai')); ?>)</span>
<?php endif; ?>
</span>
<?php if (current_user_can('manage_options') && isset($debug_info)): ?>
<div class="upkyk-debug-info" style="font-size: 12px; margin-top: 5px; color: #666; display: none;">
<?php echo esc_html($debug_info); ?>
</div>
<?php endif; ?>
</div>
</div>
<div class="upkyk-training-content">
<div class="upkyk-tabs-container upkyk-training-tabs">
<div class="upkyk-tab-links">
<a href="#phrases-tab" class="upkyk-tab-link active"><?php esc_html_e('Custom Phrases', 'upkyk-assistant-ai'); ?></a>
<a href="#documents-tab" class="upkyk-tab-link">
<span><?php esc_html_e('Training', 'upkyk-assistant-ai'); ?></span>
</a>
<a href="#keywords-tab" class="upkyk-tab-link"><?php esc_html_e('Priority Keywords', 'upkyk-assistant-ai'); ?></a>
<a href="#settings-tab" class="upkyk-tab-link"><?php esc_html_e('AI Settings', 'upkyk-assistant-ai'); ?></a>
<a href="#test-tab" class="upkyk-tab-link"><?php esc_html_e('Test Chatbot', 'upkyk-assistant-ai'); ?></a>
</div>
<div class="upkyk-tab-content">
<!-- Custom Phrases Tab -->
<div id="phrases-tab" class="upkyk-tab-pane active">
<div class="upkyk-section-intro">
<h2><?php esc_html_e('Custom Phrases', 'upkyk-assistant-ai'); ?></h2>
<p><?php esc_html_e('Teach your chatbot specific phrases and their responses. The AI will use these as examples when answering similar questions from your users.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-phrases-section">
<div class="upkyk-form-container">
<form id="upkyk-add-phrase-form" method="post">
<input type="hidden" name="action" value="upkyk_add_phrase">
<input type="hidden" name="nonce" value="<?php echo esc_attr($ajax_nonce); ?>">
<div class="upkyk-form-field">
<label for="upkyk-phrase-question"><?php esc_html_e('Question:', 'upkyk-assistant-ai'); ?></label>
<input type="text" id="upkyk-phrase-question" name="question" placeholder="<?php esc_attr_e('How do I reset my password?', 'upkyk-assistant-ai'); ?>">
</div>
<div class="upkyk-form-field">
<label for="upkyk-phrase-category"><?php esc_html_e('Category:', 'upkyk-assistant-ai'); ?></label>
<input type="text" id="upkyk-phrase-category" name="category" placeholder="<?php esc_attr_e('Account Help', 'upkyk-assistant-ai'); ?>" value="<?php esc_attr_e('General', 'upkyk-assistant-ai'); ?>">
<p class="description"><?php esc_html_e('Group similar phrases together for better organization', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-field">
<label for="upkyk-phrase-answer"><?php esc_html_e('Answer:', 'upkyk-assistant-ai'); ?></label>
<textarea id="upkyk-phrase-answer" name="answer" rows="3" placeholder="<?php esc_attr_e("You can reset your password by clicking on the 'Forgot Password' link on the login page.", 'upkyk-assistant-ai'); ?>"></textarea>
</div>
<div class="upkyk-form-actions">
<button type="button" class="button button-primary" id="add-phrase-button"><?php esc_html_e('Add Phrase', 'upkyk-assistant-ai'); ?></button>
</div>
</form>
</div>
</div>
<!-- Database Diagnosis - Phrases Table -->
<div id="phrases-database-diagnosis" style="margin-top: 20px; background: #f8f9fa; padding: 15px; border: 1px solid #e5e5e5; border-radius: 5px;">
<h3><?php esc_html_e('Phrases Table', 'upkyk-assistant-ai'); ?></h3>
<div class="phrases-table-content">
<?php
global $wpdb;
$phrases_table = $wpdb->prefix . 'upkyk_custom_phrases';
// Try to get table status from cache
$table_status = wp_cache_get('upkyk_phrases_table_status');
if (false === $table_status) {
// Direct DB query necessary here as WordPress doesn't provide a function to check if a table exists
// @codingStandardsIgnoreStart
$table_status = array(
'exists' => $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $phrases_table)) == $phrases_table,
'count' => 0
);
// @codingStandardsIgnoreEnd
if ($table_status['exists']) {
// Direct DB query necessary here as we're working with a custom table
// @codingStandardsIgnoreStart
$safe_table_name = esc_sql($phrases_table);
$table_status['count'] = $wpdb->get_var("SELECT COUNT(*) FROM `{$safe_table_name}`");
// @codingStandardsIgnoreEnd
}
wp_cache_set('upkyk_phrases_table_status', $table_status, '', 5 * MINUTE_IN_SECONDS);
}
if ($table_status['exists']) {
echo '<p style="color: green;">✓ ' . esc_html__('Table exists', 'upkyk-assistant-ai') . '</p>';
/* translators: %d: Number of custom phrases found */
echo '<p>' . sprintf(esc_html__('Contains %d phrases', 'upkyk-assistant-ai'), esc_html($table_status['count'])) . '</p>';
if ($table_status['count'] > 0) {
echo '<div style="margin-bottom: 10px;">';
echo '<button type="button" id="delete-selected-phrases" class="button button-secondary" style="background: #dd3333; color: white; border-color: #cc2222;">' . esc_html__('Delete Selected', 'upkyk-assistant-ai') . '</button>';
echo '</div>';
// Try to get phrases from cache
$phrases = wp_cache_get('upkyk_custom_phrases');
if (false === $phrases) {
// Direct DB query necessary here as we're working with a custom table
// @codingStandardsIgnoreStart
$safe_table_name = esc_sql($phrases_table);
$phrases = $wpdb->get_results(
"SELECT id, question, answer, category, created_at FROM `{$safe_table_name}` ORDER BY id DESC"
// Selecting specific columns is better than SELECT *
);
// @codingStandardsIgnoreEnd
wp_cache_set('upkyk_custom_phrases', $phrases, '', 5 * MINUTE_IN_SECONDS);
}
echo '<table class="widefat" style="margin-top: 10px;">';
echo '<thead><tr><th><input type="checkbox" id="select-all-phrases"></th><th>' . esc_html__('ID', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Question', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Answer', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Category', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Actions', 'upkyk-assistant-ai') . '</th></tr></thead>';
echo '<tbody>';
foreach ($phrases as $phrase) {
echo '<tr>';
echo '<td><input type="checkbox" class="phrase-checkbox" value="' . esc_attr($phrase->id) . '"></td>';
echo '<td>' . esc_html($phrase->id) . '</td>';
echo '<td>' . esc_html($phrase->question) . '</td>';
echo '<td>' . esc_html($phrase->answer) . '</td>';
echo '<td>' . esc_html($phrase->category) . '</td>';
echo '<td>';
echo '<button type="button" class="button button-small delete-phrase" data-id="' . esc_attr($phrase->id) . '" data-nonce="' . esc_attr($ajax_nonce) . '">' . esc_html__('Delete', 'upkyk-assistant-ai') . '</button>';
echo '</td>';
echo '</tr>';
}
echo '</tbody></table>';
} else {
echo '<p>' . esc_html__('No phrases found in the database.', 'upkyk-assistant-ai') . '</p>';
}
} else {
echo '<p style="color: red;">✗ ' . esc_html__('Table does not exist', 'upkyk-assistant-ai') . '</p>';
}
?>
</div>
</div>
</div>
<!-- Training Documents Tab -->
<div id="documents-tab" class="upkyk-tab-pane">
<div class="upkyk-section-intro">
<div class="label-with-badge">
<h2><?php esc_html_e('Training', 'upkyk-assistant-ai'); ?></h2>
<a href="https://upkyk.com/assistant-ai" class="pro-feature-badge" title="<?php esc_attr_e('Upgrade to Pro', 'upkyk-assistant-ai'); ?>" target="_blank" rel="noopener noreferrer">
<img src="<?php echo esc_url(UPKYK_ASSISTANT_AI_URL . 'assets/images/pro-upkyk-badge.svg'); ?>" alt="<?php esc_attr_e('PRO Feature', 'upkyk-assistant-ai'); ?>">
</a>
<div class="pro-feature-badge-info">
<?php esc_html_e('Training available in PRO version.', 'upkyk-assistant-ai'); ?>
</div>
</div>
<p><?php esc_html_e('Upload and manage training documents to improve your AssistantAI\'s knowledge base. These documents will be used to train your AI assistant to provide accurate responses.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-training-section upkyk-pro-teaser">
<div class="upkyk-section-header">
<h3><span class="dashicons dashicons-upload"></span> <?php esc_html_e('Upload Training Documents', 'upkyk-assistant-ai'); ?></span></h3>
</div>
<div class="upkyk-section-content pro-disabled">
<div class="upkyk-supported-formats">
<p><?php esc_html_e('Supported formats:', 'upkyk-assistant-ai'); ?> <span class="upkyk-format-badge">TXT</span> <span class="upkyk-format-badge">CSV</span> <span class="upkyk-format-badge">JSON</span></p>
<p><?php
/* translators: %s: Maximum allowed file upload size (e.g., 10MB) */
printf(esc_html__('Maximum file size: %s', 'upkyk-assistant-ai'), esc_html(size_format(wp_max_upload_size())));
?></p>
</div>
<div class="upkyk-upload-container">
<div class="upkyk-file-input">
<label class="button upkyk-choose-file button-primary disabled">
<span class="dashicons dashicons-upload"></span> <?php esc_html_e('Choose File to Upload', 'upkyk-assistant-ai'); ?>
</label>
<div id="selected-file-name"><?php esc_html_e('Available in PRO version', 'upkyk-assistant-ai'); ?></div>
<button type="button" class="button button-primary disabled" style="margin-top: 10px;">
<span class="dashicons dashicons-upload"></span> <?php esc_html_e('Upload Document', 'upkyk-assistant-ai'); ?>
</button>
</div>
</div>
</div>
</div>
<div class="upkyk-training-section upkyk-pro-teaser">
<div class="upkyk-section-header">
<h3>
<span class="dashicons dashicons-media-document"></span> <?php esc_html_e('Uploaded Documents', 'upkyk-assistant-ai'); ?>
</h3>
</div>
<div class="upkyk-section-content pro-disabled">
<div class="upkyk-list-header">
<div class="upkyk-list-search">
<input type="text" placeholder="<?php esc_attr_e('Search documents...', 'upkyk-assistant-ai'); ?>" disabled>
<span class="dashicons dashicons-search"></span>
</div>
</div>
<div class="upkyk-documents-container upkyk-greyed-out">
<?php
// Static placeholder table for documents (Core version)
echo '<table class="widefat" style="margin-top: 10px;">';
echo '<thead><tr><th>' . esc_html__('Name', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Type', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Size', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Date Added', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Status', 'upkyk-assistant-ai') . '</th><th>' . esc_html__('Actions', 'upkyk-assistant-ai') . '</th></tr></thead>';
echo '<tbody id="upkyk-documents-list">';
echo '<tr class="pro-placeholder-row">';
echo '<td colspan="6" class="pro-placeholder-cell">';
echo '<div class="pro-placeholder-content">';
echo '<span class="dashicons dashicons-lock"></span>';
echo '<p>' . esc_html__('Document management available in PRO version', 'upkyk-assistant-ai') . '</p>';
echo '</div>';
echo '</td>';
echo '</tr>';
echo '</tbody></table>';
?>
</div>
</div>
</div>
<div class="upkyk-action-bar-container upkyk-pro-teaser">
<div class="upkyk-action-bar">
<button class="button button-primary disabled">
<span class="dashicons dashicons-update"></span> <?php esc_html_e('Train Assistant AI', 'upkyk-assistant-ai'); ?>
</button>
<button class="button button-secondary disabled">
<span class="dashicons dashicons-trash"></span> <?php esc_html_e('Reset Training', 'upkyk-assistant-ai'); ?>
</button>
</div>
<div class="upkyk-training-progress">
<div class="upkyk-progress-status"></div>
</div>
</div>
</div>
<!-- Priority Keywords Tab -->
<div id="keywords-tab" class="upkyk-tab-pane">
<div class="upkyk-section-intro">
<div class="label-with-badge">
<h2><?php esc_html_e('Priority Keywords', 'upkyk-assistant-ai'); ?></h2>
<a href="https://upkyk.com/assistant-ai" class="pro-feature-badge" title="<?php esc_attr_e('Upgrade to Pro', 'upkyk-assistant-ai'); ?>" target="_blank" rel="noopener noreferrer">
<img src="<?php echo esc_url(UPKYK_ASSISTANT_AI_URL . 'assets/images/pro-upkyk-badge.svg'); ?>" alt="<?php esc_attr_e('PRO Feature', 'upkyk-assistant-ai'); ?>">
</a>
<div class="pro-feature-badge-info">
<?php esc_html_e('Priority Keywords available in PRO version.', 'upkyk-assistant-ai'); ?>
</div>
</div>
<p><?php esc_html_e('Define high-priority keywords that your AI should respond to with specific, pre-defined answers.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-keywords-section">
<div class="upkyk-form-container">
<form id="upkyk-add-keyword-form" method="post">
<input type="hidden" name="action" value="upkyk_add_keyword">
<input type="hidden" name="nonce" value="<?php echo esc_attr(wp_create_nonce('upkyk_assistant_ai_nonce')); ?>">
<div class="upkyk-form-field">
<label for="upkyk-keyword"><?php esc_html_e('Keyword or Phrase:', 'upkyk-assistant-ai'); ?></label>
<input type="text" id="upkyk-keyword" name="keyword" placeholder="<?php esc_attr_e('pricing', 'upkyk-assistant-ai'); ?>" disabled>
</div>
<div class="upkyk-form-field">
<label for="upkyk-keyword-priority"><?php esc_html_e('Priority Level:', 'upkyk-assistant-ai'); ?></label>
<select id="upkyk-keyword-priority" name="priority" disabled>
<option value="3"><?php esc_html_e('High (3)', 'upkyk-assistant-ai'); ?></option>
<option value="2"><?php esc_html_e('Medium (2)', 'upkyk-assistant-ai'); ?></option>
<option value="1"><?php esc_html_e('Low (1)', 'upkyk-assistant-ai'); ?></option>
</select>
</div>
<div class="upkyk-form-field">
<label for="upkyk-keyword-response"><?php esc_html_e('Response:', 'upkyk-assistant-ai'); ?></label>
<textarea id="upkyk-keyword-response" name="response" rows="3" placeholder="<?php esc_attr_e('Our pricing starts at $ per month for the basic plan.', 'upkyk-assistant-ai'); ?>" disabled></textarea>
</div>
<div class="upkyk-form-actions">
<button type="button" class="button button-primary" disabled><?php esc_html_e('Add Keyword', 'upkyk-assistant-ai'); ?></button>
</div>
</form>
</div>
</div>
<div id="keywords-database-diagnosis" style="margin-top: 20px; background: #f8f9fa; padding: 15px; border: 1px solid #e5e5e5; border-radius: 5px;">
<h3><?php esc_html_e('Keywords Table (PRO)', 'upkyk-assistant-ai'); ?></h3>
<p><?php esc_html_e('Add and manage priority keywords in the PRO version.', 'upkyk-assistant-ai'); ?></p>
</div>
</div>
<!-- Settings Tab -->
<div id="settings-tab" class="upkyk-tab-pane">
<div class="upkyk-section-intro">
<div class="label-with-badge">
<h2><?php esc_html_e('AI Behavior Settings', 'upkyk-assistant-ai'); ?></h2>
<a href="https://upkyk.com/assistant-ai" class="pro-feature-badge" title="<?php esc_attr_e('Upgrade to Pro', 'upkyk-assistant-ai'); ?>" target="_blank" rel="noopener noreferrer">
<img src="<?php echo esc_url(UPKYK_ASSISTANT_AI_URL . 'assets/images/pro-upkyk-badge.svg'); ?>" alt="<?php esc_attr_e('PRO Feature', 'upkyk-assistant-ai'); ?>">
</a>
<div class="pro-feature-badge-info">
<?php esc_html_e('Advanced Settings available in PRO version.', 'upkyk-assistant-ai'); ?>
</div>
</div>
<p><?php esc_html_e('Fine-tune how your AssistantAI responds to user queries by adjusting these parameters. These settings help balance accuracy, strictness, and creativity in responses.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-settings-section">
<form id="upkyk-ai-settings-form" method="post">
<input type="hidden" name="action" value="upkyk_save_training_settings">
<?php wp_nonce_field('upkyk_training_settings_nonce', 'upkyk_training_settings_nonce'); ?>
<div class="upkyk-form-field">
<label for="intent_sensitivity"><?php esc_html_e('Intent Recognition Sensitivity:', 'upkyk-assistant-ai'); ?></label>
<div class="upkyk-slider-container">
<span class="upkyk-slider-label"><?php esc_html_e('Strict', 'upkyk-assistant-ai'); ?></span>
<input type="range" id="intent_sensitivity" name="intent_sensitivity" min="0.1" max="1" step="0.1" value="<?php echo esc_attr($sensitivity); ?>" class="upkyk-slider" disabled>
<div class="upkyk-slider-value"><?php echo esc_html($sensitivity); ?></div>
<span class="upkyk-slider-label"><?php esc_html_e('Flexible', 'upkyk-assistant-ai'); ?></span>
</div>
<p class="description"><?php esc_html_e('Controls how loosely or strictly the assistant matches questions to trained content. Lower values focus on specific matches, higher values allow more flexibility in recognizing user intent.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-field">
<label for="confidence_threshold_enabled" class="upkyk-checkbox-label">
<input type="checkbox" id="confidence_threshold_enabled" name="confidence_threshold_enabled" <?php checked($confidence_threshold_enabled, '1'); ?> disabled>
<?php esc_html_e('Enable Confidence Threshold', 'upkyk-assistant-ai'); ?>
</label>
<p class="description"><?php esc_html_e('When enabled, the AI will only provide answers when it has sufficient confidence in its response.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-field" id="confidence_threshold_container" style="<?php echo esc_attr($confidence_threshold_enabled !== '1' ? 'display: none;' : ''); ?>">
<label for="confidence_threshold"><?php esc_html_e('Confidence Threshold:', 'upkyk-assistant-ai'); ?></label>
<div class="upkyk-slider-container">
<span class="upkyk-slider-label"><?php esc_html_e('Lenient', 'upkyk-assistant-ai'); ?></span>
<input type="range" id="confidence_threshold" name="confidence_threshold" min="10" max="90" step="5" value="<?php echo esc_attr($confidence_threshold); ?>" class="upkyk-slider" disabled>
<div class="upkyk-slider-value"><?php echo esc_html($confidence_threshold); ?>%</div>
<span class="upkyk-slider-label"><?php esc_html_e('Strict', 'upkyk-assistant-ai'); ?></span>
</div>
<p class="description"><?php esc_html_e('Sets the minimum confidence level required for the AI to provide an answer. If below this threshold, the fallback message will be used.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-field">
<label for="max_context_length"><?php esc_html_e('Max Context Length:', 'upkyk-assistant-ai'); ?></label>
<select id="max_context_length" name="max_context_length" disabled>
<option value="1" <?php selected($max_context_length, '1'); ?>><?php esc_html_e('1 (Short)', 'upkyk-assistant-ai'); ?></option>
<option value="2" <?php selected($max_context_length, '2'); ?>><?php esc_html_e('2 (Medium)', 'upkyk-assistant-ai'); ?></option>
<option value="3" <?php selected($max_context_length, '3'); ?>><?php esc_html_e('3 (Standard)', 'upkyk-assistant-ai'); ?></option>
<option value="4" <?php selected($max_context_length, '4'); ?>><?php esc_html_e('4 (Long)', 'upkyk-assistant-ai'); ?></option>
<option value="5" <?php selected($max_context_length, '5'); ?>><?php esc_html_e('5 (Maximum)', 'upkyk-assistant-ai'); ?></option>
</select>
<p class="description"><?php esc_html_e('Determines how many previous messages the AI uses for context when generating responses. Longer context provides more conversational awareness but uses more resources.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-field">
<label for="response_creativity"><?php esc_html_e('Response Creativity:', 'upkyk-assistant-ai'); ?></label>
<div class="upkyk-slider-container">
<span class="upkyk-slider-label"><?php esc_html_e('Consistent', 'upkyk-assistant-ai'); ?></span>
<input type="range" id="response_creativity" name="response_creativity" min="0" max="1" step="0.1" value="<?php echo esc_attr($response_creativity); ?>" class="upkyk-slider" disabled>
<div class="upkyk-slider-value"><?php echo esc_html($response_creativity); ?></div>
<span class="upkyk-slider-label"><?php esc_html_e('Creative', 'upkyk-assistant-ai'); ?></span>
</div>
<p class="description"><?php esc_html_e('Controls how creative or conservative the AI\'s responses will be. Lower values produce more consistent, predictable responses. Higher values allow more variety and creativity.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-field">
<label for="fallback_message"><?php esc_html_e('Fallback Message:', 'upkyk-assistant-ai'); ?></label>
<textarea id="fallback_message" name="fallback_message" rows="3" disabled><?php echo esc_textarea($fallback_message); ?></textarea>
<p class="description"><?php esc_html_e('Message displayed when the AI cannot provide a confident answer based on your training data.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-form-actions">
<button type="button" class="button button-primary" disabled><?php esc_html_e('Save AI Settings', 'upkyk-assistant-ai'); ?></button>
</div>
</form>
</div>
</div>
<!-- Test Chatbot Tab -->
<div id="test-tab" class="upkyk-tab-pane">
<div class="upkyk-section-intro">
<h2><?php esc_html_e('Test Your Assistant AI', 'upkyk-assistant-ai'); ?></h2>
<p><?php esc_html_e('Try out your trained AI assistant in a test environment. Ask questions and see how it responds based on your training data and custom phrases.', 'upkyk-assistant-ai'); ?></p>
</div>
<div class="upkyk-test-container">
<div class="upkyk-test-chatbox">
<div class="upkyk-test-messages" id="upkyk-test-messages">
<div class="upkyk-test-message bot">
<?php esc_html_e('Hello! I\'m your trained chatbot. Ask me a question to see how I respond.', 'upkyk-assistant-ai'); ?>
</div>
</div>
<div class="upkyk-test-input">
<input type="text" id="upkyk-test-input" placeholder="<?php esc_attr_e('Type your test question here...', 'upkyk-assistant-ai'); ?>">
<button id="upkyk-test-send" class="button"><?php esc_html_e('Send', 'upkyk-assistant-ai'); ?></button>
</div>
</div>
<!-- Response analysis container -->
<div class="upkyk-test-info">
<div class="upkyk-test-section">
<h3><?php esc_html_e('Response Analysis', 'upkyk-assistant-ai'); ?></h3>
<div id="upkyk-response-analysis">
<p><?php esc_html_e('Send a test message to see AI response details.', 'upkyk-assistant-ai'); ?></p>
</div>
</div>
<div class="upkyk-test-section">
<h3><?php esc_html_e('Question Tips', 'upkyk-assistant-ai'); ?></h3>
<ul class="upkyk-test-tips">
<li><?php esc_html_e('Try questions related to your uploaded documents', 'upkyk-assistant-ai'); ?></li>
<li><?php esc_html_e('Test variations of the same question to see how the matching works', 'upkyk-assistant-ai'); ?></li>
<li><?php esc_html_e('Ask follow-up questions to test the context memory', 'upkyk-assistant-ai'); ?></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Notification System -->
<div id="upkyk-notifications" class="upkyk-notifications"></div>
</div>
<?php
// End of the training page
?>