38960-vm/api/ai_report.php
2026-03-04 17:42:06 +00:00

43 lines
1.5 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../ai/LocalAIApi.php';
// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
$symptoms = $input['symptoms'] ?? '';
$diagnosis = $input['diagnosis'] ?? '';
if (empty($symptoms) && empty($diagnosis)) {
echo json_encode(['success' => false, 'error' => 'No symptoms or diagnosis provided.']);
exit;
}
$prompt = "You are a professional medical assistant. Based on the following symptoms and diagnosis, please generate a concise treatment plan and medical report for the patient.\n\n";
if (!empty($symptoms)) {
$prompt .= "Symptoms:\n" . strip_tags($symptoms) . "\n\n";
}
if (!empty($diagnosis)) {
$prompt .= "Diagnosis:\n" . strip_tags($diagnosis) . "\n\n";
}
$prompt .= "Please provide the report in a clear, professional format using HTML tags (like <ul>, <li>, <strong>, etc.) for better readability.";
try {
$response = LocalAIApi::createResponse([
'input' => [
['role' => 'system', 'content' => 'You are a helpful medical assistant.'],
['role' => 'user', 'content' => $prompt],
],
]);
if (!empty($response['success'])) {
$text = LocalAIApi::extractText($response);
echo json_encode(['success' => true, 'report' => $text]);
} else {
echo json_encode(['success' => false, 'error' => $response['error'] ?? 'AI generation failed.']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}