275 lines
9.4 KiB
PHP
Executable File
275 lines
9.4 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* 🤖 HAMID ENGINE SIMPLE - Version simplifiée
|
|
* Version: 1.0 - Février 2026
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
class HamidEngineSimple {
|
|
private $providers = [
|
|
'deepseek' => [
|
|
'name' => 'DeepSeek',
|
|
'priority' => 1,
|
|
'timeout' => 15,
|
|
'enabled' => true
|
|
],
|
|
'ollama' => [
|
|
'name' => 'Ollama',
|
|
'priority' => 2,
|
|
'timeout' => 30,
|
|
'enabled' => true,
|
|
'local' => true
|
|
],
|
|
'fallback' => [
|
|
'name' => 'Fallback',
|
|
'priority' => 99,
|
|
'timeout' => 5,
|
|
'enabled' => true
|
|
]
|
|
];
|
|
|
|
/**
|
|
* Ask AI with cascade fallback
|
|
*/
|
|
public function askAI($prompt, $context = [], $preferredProvider = null) {
|
|
$startTime = microtime(true);
|
|
|
|
// Try preferred provider first if specified
|
|
if ($preferredProvider && isset($this->providers[$preferredProvider]) && $this->providers[$preferredProvider]['enabled']) {
|
|
$response = $this->tryProvider($preferredProvider, $prompt, $context);
|
|
if ($response['success']) {
|
|
return $this->formatResponse($response, $preferredProvider, $startTime);
|
|
}
|
|
}
|
|
|
|
// Try providers in priority order
|
|
uasort($this->providers, function($a, $b) {
|
|
return $a['priority'] <=> $b['priority'];
|
|
});
|
|
|
|
foreach ($this->providers as $providerId => $provider) {
|
|
if (!$provider['enabled']) continue;
|
|
|
|
$response = $this->tryProvider($providerId, $prompt, $context);
|
|
if ($response['success']) {
|
|
return $this->formatResponse($response, $providerId, $startTime);
|
|
}
|
|
}
|
|
|
|
// All providers failed
|
|
return [
|
|
'response' => "Désolé, tous les fournisseurs IA sont actuellement indisponibles.\n\n" .
|
|
"**Message reçu:** " . substr($prompt, 0, 200) . "\n\n" .
|
|
"**Conseil:** Essayez de reformuler votre question ou réessayez plus tard.",
|
|
'provider' => 'none',
|
|
'success' => false,
|
|
'latency_ms' => round((microtime(true) - $startTime) * 1000),
|
|
'fallback_mode' => true,
|
|
'timestamp' => date('Y-m-d H:i:s')
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Try a specific provider
|
|
*/
|
|
private function tryProvider($providerId, $prompt, $context) {
|
|
switch ($providerId) {
|
|
case 'deepseek':
|
|
return $this->tryDeepSeek($prompt, $context);
|
|
case 'ollama':
|
|
return $this->tryOllama($prompt, $context);
|
|
case 'fallback':
|
|
return $this->tryFallback($prompt, $context);
|
|
default:
|
|
return [
|
|
'success' => false,
|
|
'error' => 'Provider not implemented'
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Try DeepSeek (simulated for now)
|
|
*/
|
|
private function tryDeepSeek($prompt, $context) {
|
|
// Simulate API call
|
|
sleep(1);
|
|
|
|
// For now, return a simulated response
|
|
// In production, implement real API call
|
|
return [
|
|
'success' => true,
|
|
'response' => "🔍 **DeepSeek Analysis**\n\n" .
|
|
"J'ai analysé votre requête technique.\n\n" .
|
|
"**Question:** " . substr($prompt, 0, 150) . "\n\n" .
|
|
"**Recommandations:**\n" .
|
|
"1. Vérifiez les logs système pour des erreurs\n" .
|
|
"2. Surveillez les métriques de performance\n" .
|
|
"3. Testez les endpoints API\n\n" .
|
|
"**Note:** Version de démonstration - API réelle en développement",
|
|
'tokens_used' => rand(50, 150)
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Try Ollama (local)
|
|
*/
|
|
private function tryOllama($prompt, $context) {
|
|
// Try to connect to local Ollama
|
|
$url = 'http://127.0.0.1:11434/api/generate';
|
|
|
|
$data = [
|
|
'model' => 'llama3.1',
|
|
'prompt' => $prompt,
|
|
'stream' => false
|
|
];
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($data),
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
|
CURLOPT_TIMEOUT => 10
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode === 200 && $response) {
|
|
$result = json_decode($response, true);
|
|
return [
|
|
'success' => true,
|
|
'response' => $result['response'] ?? 'Réponse Ollama reçue',
|
|
'tokens_used' => $result['total_duration'] ? round($result['total_duration'] / 1000) : 0
|
|
];
|
|
}
|
|
|
|
// Ollama not available
|
|
return [
|
|
'success' => false,
|
|
'error' => 'Ollama not available'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Fallback response
|
|
*/
|
|
private function tryFallback($prompt, $context) {
|
|
// Simple rule-based responses
|
|
$promptLower = strtolower($prompt);
|
|
|
|
if (strpos($promptLower, 'hello') !== false || strpos($promptLower, 'bonjour') !== false) {
|
|
$response = "👋 Bonjour ! Je suis HAMID, l'IA de Weval Mind. Comment puis-je vous aider ?";
|
|
} elseif (strpos($promptLower, 'status') !== false || strpos($promptLower, 'health') !== false) {
|
|
$response = "🟢 **Statut système**\n\nTous les services sont opérationnels.\n\nPour une analyse détaillée, utilisez l'API Gateway.";
|
|
} elseif (strpos($promptLower, 'help') !== false || strpos($promptLower, 'aide') !== false) {
|
|
$response = "🆘 **Aide HAMID**\n\nJe peux vous aider avec:\n• L'analyse technique\n• Le diagnostic système\n• Les recommandations d'optimisation\n\nPosez-moi une question spécifique !";
|
|
} else {
|
|
$response = "🤖 **HAMID Engine**\n\nJ'ai reçu votre message: \"" . substr($prompt, 0, 100) . "\"\n\nPour des réponses plus avancées, la version complète avec 11 providers est en développement.";
|
|
}
|
|
|
|
return [
|
|
'success' => true,
|
|
'response' => $response,
|
|
'tokens_used' => strlen($response) / 4 // Rough estimate
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Format response
|
|
*/
|
|
private function formatResponse($response, $providerId, $startTime) {
|
|
return [
|
|
'response' => $response['response'],
|
|
'provider' => $this->providers[$providerId]['name'],
|
|
'provider_id' => $providerId,
|
|
'success' => true,
|
|
'latency_ms' => round((microtime(true) - $startTime) * 1000),
|
|
'tokens_used' => $response['tokens_used'] ?? 0,
|
|
'timestamp' => date('Y-m-d H:i:s'),
|
|
'engine_version' => 'simple-1.0'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get providers status
|
|
*/
|
|
public function getProvidersStatus() {
|
|
$status = [];
|
|
|
|
foreach ($this->providers as $id => $provider) {
|
|
$status[] = [
|
|
'id' => $id,
|
|
'name' => $provider['name'],
|
|
'priority' => $provider['priority'],
|
|
'enabled' => $provider['enabled'],
|
|
'local' => $provider['local'] ?? false,
|
|
'timeout' => $provider['timeout']
|
|
];
|
|
}
|
|
|
|
return $status;
|
|
}
|
|
}
|
|
|
|
// API Endpoint
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$input = file_get_contents('php://input');
|
|
$data = json_decode($input, true);
|
|
|
|
$engine = new HamidEngineSimple();
|
|
|
|
if (isset($data['action'])) {
|
|
switch ($data['action']) {
|
|
case 'chat':
|
|
if (empty($data['message'])) {
|
|
echo json_encode(['error' => 'Message is required']);
|
|
exit;
|
|
}
|
|
|
|
$result = $engine->askAI(
|
|
$data['message'],
|
|
$data['context'] ?? [],
|
|
$data['provider'] ?? null
|
|
);
|
|
break;
|
|
|
|
case 'providers':
|
|
$result = [
|
|
'providers' => $engine->getProvidersStatus(),
|
|
'timestamp' => date('Y-m-d H:i:s')
|
|
];
|
|
break;
|
|
|
|
default:
|
|
$result = ['error' => 'Invalid action', 'valid_actions' => ['chat', 'providers']];
|
|
}
|
|
} else {
|
|
$result = ['error' => 'No action specified'];
|
|
}
|
|
|
|
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
|
} else {
|
|
// GET request - documentation
|
|
echo json_encode([
|
|
'service' => 'HAMID Engine Simple',
|
|
'version' => '1.0',
|
|
'status' => 'operational',
|
|
'endpoints' => [
|
|
'POST /api/hamid-engine-simple.php' => 'AI chat and queries',
|
|
'Actions' => ['chat', 'providers']
|
|
],
|
|
'example_request' => [
|
|
'action' => 'chat',
|
|
'message' => 'Analyse l architecture du système',
|
|
'context' => ['technical' => true]
|
|
]
|
|
], JSON_PRETTY_PRINT);
|
|
}
|
|
?>
|