36557-vm/ai_agent.php
Flatlogic Bot 08fcb2dae0 0.2
2025-12-01 21:25:15 +00:00

34 lines
1.0 KiB
PHP

<?php
require_once __DIR__ . '/ai/LocalAIApi.php';
$request_body = file_get_contents('php://input');
$data = json_decode($request_body, true);
$user_message = $data['message'];
if (empty($user_message)) {
echo json_encode(['reply' => 'Please provide a message.']);
exit;
}
$resp = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a helpful assistant for a real estate MLM company called Kutumbh Infra.'],
['role' => 'user', 'content' => $user_message],
],
]);
if (!empty($resp['success'])) {
$text = LocalAIApi::extractText($resp);
if ($text === '') {
$decoded = LocalAIApi::decodeJsonFromResponse($resp);
$text = $decoded ? json_encode($decoded, JSON_UNESCAPED_UNICODE) : (string)($resp['data'] ?? '');
}
$aiReply = $text;
} else {
error_log('AI error: ' . ($resp['error'] ?? 'unknown'));
$aiReply = 'Sorry, I am having trouble connecting to the AI service.';
}
header('Content-Type: application/json');
echo json_encode(['reply' => $aiReply]);