You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

<?php
// CHANGE THIS to your remote Ollama server IP or domain (e.g., http://192.168.1.50:11434)
define('OLLAMA_URL', 'http://10.2.0.3:11434');
header('Content-Type: application/json');
// 1. Handle fetching available models
if (isset($_GET['action']) && $_GET['action'] === 'models') {
$ch = curl_init(OLLAMA_URL . '/api/tags');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo json_encode(['error' => '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']);