rajanvijayan/ai-engine
最新稳定版本:1.0.0
Composer 安装命令:
composer require rajanvijayan/ai-engine
包简介
Multi-provider AI library for PHP - supports Gemini, Meta Llama, and Groq
README 文档
README
A powerful, flexible PHP library for integrating multiple AI providers into your applications. Supports Google Gemini, Meta Llama, and Groq with conversation history.
🚀 Features
- 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
📋 Requirements
- PHP 8.0 or higher
jsonextension- Composer
🛠️ Installation
composer require rajanvijayan/ai-engine
🔑 API Keys Setup
Create 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"
}
Get Your API Keys
| Provider | URL | Notes |
|---|---|---|
| Gemini | makersuite.google.com | Google AI |
| Meta Llama | llama.meta.com | Official Meta API |
| Groq | console.groq.com | FREE tier available! |
🚀 Quick Start
Basic Usage
<?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;
Choose Provider
// Gemini $ai = new AIEngine($geminiKey, ['provider' => 'gemini']); // Meta Llama $ai = new AIEngine($metaKey, ['provider' => 'meta']); // Groq (Llama models) $ai = AIEngine::create('groq', $groqKey);
Conversation Mode
$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();
📚 API Reference
Constructor
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
]
Methods
| 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 |
Static Methods
| Method | Description |
|---|---|
AIEngine::create($provider, $apiKey) |
Factory method |
AIEngine::getModelsForProvider($provider) |
List available models |
AIEngine::getDefaultModels() |
Get default model per provider |
🤖 Available Models
Gemini
gemini-2.0-flash(default)gemini-2.0-flash-litegemini-2.5-flashgemini-2.5-pro
Meta Llama
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
Groq
llama-3.3-70b-versatile(default)llama-3.1-8b-instantmixtral-8x7b-32768gemma2-9b-it
💬 Usage Examples
Simple Question
$ai = new AIEngine($apiKey); $answer = $ai->generateContent('What is the capital of France?'); echo $answer;
Conversation with Memory
$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!
Switch Providers Mid-Session
$ai = new AIEngine($geminiKey); echo $ai->getProviderName(); // "Gemini" $ai->switchProvider('groq', $groqKey); echo $ai->getProviderName(); // "Groq"
Error Handling
$response = $ai->chat('Hello'); if (is_array($response) && isset($response['error'])) { echo "Error: " . $response['error']; } else { echo "AI: " . $response; }
🧪 Testing
Interactive Test
php test.php
This launches an interactive chat where you can:
- Select a provider
- Chat with the AI
- Use
/newto clear conversation - Use
/quitto exit
📁 Project Structure
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
🔧 Adding New Providers
- Create a new class implementing
ProviderInterface - Implement required methods:
generateContent(),sendMessage(),startNewConversation(),getConversationHistory(),setSystemInstruction(),isConfigured(),getName() - Add the provider to
AIEngine::createProvider()
🛡️ Error Handling
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 |
📄 License
MIT License - see LICENSE file.
🙏 Acknowledgments
- Google for Gemini API
- Meta for Llama API
- Groq for fast inference
Made with ❤️ for the PHP community
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-04