81 lines
3.3 KiB
PHP
81 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* ambre-gen-pipeline.php · AMBRE · pipeline complet chat → LLM markdown → pandoc file
|
|
* Usage: GET ?type=pdf|docx|pptx&topic=... (from chat intent cmd)
|
|
* Output: text response with file URL embedded
|
|
*/
|
|
header("Content-Type: text/plain; charset=utf-8");
|
|
|
|
$type = strtolower(trim($_GET["type"] ?? "pdf"));
|
|
$topic = trim($_GET["topic"] ?? "");
|
|
|
|
if (!$topic) { echo "❌ topic required"; exit; }
|
|
$allowed = ["pdf","docx","pptx","html","epub"];
|
|
if (!in_array($type, $allowed)) { echo "❌ type invalide, allowed: " . implode("|", $allowed); exit; }
|
|
|
|
// === 1. Call LLM to generate clean markdown ===
|
|
$sys_prompt = "Tu es un generateur de contenu professionnel. Réponds UNIQUEMENT en markdown pur, pas de preambule, pas de meta-commentaire. Pour les PPTX, utilise # pour chaque slide title. Français par défaut.";
|
|
|
|
$user_prompt = match($type) {
|
|
"pdf" => "Génère un document PDF professionnel, structuré, complet (2-3 pages) sur: $topic. Titres, sections, bullets.",
|
|
"docx" => "Génère un document Word professionnel, structuré sur: $topic. Titres, sections, tableaux si pertinent.",
|
|
"pptx" => "Génère une présentation de 5-7 slides sur: $topic. Chaque slide commence par # (titre slide). Bullets concises.",
|
|
"html" => "Génère un document HTML propre sur: $topic.",
|
|
default => "Génère un document sur: $topic.",
|
|
};
|
|
|
|
$llm_raw = @file_get_contents("http://127.0.0.1:4000/v1/chat/completions", false, stream_context_create([
|
|
"http" => [
|
|
"method" => "POST",
|
|
"header" => "Content-Type: application/json\r\n",
|
|
"content" => json_encode([
|
|
"model" => "fast",
|
|
"messages" => [
|
|
["role"=>"system", "content"=>$sys_prompt],
|
|
["role"=>"user", "content"=>$user_prompt],
|
|
],
|
|
"max_tokens" => 2500,
|
|
"temperature" => 0.5,
|
|
"stream" => false,
|
|
]),
|
|
"timeout" => 30,
|
|
],
|
|
]));
|
|
|
|
$llm_d = @json_decode($llm_raw, true);
|
|
$md_content = $llm_d["choices"][0]["message"]["content"] ?? "";
|
|
|
|
if (!$md_content) { echo "❌ LLM failed to generate content"; exit; }
|
|
|
|
// === 2. Call ambre-doc-gen to create the file ===
|
|
$title = substr($topic, 0, 60);
|
|
$gen_ctx = stream_context_create([
|
|
"http" => [
|
|
"method" => "POST",
|
|
"header" => "Content-Type: application/json\r\n",
|
|
"content" => json_encode([
|
|
"type" => $type,
|
|
"title" => $title,
|
|
"content" => $md_content,
|
|
]),
|
|
"timeout" => 45,
|
|
],
|
|
]);
|
|
$gen_raw = @file_get_contents("http://127.0.0.1/api/ambre-doc-gen.php", false, $gen_ctx);
|
|
$gen_d = @json_decode($gen_raw, true);
|
|
|
|
if (!$gen_d || empty($gen_d["ok"])) {
|
|
echo "❌ Generation failed: " . ($gen_d["error"] ?? "unknown") . "\n";
|
|
echo "MD content was: " . substr($md_content, 0, 200);
|
|
exit;
|
|
}
|
|
|
|
// === 3. Output rich response ===
|
|
$icon = match($type) { "pdf"=>"📄", "docx"=>"📝", "pptx"=>"🎯", "html"=>"🌐", default=>"📎" };
|
|
echo "$icon **$title** généré\n\n";
|
|
echo "🔗 Télécharger: " . $gen_d["full_url"] . "\n";
|
|
echo "📦 Taille: " . $gen_d["size_human"] . " · ⚙️ " . $gen_d["elapsed_ms"] . "ms · engine: " . $gen_d["engine"] . "\n\n";
|
|
echo "---\n\nAperçu contenu:\n\n";
|
|
echo substr($md_content, 0, 800);
|
|
if (strlen($md_content) > 800) echo "\n\n... [document complet dans le fichier]";
|