Compare commits
2 Commits
3849e0572c
...
ed7dfe7795
| Author | SHA1 | Date | |
|---|---|---|---|
| ed7dfe7795 | |||
| 3c6344e94f |
@ -36,9 +36,51 @@ Implementovať klienta pre DAIAPI a vytvoriť generátor promptov, ktorý na zá
|
||||
Žiadne.
|
||||
|
||||
## API a dátové štruktúry
|
||||
DAIAPI typicky očakáva:
|
||||
- URL: `http://webwizard.test:port/v1/chat/completions` (podľa lokálneho nastavenia).
|
||||
- Formát: JSON (messages, temperature, atď.).
|
||||
DAIAPI je proprietarne API, ktore sa da pouzivat volanim na VPN adresu 10.2.8.1 a port 9001, prakticka implementacia v PHP:
|
||||
|
||||
```php
|
||||
function daiAPIrun(string $prompt): ?string
|
||||
{
|
||||
$url = "http://192.168.122.10:9001/run";
|
||||
$payload = json_encode([
|
||||
"prompt" => $prompt
|
||||
]);
|
||||
if ($payload === false) {
|
||||
return null;
|
||||
}
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Content-Type: application/json",
|
||||
"Content-Length: " . strlen($payload)
|
||||
],
|
||||
CURLOPT_POSTFIELDS => $payload,
|
||||
CURLOPT_TIMEOUT => 60,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
if ($response === false) {
|
||||
curl_close($ch);
|
||||
return null;
|
||||
}
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
if ($httpCode !== 200) {
|
||||
return null;
|
||||
}
|
||||
$data = json_decode($response, true);
|
||||
if (!is_array($data)) {
|
||||
return null;
|
||||
}
|
||||
// očakávame: { "success": true, "answer": "...", ... }
|
||||
if (empty($data["success"]) || !isset($data["answer"])) {
|
||||
return null;
|
||||
}
|
||||
return $data["answer"];
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Frontend požiadavky
|
||||
Nerelevantné.
|
||||
|
||||
69
public/ajax.php
Normal file
69
public/ajax.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Set headers
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Error handling
|
||||
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
|
||||
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
|
||||
});
|
||||
|
||||
function sendResponse(bool $success, $dataOrError, int $httpStatus = 200): void {
|
||||
http_response_code($httpStatus);
|
||||
$response = ['success' => $success];
|
||||
if ($success) {
|
||||
$response['data'] = $dataOrError;
|
||||
} else {
|
||||
$response['error'] = $dataOrError;
|
||||
}
|
||||
echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// Only POST allowed
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
sendResponse(false, ['code' => 'METHOD_NOT_ALLOWED', 'message' => 'Only POST requests are allowed.'], 405);
|
||||
}
|
||||
|
||||
// Read JSON input
|
||||
$input = file_get_contents('php://input');
|
||||
$data = json_decode($input, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
sendResponse(false, ['code' => 'INVALID_JSON', 'message' => 'Invalid JSON input.'], 400);
|
||||
}
|
||||
|
||||
// Validate action
|
||||
$action = $data['action'] ?? null;
|
||||
if (!$action) {
|
||||
sendResponse(false, ['code' => 'MISSING_ACTION', 'message' => 'Action is required.'], 400);
|
||||
}
|
||||
|
||||
// Check X-User-ID header (except for initSession if we want to allow it)
|
||||
$userId = $_SERVER['HTTP_X_USER_ID'] ?? null;
|
||||
if (!$userId && $action !== 'initSession') {
|
||||
sendResponse(false, ['code' => 'UNAUTHORIZED', 'message' => 'X-User-ID header is missing.'], 401);
|
||||
}
|
||||
|
||||
// Router
|
||||
switch ($action) {
|
||||
case 'ping':
|
||||
sendResponse(true, ['message' => 'pong', 'timestamp' => time()]);
|
||||
break;
|
||||
|
||||
default:
|
||||
sendResponse(false, ['code' => 'UNKNOWN_ACTION', 'message' => "Action '$action' is not defined."], 404);
|
||||
break;
|
||||
}
|
||||
|
||||
} catch (Throwable $e) {
|
||||
sendResponse(false, [
|
||||
'code' => 'INTERNAL_SERVER_ERROR',
|
||||
'message' => $e->getMessage()
|
||||
], 500);
|
||||
}
|
||||
Reference in New Issue
Block a user