Universal AI Engine
Universal AI & LLM Engine (`Core\AI`)
Zero-dependency, multi-provider AI client built into _core/AI.php supporting
DeepSeek (with automated model fallback), Google Gemini (3.8
flash, 3.7 Flash, 3.6 Flash, 3.5 Flash, 3.1 Pro), OpenAI (GPT-4o, o3-mini),
Anthropic Claude (3.7 Sonnet, 3.5 Sonnet), and local Ollama.
1. Quick Text & Structured JSON Generation
<?php
use Core\AI;
// 1. Simple text generation using default provider configured in .env
$answer = AI::text("Explain database indexing in simple terms.");
// 2. Global helper shortcut
$summary = ai("Summarize the following contract: " . $contractText);
$profile = AI::json("Generate a mock user profile with name, email, and occupation.");
$code = AI::text("Write a regex to match E.164 phone numbers", [
'system' => 'You are a senior PHP and regex specialist. Return only code.',
'temperature' => 0.2
]);
2. Supported AI Providers & Modern Models
Google Gemini
Direct integration with Google Generative Language API. Supports
gemini-3.7-flash, gemini-3.6-flash, gemini-3.5-flash,
gemini-3.1-pro, and gemini-2.0-flash.
$res = AI::gemini("Analyze quarterly sales trends", [
'model' => 'gemini-3.7-flash',
'fallback_model' => 'gemini-3.5-flash',
'json_mode' => true
]);
DeepSeek AI
Direct integration with api.deepseek.com. Supports
deepseek-v4-flash, deepseek-chat, and
deepseek-reasoner with reasoning token metrics.
$res = AI::deepseek($prompt, [
'model' => 'deepseek-v4-flash',
'fallback_model' => 'deepseek-chat',
'system' => 'You are a financial advisor.'
]);
Anthropic Claude
Supports claude-3-7-sonnet-20250219, claude-3-5-sonnet-20241022,
and claude-3-5-haiku-20241022 with direct system prompt mapping.
$res = AI::anthropic($prompt, [
'model' => 'claude-3-7-sonnet-20250219',
'max_tokens' => 4096
]);
OpenAI
Supports gpt-4o, gpt-4o-mini, o1, and
o3-mini with standard response format schemas.
$res = AI::openai($prompt, [
'model' => 'gpt-4o-mini',
'temperature' => 0.4
]);
Local Ollama & Private Models
Zero external API key required. Connects directly to local or self-hosted Ollama instances running DeepSeek-R1, Llama 3.3, Qwen 2.5, or Mistral.
$res = AI::ollama($prompt, [
'model' => 'deepseek-r1:8b',
'base_url' => 'http://localhost:11434'
]);
3. Built-in AI Rate Limiter & Quota Guard
Protect your public AI chat endpoints against quota exhaustion and DDoS abuse with dual per-minute and per-day rate limits powered by Redis:
<?php
use Core\AI;
use Core\Request;
use Core\Response;
Request::POST(function() {
$ip = Request::ip();
$limit = AI::checkRateLimit($ip, limitPerMinute: 6, limitPerDay: 50);
if (!$limit['allowed']) {
Response::json(['error' => $limit['error']], 429);
exit;
}
$message = input('message', '');
$reply = AI::text($message, [
'provider' => 'gemini',
'model' => 'gemini-3.7-flash',
'system' => 'You are a helpful assistant.'
]);
Response::json([
'reply' => $reply,
'remaining_daily' => $limit['remaining_day'] ?? 0
]);
});