|
@@ -0,0 +1,67 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+require_once 'CsvReader.php';
|
|
|
|
|
+
|
|
|
|
|
+header('Content-Type: application/json');
|
|
|
|
|
+
|
|
|
|
|
+// only accept POST requests
|
|
|
|
|
+if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
|
|
|
+ http_response_code(405);
|
|
|
|
|
+ echo json_encode(['error' => 'Only POST allowed']);
|
|
|
|
|
+ exit;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function extractPdfText($file) {
|
|
|
|
|
+ $tmp = tempnam(sys_get_temp_dir(), 'pdftext');
|
|
|
|
|
+ exec("pdftotext " . escapeshellarg($file) . " " . escapeshellarg($tmp));
|
|
|
|
|
+ $text = file_get_contents($tmp);
|
|
|
|
|
+ unlink($tmp);
|
|
|
|
|
+ return $text ?: '';
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ### 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) : '';
|
|
|
|
|
+
|
|
|
|
|
+$reader = new CsvReader(__DIR__ . '/data/csv');
|
|
|
|
|
+$rows = $reader->readAll();
|
|
|
|
|
+$contextText = "";
|
|
|
|
|
+foreach ($rows as $row) {
|
|
|
|
|
+ $contextText .= "- " . implode(" | ", $row) . "\n";
|
|
|
|
|
+}
|
|
|
|
|
+$pdf1 = extractPdfText(__DIR__ . '/data/pdf/onboarding.pdf');
|
|
|
|
|
+$pdf2 = extractPdfText(__DIR__ . '/data/pdf/urlaub.pdf');
|
|
|
|
|
+
|
|
|
|
|
+$pdfContext = "\n\nAus Onboarding-Dokument:\n" . $pdf1 . "\n\nAus Urlaubsdokument:\n" . $pdf2;
|
|
|
|
|
+
|
|
|
|
|
+$prompt = "Deine Rolle ist: ". $role;
|
|
|
|
|
+$prompt .= "\nDeine Frage ist: " . $question;
|
|
|
|
|
+$prompt .= "\nCSV-Daten:\n" . $contextText;
|
|
|
|
|
+$prompt .= "\nPDF-Kontext:\n" . $pdfContext;
|
|
|
|
|
+
|
|
|
|
|
+$payload = json_encode([
|
|
|
|
|
+ "model" => "deepseek-r1",
|
|
|
|
|
+ "prompt" => $prompt,
|
|
|
|
|
+ "stream" => false
|
|
|
|
|
+]);
|
|
|
|
|
+
|
|
|
|
|
+// ### Send Request
|
|
|
|
|
+$ch = curl_init('http://localhost:11434/api/generate');
|
|
|
|
|
+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);
|
|
|
|
|
+
|
|
|
|
|
+// ### Prepare Response
|
|
|
|
|
+$responseText = isset($data['response']) ? $data['response'] : 'No answer from Deepseek!';
|
|
|
|
|
+$responseText = preg_replace('/<think>.*?<\/think>/s', ' ', $responseText);
|
|
|
|
|
+$responseText = trim($responseText);
|
|
|
|
|
+
|
|
|
|
|
+echo json_encode([
|
|
|
|
|
+ 'reply' => $responseText,
|
|
|
|
|
+ 'misc' => $pdfContext
|
|
|
|
|
+]);
|