implemeted step 13 by Gemini

- added configuracion .env
- added test script for DAI API client
This commit is contained in:
2026-06-14 13:00:26 +02:00
parent 2b9b62b0aa
commit ec698f3f34
7 changed files with 241 additions and 0 deletions

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
# Production default
DAIAPI_URL=http://192.168.122.10:9001/run

2
.gitignore vendored
View File

@ -3,3 +3,5 @@
/data/llm/*.json /data/llm/*.json
/data/projects/*.json /data/projects/*.json
/data/users/*.json /data/users/*.json
/vendor/
.env

View File

@ -4,6 +4,9 @@ declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
// Load configuration
\App\Utils\Config::load(__DIR__ . '/../.env');
// Set headers // Set headers
header('Content-Type: application/json; charset=utf-8'); header('Content-Type: application/json; charset=utf-8');

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace App\Prompts;
class ContentPrompt
{
/**
* Generates a prompt for the AI based on wizard data.
*/
public function generate(array $wizardData): string
{
$businessName = $wizardData['identity']['business_name'] ?? 'neznáma firma';
$group = $wizardData['business_category']['group'] ?? 'univerzálne';
$subcategory = $wizardData['business_category']['subcategory'] ?? '';
$tagline = $wizardData['identity']['tagline'] ?? '';
$description = $wizardData['identity']['description'] ?? '';
$servicesItems = $wizardData['services']['items'] ?? [];
$servicesStr = "";
foreach ($servicesItems as $item) {
$servicesStr .= "- {$item['name']}: {$item['description']} (od {$item['price_from']} EUR)\n";
}
$smartAnswers = $wizardData['smart_answers'] ?? [];
$answersStr = "";
foreach ($smartAnswers as $id => $val) {
$valStr = is_bool($val) ? ($val ? 'Áno' : 'Nie') : $val;
$answersStr .= "- $id: $valStr\n";
}
$prompt = "Si profesionálny copywriter a marketingový špecialista. Tvojou úlohou je vytvoriť obsah pre novú webovú stránku pre klienta: \"$businessName\".\n\n";
$prompt .= "KONTEXT:\n";
$prompt .= "- Oblasť: $group / $subcategory\n";
$prompt .= "- Slogan: $tagline\n";
$prompt .= "- Popis: $description\n";
$prompt .= "- Ponúkané služby:\n$servicesStr\n";
$prompt .= "- Doplnujúce fakty:\n$answersStr\n\n";
$prompt .= "POŽIADAVKY NA OBSAH:\n";
$prompt .= "1. Píš v slovenskom jazyku, tónom, ktorý sa hodí pre daný segment (napr. priateľský pre kaviareň, profesionálny pre advokáta).\n";
$prompt .= "2. Vytvor SEO titulok a meta popis pre domovskú stránku.\n";
$prompt .= "3. Vytvor texty pre Hero sekciu (nadpis a podnadpis).\n";
$prompt .= "4. Vytvor sekciu 'O nás' (cca 3 odseky).\n";
$prompt .= "5. Rozšír popisy služieb na atraktívne marketingové texty.\n";
$prompt .= "6. Navrhni 3-5 otázok a odpovedí pre FAQ sekciu na základe kontextu.\n\n";
$prompt .= "STRIKTNÉ PRAVIDLÁ:\n";
$prompt .= "- Odpovedaj VÝHRADNE vo formáte JSON.\n";
$prompt .= "- V žiadnom prípade NEPOUŽÍVAJ HTML tagy (žiadne <div>, <p>, <h1> atď.).\n";
$prompt .= "- Obsah musí byť pripravený na priame vloženie do šablóny.\n\n";
$prompt .= "FORMÁT ODPOVEDE (JSON):\n";
$prompt .= "{\n";
$prompt .= " \"seo\": { \"title\": \"...\", \"description\": \"...\" },\n";
$prompt .= " \"hero\": { \"title\": \"...\", \"subtitle\": \"...\" },\n";
$prompt .= " \"about\": { \"title\": \"O nás\", \"text\": \"...\" },\n";
$prompt .= " \"services\": [\n";
$prompt .= " { \"id\": \"...\", \"title\": \"...\", \"text\": \"...\", \"price_info\": \"...\" }\n";
$prompt .= " ],\n";
$prompt .= " \"faq\": [\n";
$prompt .= " { \"question\": \"...\", \"answer\": \"...\" }\n";
$prompt .= " ]\n";
$prompt .= "}\n";
return $prompt;
}
}

View File

@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
class DAIClient
{
private string $apiUrl;
private int $timeout = 60;
public function __construct(?string $apiUrl = null)
{
if ($apiUrl !== null) {
$this->apiUrl = $apiUrl;
} else {
$this->apiUrl = \App\Utils\Config::get('DAIAPI_URL', 'http://192.168.122.10:9001/run');
}
}
/**
* Sends a prompt to the DAIAPI and returns the raw answer string.
*/
public function sendRequest(string $prompt): ?string
{
$payload = json_encode([
'prompt' => $prompt
]);
if ($payload === false) {
error_log("DAIClient Error: Failed to encode JSON payload.");
return null;
}
$ch = curl_init($this->apiUrl);
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 => $this->timeout,
]);
$response = curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response === false) {
error_log("DAIClient Error: cURL error: $error");
return null;
}
if ($httpCode !== 200) {
error_log("DAIClient Error: API returned HTTP code $httpCode. Response: $response");
return null;
}
$data = json_decode($response, true);
if (!is_array($data) || empty($data['success']) || !isset($data['answer'])) {
error_log("DAIClient Error: Invalid API response format: " . $response);
return null;
}
return $data['answer'];
}
}

49
src/Utils/Config.php Normal file
View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Utils;
class Config
{
/**
* Loads environment variables from a .env file.
*/
public static function load(string $path): void
{
if (!file_exists($path)) {
return;
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value);
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
}
}
}
/**
* Retrieves an environment variable or a default value.
*/
public static function get(string $key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return $default;
}
return $value;
}
}

44
tests/debug_dai.php Normal file
View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
// Load configuration
\App\Utils\Config::load(__DIR__ . '/../.env');
use App\Services\DAIClient;
/**
* CLI Debug skript pre overenie konektivity k DAIAPI
*/
echo "--- DAIAPI Connectivity Test ---" . PHP_EOL;
$client = new DAIClient();
$testPrompt = "Ahoj, aký je dnes deň a napíš mi v jednej krátkej vete, čo si o sebe myslíš ako o AI?";
echo "Odosielam testovací prompt: \"$testPrompt\"" . PHP_EOL;
echo "Čakám na odpoveď (timeout 60s)..." . PHP_EOL;
$startTime = microtime(true);
$answer = $client->sendRequest($testPrompt);
$endTime = microtime(true);
$duration = round($endTime - $startTime, 2);
echo PHP_EOL;
if ($answer !== null) {
echo "[SUCCESS] Pripojenie je funkčné!" . PHP_EOL;
echo "[INFO] Čas odpovede: {$duration}s" . PHP_EOL;
echo "--- ODPOVEĎ OD AI ---" . PHP_EOL;
echo $answer . PHP_EOL;
echo "---------------------" . PHP_EOL;
} else {
echo "[ERROR] Nepodarilo sa získať odpoveď od DAIAPI." . PHP_EOL;
echo "[DEBUG] Skontrolujte, či:" . PHP_EOL;
echo "1. Ste pripojený k VPN/sieti, kde beží DAIAPI." . PHP_EOL;
echo "2. Adresa http://192.168.122.10:9001 je dostupná." . PHP_EOL;
echo "3. API server nie je preťažený." . PHP_EOL;
}