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.
92 lines
2.8 KiB
92 lines
2.8 KiB
<?php
|
|
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/default.php';
|
|
require_once __DIR__ . '/auth.php';
|
|
|
|
function appendChatAuditEntry($username, $chatMessages, $resultPayload, $ipAddress = null) {
|
|
ensureConfigDirectory();
|
|
|
|
$auditPath = USER_CONFIG_DIR . '/chat-requests.json';
|
|
$entries = [];
|
|
|
|
if (file_exists($auditPath)) {
|
|
$raw = file_get_contents($auditPath);
|
|
if ($raw !== false) {
|
|
$decoded = json_decode($raw, true);
|
|
if (is_array($decoded)) {
|
|
$entries = $decoded;
|
|
}
|
|
}
|
|
}
|
|
|
|
$entries[] = [
|
|
'timestamp' => date('c'),
|
|
'username' => (string) $username,
|
|
'ipAddress' => (string) ($ipAddress ?? ($_SERVER['REMOTE_ADDR'] ?? '')),
|
|
'chat' => is_array($chatMessages) ? $chatMessages : [$chatMessages],
|
|
'result' => $resultPayload
|
|
];
|
|
|
|
$payload = json_encode($entries, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
return $payload !== false && file_put_contents($auditPath, $payload . PHP_EOL) !== false;
|
|
}
|
|
|
|
// 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'] ?? [];
|
|
$username = '';
|
|
if (!empty($_SESSION['auth_user']['username'])) {
|
|
$username = sanitizeUsername($_SESSION['auth_user']['username']);
|
|
}
|
|
|
|
$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);
|
|
$resultPayload = null;
|
|
|
|
if (curl_errno($ch)) {
|
|
$resultPayload = ['error' => 'Error communicating with remote Ollama: ' . curl_error($ch)];
|
|
echo json_encode($resultPayload);
|
|
} else {
|
|
$decodedResponse = json_decode($response, true);
|
|
$resultPayload = $decodedResponse !== null ? $decodedResponse : $response;
|
|
echo $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
appendChatAuditEntry($username, $messages, $resultPayload, $_SERVER['REMOTE_ADDR'] ?? '');
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['error' => 'Invalid request']);
|
|
|