38751-vm/app/Controllers/AIController.php
Flatlogic Bot dc6297b93b Bagus ini
2026-02-25 22:41:29 +00:00

40 lines
1.4 KiB
PHP

<?php
namespace App\Controllers;
use App\Core\Controller;
use App\Services\LanguageService;
class AIController extends Controller {
public function chat() {
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
$userMessage = $input['message'] ?? '';
if (empty($userMessage)) {
echo json_encode(['error' => __('error_empty_message', 'Message is empty')]);
return;
}
require_once __DIR__ . '/../../ai/LocalAIApi.php';
$langName = LanguageService::getLang() == 'id' ? 'Indonesian' : 'English';
$systemPrompt = "You are a helpful assistant for " . get_setting('site_name', 'ApkNusa') . ", an APK downloader and tech blog site. Provide concise and accurate information about Android apps, games, and technology. Be youthful and professional. IMPORTANT: Always respond in " . $langName . ".";
$resp = \LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => $systemPrompt],
['role' => 'user', 'content' => $userMessage],
],
]);
if (!empty($resp['success'])) {
$text = \LocalAIApi::extractText($resp);
echo json_encode(['reply' => $text]);
} else {
echo json_encode(['error' => __('error_ai', 'AI Assistant is currently unavailable.')]);
}
}
}