'Only POST allowed']); exit; } // Build prompt for deepseek $input = json_decode(file_get_contents('php://input'), true); $question = isset($input['question']) ? $input['question'] : ''; $roleFile = __DIR__ . '/data/role.txt'; $role = file_exists($roleFile) ? file_get_contents($roleFile) : ''; // Reading CSV data $csvReader = new CSVReader(__DIR__ . '/data/csv'); $csvRows = $csvReader->readAll(); $contextText = $csvReader->toString($csvRows); // Reading PDF data $pdfReader = new PDFReader(__DIR__ . '/data/pdf'); $pdfData = $pdfReader->readAll(); $pdfContext = $pdfReader->toString($pdfData); // TODO: Read HTML data $htmlContext = ''; $prompt = new PromptBuilder(); $prompt->setRole($role); $prompt->setQuestion($question); $promptContext = "\nCSV-Kontext:\n" . $contextText; $promptContext .= "\nPDF-Kontext:\n" . $pdfContext; $promptContext .= "\nHTML-Wissensdatenbank:\n" . $htmlContext; $prompt->setContext($contextText); $fullPrompt = $prompt->buildPromptString(); error_log("System prompt length: " . strlen($fullPrompt)); $payload = $prompt->buildPromptData(); //Starting Prompt Generation $start = microtime(true); $ch = curl_init('http://localhost:1234/api/v0/chat/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); $end = microtime(true); $responseText = isset($data['choices'][0]['message']['content']) ? $data['choices'][0]['message']['content'] : ''; $responseText = trim($responseText); echo json_encode([ 'reply' => $responseText, 'duration_seconds' => round($end - $start, 3) ]);