<?php
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
if (!isset($input['message']) || empty(trim($input['message']))) {
http_response_code(400);
echo json_encode(['error' => 'No message provided']);
exit;
}
$userMessage = trim($input['message']);
$payload = [
"model" => "tinyllama",
"prompt" => $userMessage,
"stream" => false
];
$ch = curl_init('http://localhost:11434/api/generate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($response === false) {
http_response_code(500);
echo json_encode(['error' => 'Curl error', 'detail' => $curlError]);
exit;
}
if ($httpCode !== 200) {
http_response_code($httpCode);
echo json_encode([
'error' => 'Model request failed',
'status' => $httpCode,
'body' => $response
]);
exit;
}
$data = json_decode($response, true);
if (!isset($data['response'])) {
echo json_encode([
'error' => 'Model did not return a valid response',
'raw' => $data
]);
exit;
}
echo json_encode([
'response' => trim($data['response'])
]);
view raw back.php hosted with ❤ by GitHub