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

50 lines
1.8 KiB
PHP

<?php
// WEVIA Chat v3 — Sovereign Direct (fast, no Redis dependency)
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; }
$input = json_decode(file_get_contents('php://input'), true);
$msg = trim($input['message'] ?? $input['msg'] ?? '');
if (!$msg) { echo json_encode(['response' => 'Message vide', 'provider' => 'error']); exit; }
// Call sovereign-api:4000 directly (12 providers, 0 EUR)
$ch = curl_init('http://127.0.0.1:4000/v1/chat/completions');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 14,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'model' => 'auto',
'messages' => [
['role' => 'system', 'content' => 'Tu es WEVIA, IA souveraine de WEVAL Consulting, cabinet transformation digitale a Casablanca. Reponds en francais, professionnel et concis.'],
['role' => 'user', 'content' => $msg]
],
'max_tokens' => 800,
'temperature' => 0.3,
])
]);
$resp = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$time_ms = round(curl_getinfo($ch, CURLINFO_TOTAL_TIME) * 1000);
curl_close($ch);
if ($code === 200) {
$d = json_decode($resp, true);
$text = $d['choices'][0]['message']['content'] ?? '';
$prov = $d['provider'] ?? 'sovereign';
if ($text) {
echo json_encode([
'response' => $text,
'provider' => $prov,
'thinking' => ['Analyse en cours...'],
'duration_ms' => $time_ms
]);
exit;
}
}
echo json_encode(['response' => 'Tous les providers sont occupes. Reessayez.', 'provider' => 'busy', 'duration_ms' => $time_ms]);