A powerful, flexible PHP library for integrating multiple AI providers into your applications. Supports Google Gemini, Meta Llama, and Groq with conversation history.
- Multi-Provider Support: Gemini, Meta Llama, and Groq (Llama)
- Conversation Mode: Maintains chat history for context-aware responses
- Easy Provider Switching: Switch between providers on the fly
- System Instructions: Set AI personality/behavior per conversation
- Simple API: Intuitive interface that works out of the box
- Extensible: Easy to add new providers
- PHP 8.0 or higher
jsonextension- Composer
composer require rajanvijayan/ai-engineCreate a test.ai-key file in your project root:
{
"gemini": "YOUR_GEMINI_API_KEY",
"meta": "YOUR_META_LLAMA_API_KEY",
"groq": "YOUR_GROQ_API_KEY"
}| Provider | URL | Notes |
|---|---|---|
| Gemini | makersuite.google.com | Google AI |
| Meta Llama | llama.meta.com | Official Meta API |
| Groq | console.groq.com | FREE tier available! |
<?php
require_once 'vendor/autoload.php';
use AIEngine\AIEngine;
// Create with Gemini (default)
$ai = new AIEngine('your-gemini-api-key');
$response = $ai->generateContent('Hello! How are you?');
echo $response;// Gemini
$ai = new AIEngine($geminiKey, ['provider' => 'gemini']);
// Meta Llama
$ai = new AIEngine($metaKey, ['provider' => 'meta']);
// Groq (Llama models)
$ai = AIEngine::create('groq', $groqKey);$ai = AIEngine::create('groq', $groqKey);
$ai->setSystemInstruction("You are a helpful assistant.");
// Start a conversation
$ai->chat("My name is John");
$ai->chat("What's my name?"); // AI remembers: "John"
// Clear conversation
$ai->newConversation();new AIEngine($apiKey, $config = [])Config Options:
[
'provider' => 'gemini', // 'gemini', 'meta', or 'groq'
'model' => null, // Model name (uses provider default if null)
'timeout' => 60, // Request timeout in seconds
'enable_logging' => false // Enable logging
]| Method | Description |
|---|---|
generateContent($prompt) |
Single prompt, no history |
chat($message) |
Send message with conversation history |
newConversation() |
Clear conversation history |
getHistory() |
Get conversation history array |
setSystemInstruction($text) |
Set AI personality/behavior |
switchProvider($name, $apiKey) |
Switch to different provider |
getProviderName() |
Get current provider name |
| Method | Description |
|---|---|
AIEngine::create($provider, $apiKey) |
Factory method |
AIEngine::getModelsForProvider($provider) |
List available models |
AIEngine::getDefaultModels() |
Get default model per provider |
gemini-2.0-flash(default)gemini-2.0-flash-litegemini-2.5-flashgemini-2.5-pro
Llama-4-Maverick-17B-128E-Instruct-FP8(default)Llama-4-Scout-17B-16E-InstructLlama-3.3-70B-InstructLlama-3.2-3B-InstructLlama-3.2-1B-Instruct
llama-3.3-70b-versatile(default)llama-3.1-8b-instantmixtral-8x7b-32768gemma2-9b-it
$ai = new AIEngine($apiKey);
$answer = $ai->generateContent('What is the capital of France?');
echo $answer;$ai = AIEngine::create('groq', $groqKey);
$ai->setSystemInstruction("You are a math tutor. Be concise.");
echo $ai->chat("What is 15 + 27?"); // "42"
echo $ai->chat("Multiply that by 2"); // "84" - remembers previous answer!$ai = new AIEngine($geminiKey);
echo $ai->getProviderName(); // "Gemini"
$ai->switchProvider('groq', $groqKey);
echo $ai->getProviderName(); // "Groq"$response = $ai->chat('Hello');
if (is_array($response) && isset($response['error'])) {
echo "Error: " . $response['error'];
} else {
echo "AI: " . $response;
}php test.phpThis launches an interactive chat where you can:
- Select a provider
- Chat with the AI
- Use
/newto clear conversation - Use
/quitto exit
ai-engine/
βββ src/
β βββ AIEngine.php # Main engine class
β βββ Providers/
β βββ ProviderInterface.php # Provider contract
β βββ Gemini.php # Google Gemini
β βββ MetaLlama.php # Meta Llama API
β βββ Groq.php # Groq API
βββ test.php # Interactive test
βββ test.ai-key # API keys (JSON)
βββ composer.json
βββ README.md
- Create a new class implementing
ProviderInterface - Implement required methods:
generateContent(),sendMessage(),startNewConversation(),getConversationHistory(),setSystemInstruction(),isConfigured(),getName() - Add the provider to
AIEngine::createProvider()
Common errors returned:
| Error | Cause |
|---|---|
Provider not properly configured |
Invalid or missing API key |
Invalid prompt |
Empty or too long prompt |
Error contacting API |
Network or API issue |
MIT License - see LICENSE file.
- Google for Gemini API
- Meta for Llama API
- Groq for fast inference
Made with β€οΈ for the PHP community