This commit is contained in:
Flatlogic Bot 2026-02-26 19:54:27 +00:00
parent 3f421e9a53
commit 0c23b971b3
9 changed files with 51 additions and 18 deletions

View File

@ -28,6 +28,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$head_ads = $_POST['head_ads'] ?? '';
$body_ads = $_POST['body_ads'] ?? '';
$openai_api_key = $_POST['openai_api_key'] ?? '';
$openai_model = $_POST['openai_model'] ?? 'gpt-4o-mini';
$site_name = $_POST['site_name'] ?? 'TikTok Live AI Assistant';
$default_language = $_POST['default_language'] ?? 'en';
@ -57,6 +58,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
'head_ads' => $head_ads,
'body_ads' => $body_ads,
'openai_api_key' => $openai_api_key,
'openai_model' => $openai_model,
'site_name' => $site_name,
'site_icon' => $site_icon,
'site_favicon' => $site_favicon,
@ -77,6 +79,7 @@ $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
$head_ads = $settings['head_ads'] ?? '';
$body_ads = $settings['body_ads'] ?? '';
$openai_api_key = $settings['openai_api_key'] ?? '';
$openai_model = $settings['openai_model'] ?? 'gpt-4o-mini';
$site_name = $settings['site_name'] ?? 'TikTok Live AI Assistant';
$site_icon = $settings['site_icon'] ?? 'assets/images/logo.png';
$site_favicon = $settings['site_favicon'] ?? 'favicon.ico';
@ -165,12 +168,22 @@ $default_language = $settings['default_language'] ?? 'en';
<hr class="border-secondary my-2">
</div>
<div class="col-12">
<div class="col-md-6">
<label for="openai_api_key" class="form-label fw-semibold text-secondary small"><?= __('openai_key') ?></label>
<input type="password" name="openai_api_key" id="openai_api_key" class="form-control" placeholder="sk-..." value="<?= htmlspecialchars($openai_api_key) ?>">
<div class="form-text text-muted">If left empty, the application will use the Flatlogic AI Proxy.</div>
</div>
<div class="col-md-6">
<label for="openai_model" class="form-label fw-semibold text-secondary small"><?= __('openai_model') ?></label>
<select name="openai_model" id="openai_model" class="form-select">
<option value="gpt-4o-mini" <?= $openai_model === 'gpt-4o-mini' ? 'selected' : '' ?>>GPT-4o Mini (Default)</option>
<option value="o3-mini" <?= $openai_model === 'o3-mini' ? 'selected' : '' ?>>o3-mini (Reasoning)</option>
<option value="gpt-4o" <?= $openai_model === 'gpt-4o' ? 'selected' : '' ?>>GPT-4o (High Quality)</option>
</select>
<div class="form-text text-muted">Choose which model to use with your API Key.</div>
</div>
<div class="col-md-6">
<label for="head_ads" class="form-label fw-semibold text-secondary small"><?= __('head_scripts') ?></label>
<textarea name="head_ads" id="head_ads" rows="4" class="form-control" placeholder="Paste your <head> scripts here..."><?= htmlspecialchars($head_ads) ?></textarea>

View File

@ -41,9 +41,9 @@ class LocalAIApi
];
}
// Use custom model if provided, else use default from config
// Use custom model if provided, else use default from site_settings or config
if (!isset($payload['model']) || $payload['model'] === '') {
$payload['model'] = !empty($cfg['openai_api_key']) ? 'gpt-4o-mini' : $cfg['default_model'];
$payload['model'] = !empty($cfg['openai_model']) ? $cfg['openai_model'] : (!empty($cfg['openai_api_key']) ? 'gpt-4o-mini' : $cfg['default_model']);
}
// If we have a custom OpenAI API key, we call OpenAI directly (synchronously)
@ -386,18 +386,22 @@ class LocalAIApi
throw new RuntimeException('Invalid AI config format: expected array');
}
// Try to load custom API key from DB if possible
// Try to load custom API key and model from DB if possible
$dbConfig = __DIR__ . '/../db/config.php';
if (file_exists($dbConfig)) {
try {
require_once $dbConfig;
$pdo = db();
$stmt = $pdo->query("SELECT setting_value FROM site_settings WHERE setting_key = 'openai_api_key' LIMIT 1");
$val = $stmt->fetchColumn();
$cfg['openai_api_key'] = $val ?: null;
// Fetch all relevant settings in one go
$stmt = $pdo->query("SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('openai_api_key', 'openai_model')");
while ($row = $stmt->fetch()) {
$cfg[$row['setting_key']] = $row['setting_value'] ?: null;
}
} catch (Exception $e) {
// Fail silently, fallback to proxy
// Fail silently, fallback to defaults
$cfg['openai_api_key'] = null;
$cfg['openai_model'] = null;
}
}

View File

@ -24,7 +24,7 @@ if (
if ($key === '') {
continue;
}
$value = trim($value, "\"' ");
$value = trim($value, "' ");
if (getenv($key) === false || getenv($key) === '') {
putenv("{$key}={$value}");
}
@ -46,7 +46,7 @@ return [
'project_id' => $projectId,
'project_uuid' => $projectUuid,
'project_header' => 'project-uuid',
'default_model' => 'gpt-5-mini',
'default_model' => 'gpt-4o-mini',
'timeout' => 30,
'verify_tls' => true,
];
];

View File

@ -32,7 +32,7 @@ try {
// 3. Call AI API
$response = LocalAIApi::createResponse([
'model' => 'gpt-4o-mini',
// Model is handled by LocalAIApi defaults/site_settings if omitted
'input' => [
['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $message],
@ -61,4 +61,4 @@ try {
} catch (Exception $e) {
error_log("Chat Error: " . $e->getMessage());
echo json_encode(['reply' => "An internal error occurred."]);
}
}

View File

@ -19,7 +19,7 @@ if (empty($text)) {
}
// Get Telegram Token from DB
$stmt = db()->query("SELECT setting_value FROM settings WHERE setting_key = 'telegram_token'");
$stmt = db()->query("SELECT setting_value FROM site_settings WHERE setting_key = 'telegram_token'");
$token = $stmt->fetchColumn();
if (!$token) {
@ -64,7 +64,7 @@ try {
// 2. Call AI
$response = LocalAIApi::createResponse([
'model' => 'gpt-4o-mini',
// Model is handled by LocalAIApi defaults/site_settings if omitted
'input' => [
['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $text],
@ -88,4 +88,4 @@ try {
} catch (Exception $e) {
error_log("Telegram Webhook Error: " . $e->getMessage());
}
}

View File

@ -0,0 +1 @@
INSERT INTO site_settings (setting_key, setting_value) VALUES ('openai_model', 'gpt-4o-mini') ON DUPLICATE KEY UPDATE setting_key=setting_key;

View File

@ -0,0 +1,13 @@
CREATE TABLE IF NOT EXISTS faqs (
id INT AUTO_INCREMENT PRIMARY KEY,
keywords TEXT NOT NULL,
answer TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS messages (
id INT AUTO_INCREMENT PRIMARY KEY,
user_message TEXT NOT NULL,
ai_response TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

View File

@ -30,6 +30,7 @@ return [
'save_settings' => 'Save All Settings',
'site_settings' => 'Site Settings',
'openai_key' => 'OpenAI API Key',
'openai_model' => 'OpenAI Model',
'head_scripts' => 'Head Scripts',
'body_scripts' => 'Body Scripts',
'site_name' => 'Site Name',
@ -40,4 +41,4 @@ return [
'personality_funny' => 'Funny',
'personality_serious' => 'Serious',
'personality_expert' => 'Expert',
];
];

View File

@ -30,6 +30,7 @@ return [
'save_settings' => 'Simpan Semua Pengaturan',
'site_settings' => 'Pengaturan Situs',
'openai_key' => 'Kunci API OpenAI',
'openai_model' => 'Model OpenAI',
'head_scripts' => 'Skrip Head',
'body_scripts' => 'Skrip Body',
'site_name' => 'Nama Situs',
@ -40,4 +41,4 @@ return [
'personality_funny' => 'Lucu',
'personality_serious' => 'Serius',
'personality_expert' => 'Pakar',
];
];