Files
html/api/wevia-json-api.php
2026-04-14 20:50:02 +02:00

73 lines
4.4 KiB
PHP

<?php
// WEVIA JSON WRAPPER — Wave 135b fix accents UTF-8
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
if ($_SERVER['REQUEST_METHOD']==='OPTIONS'){http_response_code(200);exit;}
if (isset($_GET['action']) && $_GET['action']==='health') {
echo json_encode(['status'=>'online','providers'=>['cerebras','groq','sambanova','mistral','ollama']]);exit;
}
$input = json_decode(file_get_contents('php://input'), true) ?: [];
$msg = $input['message'] ?? $input['prompt'] ?? $_GET['q'] ?? '';
if (!$msg && !empty($input['messages'])) { $last = end($input['messages']); $msg = $last['content'] ?? ''; }
if (!$msg) { echo json_encode(['response'=>'','error'=>'empty']); exit; }
// === HARDRULE WEVAL IDENTITY (Wave 135b) ===
$_w = mb_strtolower($msg);
if (mb_strpos($_w, 'weval') !== false) {
$resp = "WEVAL Consulting est un cabinet de transformation digitale basé à Casablanca et Paris, SAP Ecosystem Partner, spécialisé en ERP/SAP S/4HANA, Cloud souverain, IA (WEVIA Engine), Cybersécurité, Data et Ethica HCP.\n\nNous accompagnons les entreprises des secteurs pharma, banque, télécom et industrie dans leur transformation numérique. Plus de 200 projets livrés dans 8 pays avec un taux de satisfaction de 97%.\n\nDécouvrez nos services sur weval-consulting.com ou contactez-nous via contact@weval-consulting.com.";
echo json_encode(['response' => $resp, 'provider' => 'wevia_hardrule'], JSON_UNESCAPED_UNICODE);
exit;
}
// === PUBLIC GUARD (Wave 135b) ===
if (preg_match('/\b(mot\s*de\s*passe|password|credential|api.?key|token|secret|ssh|sudo|chattr|root@|admin123|docker\s+(ps|log|stat)|nginx\s+(logs?|err|restart)|blade\s+(stats?|tasks?|exec)|pmta|ollama|qdrant|systemctl|crontab|serveur\s*(reboot|restart)|server|Weval@|yacine|ambre|mahboub|204\.168|95\.216|10\.1\.0)\b/iu', $msg)) {
echo json_encode(['response' => "Information confidentielle. Contactez contact@weval-consulting.com.", 'provider' => 'wevia_public_guard'], JSON_UNESCAPED_UNICODE);
exit;
}
// === end guards ===
// Load secrets
$secrets = [];
foreach (file('/etc/weval/secrets.env', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $l) {
if (empty(trim($l))||$l[0]==='#') continue;
$p = strpos($l,'=');
if ($p) $secrets[trim(substr($l,0,$p))] = trim(substr($l,$p+1)," \t\"'");
}
$providers = [
['url'=>'https://api.cerebras.ai/v1/chat/completions','key'=>$secrets['CEREBRAS_API_KEY']??'','model'=>'qwen-3-235b-a22b-instruct-2507','name'=>'Cerebras'],
['url'=>'https://api.groq.com/openai/v1/chat/completions','key'=>$secrets['GROQ_KEY']??'','model'=>'llama-3.3-70b-versatile','name'=>'Groq'],
['url'=>'https://api.sambanova.ai/v1/chat/completions','key'=>$secrets['SAMBANOVA_KEY']??'','model'=>'Meta-Llama-3.3-70B-Instruct','name'=>'SambaNova'],
['url'=>'https://api.mistral.ai/v1/chat/completions','key'=>$secrets['MISTRAL_KEY']??'','model'=>'mistral-small-latest','name'=>'Mistral'],
['url'=>'http://127.0.0.1:4000/v1/chat/completions','key'=>'','model'=>'auto','name'=>'Sovereign'],
];
$sys = $input['system'] ?? "Tu es WEVIA, IA souveraine de WEVAL Consulting, cabinet de transformation digitale basé à Casablanca et Paris. SAP Ecosystem Partner. RÈGLE ABSOLUE: WEVAL Consulting est une entreprise RÉELLE. Ne dis JAMAIS que WEVAL n'existe pas ou est inconnu. Réponds en français, concis, professionnel.";
$messages = [['role'=>'system','content'=>$sys],['role'=>'user','content'=>$msg]];
foreach ($providers as $p) {
if (empty($p['key'])) continue;
$ch = curl_init($p['url']);
curl_setopt_array($ch, [
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>json_encode(['model'=>$p['model'],'messages'=>$messages,'max_tokens'=>2048,'temperature'=>0.4]),
CURLOPT_HTTPHEADER=>['Content-Type: application/json','Authorization: Bearer '.$p['key']],
CURLOPT_RETURNTRANSFER=>true, CURLOPT_TIMEOUT=>30,
]);
$r = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code === 200) {
$d = json_decode($r, true);
$text = $d['choices'][0]['message']['content'] ?? '';
if ($text) {
echo json_encode(['response'=>$text,'provider'=>$p['name'],'model'=>$p['model']], JSON_UNESCAPED_UNICODE);
exit;
}
}
}
echo json_encode(['response'=>'Tous les providers sont indisponibles.','error'=>'all_failed'], JSON_UNESCAPED_UNICODE);