| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- require_once 'CSVReader.php';
- require_once 'PDFReader.php';
- require_once 'PromptBuilder.php';
- // 2 Minuten erlauben
- set_time_limit(2500);
- 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;
- }
- // 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)
- ]);
|