'Could not connect to Ollama server.']); } else { echo $response; } curl_close($ch); exit; } // 2. Handle sending chat messages if ($_SERVER['REQUEST_METHOD'] === 'POST') { $input = json_decode(file_get_contents('php://input'), true); $model = $input['model'] ?? 'llama3'; $messages = $input['messages'] ?? []; $payload = json_encode([ 'model' => $model, 'messages' => $messages, 'stream' => false ]); $ch = curl_init(OLLAMA_URL . '/api/chat'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $response = curl_exec($ch); if (curl_errno($ch)) { echo json_encode(['error' => 'Error communicating with remote Ollama: ' . curl_error($ch)]); } else { echo $response; } curl_close($ch); exit; } echo json_encode(['error' => 'Invalid request']);