Files
wevia-brain/weval-think-api.php
2026-04-12 23:01:36 +02:00

143 lines
7.0 KiB
PHP
Executable File

<?php
// weval-think-api.php — Real-time LLM reasoning endpoint
// Called by wevia.html to generate thinking blocks like Claude Opus
// Uses fastest provider (Groq) with a specialized reasoning prompt
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit; }
$in = json_decode(file_get_contents('php://input'), true);
$msg = trim($in['message'] ?? '');
$lang = $in['language'] ?? 'fr';
if (!$msg || mb_strlen($msg) < 3) {
echo json_encode(['thinking' => 'Réponse directe à cette salutation.']);
exit;
}
// Reasoning prompt — generates Claude-style thinking
$thinkPrompt = ($lang === 'fr' || $lang === 'ar') ?
"Tu es le moteur de raisonnement interne de WEVIA. Génère une PENSÉE INTERNE (pas une réponse) sur cette question de l'utilisateur.
RÈGLES:
- Écris EXACTEMENT comme Claude Opus pense en interne: analyse structurée, décomposition du problème, identification des angles d'approche
- Identifie les MOTS-CLÉS spécifiques de la question et analyse-les
- Mentionne les domaines de connaissance à mobiliser
- Évalue la complexité et le type de réponse nécessaire
- Note les pièges potentiels ou les nuances importantes
- Propose ta stratégie de réponse
- 3-5 phrases max, style analytique et intelligent
- JAMAIS de réponse à la question, UNIQUEMENT le raisonnement
- Chaque pensée doit être UNIQUE et SPÉCIFIQUE à cette question exacte
- Utilise des termes techniques pertinents au domaine de la question
Exemple pour 'Comment améliorer la délivrabilité email?':
'Sujet technique email marketing — je dois couvrir les aspects SPF/DKIM/DMARC, la réputation IP, le warmup progressif et le contenu. Le contexte Maroc/Afrique implique des considérations spécifiques sur les FAI locaux. Je structure par priorité d'impact.'
Question de l'utilisateur: " :
"You are WEVIA's internal reasoning engine. Generate an INTERNAL THOUGHT (not a response) about this user question.
RULES:
- Write exactly like Claude Opus thinks internally: structured analysis, problem decomposition, approach identification
- Identify SPECIFIC keywords and analyze them
- Mention knowledge domains to mobilize
- Assess complexity and needed response type
- Note potential pitfalls or important nuances
- 3-5 sentences max, analytical and intelligent style
- NEVER answer the question, ONLY the reasoning
- Each thought must be UNIQUE and SPECIFIC to this exact question
User question: ";
$thinkPrompt .= $msg;
// Use Groq (fastest) for thinking — doesn't need to be deep, just contextual
$providers = [
// Cerebras FIRST — preserve Groq quota for Full API
['url' => 'https://api.cerebras.ai/v1/chat/completions', 'key' => 'csk-4wrrhkpr568ry9xx49k9mcynwdx483nx53dd62yh5xedfckh', 'model' => 'llama3.1-8b'],
['url' => 'https://api.sambanova.ai/v1/chat/completions', 'key' => '9541b2a0-6ddc-4e7d-a957-c348d6119c3f', 'model' => 'Meta-Llama-3.3-70B-Instruct'],
['url' => 'https://api.groq.com/openai/v1/chat/completions', 'key' => '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', 'model' => 'llama-3.3-70b-versatile'],
];
$thinking = '';
foreach ($providers as $p) {
$ch = curl_init($p['url']);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Authorization: Bearer ' . $p['key']],
CURLOPT_POSTFIELDS => json_encode([
'model' => $p['model'],
'messages' => [
['role' => 'system', 'content' => 'Tu génères des pensées internes analytiques. 3-5 phrases max. Style: raisonnement structuré.'],
['role' => 'user', 'content' => $thinkPrompt]
],
'max_tokens' => 200,
'temperature' => 0.7
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5, // 5s max — thinking must be FAST
CURLOPT_CONNECTTIMEOUT => 3,
]);
$raw = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code === 200 && $raw) {
$data = json_decode($raw, true);
$thinking = trim($data['choices'][0]['message']['content'] ?? '');
if ($thinking) break;
}
}
// Fallback if all providers fail
if (!$thinking) {
$keywords = array_filter(explode(' ', $msg), function($w) { return mb_strlen($w) > 3; });
$kw = implode(', ', array_slice($keywords, 0, 4));
$thinking = "Analyse des termes clés : {$kw}. Structuration d'une réponse précise et contextualisée.";
}
// Clean: remove any actual answer, keep only reasoning
$thinking = preg_replace('/^(Voici|Bien sûr|Certainement|D\'accord|Pour répondre)[^.]*\./i', '', $thinking);
$thinking = trim($thinking);
// ═══ RESPONSE GENERATION (FIX: was only returning thinking) ═══
$response = "";
$responsePrompt = "Voici ton raisonnement interne:\n{$thinking}\n\nGénère maintenant une réponse complète et professionnelle.\n\nQuestion: {$msg}";
foreach ($providers as $p) {
$ch = curl_init($p["url"]);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json", "Authorization: Bearer " . $p["key"]],
CURLOPT_POSTFIELDS => json_encode([
"model" => $p["model"],
"messages" => [
["role" => "system", "content" => "Tu es WEVIA, IA cognitive souveraine de WEVAL Consulting (Casablanca). REGLE ABSOLUE: quand on demande les services ou expertise de WEVAL, cite UNIQUEMENT les vrais services listes ci-dessous, JAMAIS du conseil generique. Sois CONCIS: 2-4 paragraphes max pour les questions simples, 3-5 pour les complexes. Reponds avec precision et exemples concrets.
EXPERTISE WEVAL: SAP S/4HANA (RISE, BTP, Fiori) | Vistex (Pricing, exclusif Afrique) | Odoo | Cloud souverain (Huawei, Scaleway, OVH, AWS/Azure/GCP) | Cybersecurite (SOC, SIEM, Zero Trust, ISO 27001, RGPD) | IA et Data (LLMs, RAG, fine-tuning, Power BI) | Marketing Digital | Six Sigma | Pharma/GMP | Banque/Fintech | Retail/Supply Chain | Telecom/5G. Partenariats: SAP, Vistex, Huawei Cloud, Scaleway, IQVIA.
REGLES: Concis (3-5 paragraphes max). Recommandation actionnable. JAMAIS mentionner WEVADS/PowerMTA/ADX/Arsenal/Sentinel."],
["role" => "user", "content" => $responsePrompt]
],
"max_tokens" => 1500,
"temperature" => 0.6
]),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 5,
]);
$raw = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 && $raw) {
$data2 = json_decode($raw, true);
$response = trim($data2["choices"][0]["message"]["content"] ?? "");
$response = preg_replace("/<think>.*?<\/think>/s", "", $response);
$response = trim($response);
if ($response) break;
}
}
echo json_encode(["thinking" => $thinking, "response" => $response, "mode" => "think", "engine" => "WEVIA Think"]);