62 lines
3.5 KiB
PHP
62 lines
3.5 KiB
PHP
<?php
|
|
// WEVIA LLM Helper v4 — 12 FREE providers cascade (tested 11-avr)
|
|
function wv_llm($prompt, $system = "Tu es WEVIA, IA souveraine de WEVAL Consulting.") {
|
|
// OPUS 20avr doctrine #4: anti-hallucination guard prepended to all system prompts
|
|
$__guard = "REGLES STRICTES: Tu es un LLM sans acces shell ni filesystem. Si user demande exec commande/diagnostic/lecture fichier/status systeme: reponds 'Cette requete necessite un intent shell reel cote WEVIA. Tape diagnostique toi ou demande a Opus de wire un intent dedie.' N INVENTE JAMAIS outputs commandes PIDs paths MD5 timestamps docker containers journaux systeme. Si info factuelle inconnue dis-le. ";
|
|
$system = $__guard . $system;
|
|
$env = @file_get_contents("/etc/weval/secrets.env") ?: "";
|
|
$providers = [];
|
|
// Tier 0: Sovereign API local (fastest, routes to best cloud provider)
|
|
$providers[] = ["url" => "http://127.0.0.1:4000/v1/chat/completions", "key" => "none", "model" => "auto", "name" => "Sovereign"];
|
|
|
|
|
|
|
|
// Tier 1: Groq (fastest, 100K tok/day)
|
|
if (preg_match("/GROQ_KEY=(.+)/", $env, $m))
|
|
$providers[] = ["url" => "https://api.groq.com/openai/v1/chat/completions", "key" => trim($m[1]), "model" => "llama-3.3-70b-versatile", "name" => "Groq"];
|
|
|
|
// Tier 2: Cerebras (ultra-fast, free)
|
|
if (preg_match("/CEREBRAS_API_KEY=(.+)/", $env, $m))
|
|
$providers[] = ["url" => "https://api.cerebras.ai/v1/chat/completions", "key" => trim($m[1]), "model" => "llama-3.3-70b", "name" => "Cerebras"];
|
|
|
|
// Tier 3: Gemini 2.5 Flash (reliable, free)
|
|
if (preg_match("/GEMINI_KEY=(.+)/", $env, $m))
|
|
$providers[] = ["url" => "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions", "key" => trim($m[1]), "model" => "gemini-2.0-flash", "name" => "Gemini"];
|
|
|
|
// Tier 4: Mistral (free tier)
|
|
if (preg_match("/MISTRAL_KEY=(.+)/", $env, $m))
|
|
$providers[] = ["url" => "https://api.mistral.ai/v1/chat/completions", "key" => trim($m[1]), "model" => "open-mistral-nemo", "name" => "Mistral"];
|
|
|
|
// Tier 5: OpenRouter (free models)
|
|
if (preg_match("/OPENROUTER_KEY=(.+)/", $env, $m))
|
|
$providers[] = ["url" => "https://openrouter.ai/api/v1/chat/completions", "key" => trim($m[1]), "model" => "meta-llama/llama-3.3-70b-instruct:free", "name" => "OpenRouter"];
|
|
|
|
// Tier 6: SambaNova (if renewed)
|
|
if (preg_match("/SAMBANOVA_KEY=(.+)/", $env, $m))
|
|
$providers[] = ["url" => "https://api.sambanova.ai/v1/chat/completions", "key" => trim($m[1]), "model" => "DeepSeek-V3.2", "name" => "SambaNova"];
|
|
|
|
foreach ($providers as $p) {
|
|
$data = json_encode(["model" => $p["model"], "messages" => [
|
|
["role" => "system", "content" => $system],
|
|
["role" => "user", "content" => $prompt]
|
|
], "max_tokens" => 1500], JSON_UNESCAPED_UNICODE);
|
|
$ch = curl_init($p["url"]);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $data,
|
|
CURLOPT_HTTPHEADER => ["Content-Type: application/json", "Authorization: Bearer " . $p["key"]],
|
|
CURLOPT_TIMEOUT => 20,
|
|
CURLOPT_SSL_VERIFYPEER => false
|
|
]);
|
|
$r = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($code === 200) {
|
|
$d = @json_decode($r, true);
|
|
$content = $d["choices"][0]["message"]["content"] ?? null;
|
|
if ($content) return $content;
|
|
}
|
|
}
|
|
return "LLM: all providers busy";
|
|
}
|