Files
html/api/opus5-weval-ia-fast-safe.php

97 lines
4.2 KiB
PHP

<?php
// OPUS5 — Wrapper safe pour weval-ia-fast.php
// But : répondre en <2s aux queries métier (prospect/recense/stats), fallback JSON propre si timeout
// Usage (avec authorization Yanis) : remplacer /api/weval-ia-fast.php par /api/opus5-weval-ia-fast-safe.php dans ethica-chatbot.html
header('Content-Type: application/json');
$start = microtime(true);
$raw = file_get_contents('php://input');
$d = json_decode($raw, true) ?: [];
$msg = strtolower((string)($d['message'] ?? ''));
// === FAST DISPATCH (pre-LLM) ===
// Pattern 1: "prospect/recense/agent/aujourd'hui"
if (preg_match('/\b(prospect|recense|agent).{0,50}(aujourd|today|hui|nouveau)\b/iu', $msg)
|| preg_match('/\b(aujourd|today|hui)\b.{0,80}\b(prospect|recense|agent)\b/iu', $msg)
|| (strpos($msg, 'prospect') !== false && strpos($msg, 'agent') !== false)) {
$pt = @file_get_contents('https://127.0.0.1/api/opus5-prospects-today.php', false, stream_context_create([
'http' => ['timeout' => 5],
'ssl' => ['verify_peer'=>false, 'verify_peer_name'=>false]
]));
$data = @json_decode($pt, true);
if ($data && isset($data['response'])) {
$ms = round((microtime(true)-$start)*1000);
echo json_encode([
'response' => $data['response'],
'provider' => 'opus5-prospects-today-fast',
'mode' => 'direct-db',
'ms' => $ms,
'sources' => [['url'=>'/api/opus5-prospects-today.php','label'=>'PG direct DB']]
], JSON_UNESCAPED_UNICODE);
exit;
}
}
// Pattern 2: ethica stats (combien HCP, ethica total, etc.)
if (preg_match('/\bethica\b/iu', $msg) && preg_match('/\b(combien|total|stats|live|hcps?|chiffres)\b/iu', $msg)) {
$e = @file_get_contents('https://127.0.0.1/api/ethica-stats-api.php', false, stream_context_create([
'http' => ['timeout' => 5],
'ssl' => ['verify_peer'=>false, 'verify_peer_name'=>false]
]));
$data = @json_decode($e, true);
if ($data && isset($data['total'])) {
$resp = sprintf(
"Ethica base live (source PG ethica.medecins_real S95) :\n\n- Total HCPs : %s\n- Avec email : %s\n- Avec téléphone : %s\n\nPar pays : Algérie %s, Maroc %s, Tunisie %s.",
number_format($data['total'], 0, ',', ' '),
number_format($data['with_email'] ?? 0, 0, ',', ' '),
number_format($data['with_telephone'] ?? 0, 0, ',', ' '),
number_format($data['by_country']['DZ'] ?? 0, 0, ',', ' '),
number_format($data['by_country']['MA'] ?? 0, 0, ',', ' '),
number_format($data['by_country']['TN'] ?? 0, 0, ',', ' ')
);
echo json_encode([
'response' => $resp,
'provider' => 'opus5-ethica-stats-fast',
'mode' => 'direct-db',
'ms' => round((microtime(true)-$start)*1000),
'sources' => []
], JSON_UNESCAPED_UNICODE);
exit;
}
}
// === FALLBACK: proxy vers weval-ia-fast.php original avec timeout 25s ===
$ch = curl_init('https://127.0.0.1/api/weval-ia-fast.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $raw,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Host: weval-consulting.com'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_TIMEOUT => 25,
CURLOPT_CONNECTTIMEOUT => 3
]);
$resp = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err = curl_error($ch);
curl_close($ch);
// Si HTML (erreur) ou vide/timeout → fallback JSON propre
if (!$resp || strpos($resp, '<!DOCTYPE') === 0 || strpos($resp, '<html') === 0 || $http >= 500) {
$ms = round((microtime(true)-$start)*1000);
echo json_encode([
'response' => "Je n'ai pas pu traiter cette requête dans les temps. Essayez de poser une question plus courte ou utilisez les raccourcis ci-dessus (HCPs par pays, Spécialités, Campagnes, Taux ouverture, Senders, Warmup, RGPD Santé, Délivrabilité, Concurrence, Pricing).",
'provider' => 'opus5-safe-fallback',
'mode' => 'graceful',
'upstream_http' => $http,
'ms' => $ms,
'sources' => []
], JSON_UNESCAPED_UNICODE);
exit;
}
// Réponse upstream valide → renvoyer telle quelle
echo $resp;