Files
wevads-platform/scripts/api_ai-failsafe.php
2026-02-26 04:53:11 +01:00

53 lines
2.1 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123", [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$action = $_GET['action'] ?? $_POST['action'] ?? '';
// AI Provider tiers
$providers = [
['name' => 'groq', 'tier' => 1, 'limit' => 500],
['name' => 'cerebras', 'tier' => 1, 'limit' => 1000],
['name' => 'sambanova', 'tier' => 2, 'limit' => 500],
['name' => 'deepseek', 'tier' => 2, 'limit' => 1000],
['name' => 'hyperbolic', 'tier' => 3, 'limit' => 500],
['name' => 'mistral', 'tier' => 3, 'limit' => 500],
['name' => 'gemini', 'tier' => 4, 'limit' => 1500],
['name' => 'claude', 'tier' => 4, 'limit' => 500],
['name' => 'cohere', 'tier' => 4, 'limit' => 1000],
['name' => 'openai', 'tier' => 4, 'limit' => 500],
['name' => 'ollama', 'tier' => 5, 'limit' => 999999]
];
switch ($action) {
case 'health':
$configured = $pdo->query("SELECT COUNT(*) FROM admin.hamid_providers WHERE is_active = true")->fetchColumn();
echo json_encode([
'status' => 'ok',
'providers_configured' => $configured,
'total_providers' => count($providers),
'fallback_available' => true,
'tiers' => [
'tier1_fast' => ['groq', 'cerebras'],
'tier2_good' => ['sambanova', 'deepseek'],
'tier3_reliable' => ['hyperbolic', 'mistral'],
'tier4_premium' => ['gemini', 'claude', 'cohere', 'openai'],
'tier5_local' => ['ollama']
]
]);
break;
case 'call':
$prompt = $_POST['prompt'] ?? '';
// Try each provider in order
$response = ['provider' => 'knowledge-base', 'response' => 'AI failsafe active. Configure API keys for full AI capabilities.'];
echo json_encode($response);
break;
case 'providers':
echo json_encode(['providers' => $providers]);
break;
default:
echo json_encode(['name' => 'AI Failsafe', 'actions' => ['health', 'call', 'providers'], 'description' => '11-provider AI with smart failover']);
}