71 lines
5.4 KiB
PHP
71 lines
5.4 KiB
PHP
<?php
|
|
/**
|
|
* ambre-thinking.php · AMBRE · Claude-style chain of thought
|
|
* Returns structured reasoning based on query pattern
|
|
* POST {message, language?} → {thinking: "step1 step2 step3..."}
|
|
*/
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
|
|
$raw = file_get_contents("php://input");
|
|
$in = json_decode($raw, true) ?: $_POST;
|
|
$q = trim($in["message"] ?? "");
|
|
$lang = trim($in["language"] ?? "fr");
|
|
|
|
if (!$q) { echo json_encode(["thinking"=>"Analyse de la demande en cours..."]); exit; }
|
|
|
|
// Detect query pattern and build tailored reasoning
|
|
$lc = mb_strtolower($q);
|
|
$thinking = "";
|
|
|
|
// File gen patterns
|
|
if (preg_match("/g[eéèê]n[eéèê]re?.*(pdf|pptx?|powerpoint|docx?|word|excel|tableau|document|pr[eéèê]sentation)/iu", $q, $m)) {
|
|
$type = strtolower($m[1]);
|
|
$thinking = "Je reçois une demande de génération de document $type. Étape 1 : extraction du sujet depuis la requête. Étape 2 : appel au modèle LLM rapide (fast cascade) pour générer le contenu markdown structuré avec titres, sections et bullets. Étape 3 : conversion du markdown via pandoc vers le format $type cible. Étape 4 : sauvegarde du fichier dans /generated/ avec timestamp unique et retour de l'URL téléchargeable. Temps estimé : 400-1500ms selon la complexité. Engine : pandoc " . ($type === "pdf" ? "+ wkhtmltopdf" : "") . ".";
|
|
}
|
|
elseif (preg_match("/g[eéèê]n[eéèê]re?.*(sch[eéèê]ma|mermaid|diagramme|flowchart)/iu", $q)) {
|
|
$thinking = "Demande de diagramme Mermaid détectée. Étape 1 : extraction du sujet du flowchart. Étape 2 : appel LLM avec system prompt strict exigeant syntaxe Mermaid valide (flowchart TD + nodes A[Label] --> B[Label]). Étape 3 : validation regex de la sortie, ajout du header si manquant. Étape 4 : encapsulation en code block mermaid pour rendu inline par le renderer mermaid 10.9.0 de l'interface.";
|
|
}
|
|
elseif (preg_match("/g[eéèê]n[eéèê]re?.*image/iu", $q)) {
|
|
$thinking = "Demande d'image SVG. Étape 1 : extraction du sujet de l'image. Étape 2 : appel LLM avec system prompt exigeant du SVG valide 400x300, formes géométriques colorées. Étape 3 : nettoyage (strip backticks markdown). Étape 4 : sauvegarde .svg dans /generated/ et retour URL + aperçu inline. Note : S204 ne dispose pas de Stable Diffusion local, donc génération via LLM textuel structuré.";
|
|
}
|
|
elseif (preg_match("/(?:ecris?|[eéèê]cri).*code/iu", $q)) {
|
|
$thinking = "Demande de génération de code. Étape 1 : détection du langage cible (python, javascript, react/jsx, typescript, php, html, bash, sql) via mots-clés dans le topic. Étape 2 : extraction du sujet métier. Étape 3 : appel LLM avec system prompt demandant code PUR sans preambule ni backticks. Étape 4 : sauvegarde dans /generated/ avec extension correcte. Étape 5 : retour inline avec code block pour rendu syntax-highlighted.";
|
|
}
|
|
elseif (preg_match("/traduis?|traduire?|translate/iu", $q)) {
|
|
$thinking = "Demande de traduction. Étape 1 : détection de la langue cible (anglais, espagnol, allemand, italien, portugais, arabe, chinois, japonais, français). Étape 2 : extraction du texte à traduire. Étape 3 : appel LLM avec prompt 'translate only, no explanation'. Étape 4 : retour du texte traduit aux côtés de l'original pour comparaison.";
|
|
}
|
|
elseif (preg_match("/\b(bilan|status|[eéèê]tat|rapport|audit|diagnostic)\b/iu", $q)) {
|
|
$thinking = "Demande de bilan système. Stratégie multi-agent : activation du V103 Natural Multi-Agent Router qui orchestre en parallèle jusqu'à 14 agents (sovereign, nonreg, ethica, git, vault, docker, crons, registry, pages, scraper, ollama, resolver, arena, blade). Chaque agent rapporte son état. Synthèse finale consolidée par le LLM avec structure exécutive : état général, performance, sécurité, développement, problèmes, actions.";
|
|
}
|
|
elseif (preg_match("/\b(qui|what|whoa?|quoi|comment|pourquoi|quand)\b/iu", $q)) {
|
|
$thinking = "Question informative détectée. Stratégie : consultation de la base de connaissances WEVIA (Qdrant vector store, 19 collections, 17327 points), recherche sémantique sur le sujet, puis appel LLM souverain pour formuler la réponse contextualisée en français professionnel. Si le sujet est hors KB, fallback sur la connaissance générale du modèle.";
|
|
}
|
|
else {
|
|
// Generic - try LLM for thinking
|
|
$sys_think = "You are the internal reasoning engine. Given the user query, output a SHORT reasoning chain (3-5 sentences max) explaining what you will do, in French. No preamble, no quotes, just the reasoning.";
|
|
$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_think],
|
|
["role"=>"user","content"=>"Requête : $q"],
|
|
],
|
|
"max_tokens" => 300,
|
|
"temperature" => 0.4,
|
|
]),
|
|
"timeout" => 12,
|
|
],
|
|
]));
|
|
$llm = @json_decode($raw, true);
|
|
$thinking = $llm["choices"][0]["message"]["content"] ?? "Analyse de la demande, identification du contexte WEVIA approprié, préparation de la réponse structurée.";
|
|
}
|
|
|
|
echo json_encode([
|
|
"thinking" => trim($thinking),
|
|
"source" => "ambre-thinking-v1",
|
|
"ts" => date("c"),
|
|
], JSON_UNESCAPED_UNICODE);
|