muradbdinfo/laravelai
Unified AI chat interface for Laravel with pluggable providers (Ollama, OpenAI/ChatGPT, Anthropic/Claude, DeepSeek). Includes quick setup, configuration, real-world examples, chat app integration, API reference, and built-in RAG support.
Installation:
composer require muradbdinfo/laravelai
php artisan vendor:publish --provider="Muradbdinfo\LaravelAI\LaravelAIServiceProvider" --tag="config"
config/laravelai.php.Environment Configuration:
Add your API keys to .env (e.g., OPENAI_API_KEY, OLLAMA_BASE_URL).
Configure the default provider in config/laravelai.php:
'default' => env('AI_PROVIDER', 'openai'),
First Use Case: Use the facade to send a prompt:
use Muradbdinfo\LaravelAI\Facades\LaravelAI;
$response = LaravelAI::send('What is Laravel?');
dd($response);
choices, usage, and metadata.config/laravelai.php (providers, defaults, rate limits).app/Facades/LaravelAI.php (primary interface).app/Providers/LaravelAIServiceProvider.php (service binding).Dynamically switch AI providers at runtime:
// Use Ollama locally
$response = LaravelAI::use('ollama')->send('Explain Laravel Eloquent');
// Fallback to OpenAI if Ollama fails
LaravelAI::fallbackTo('openai');
Handle streaming responses for real-time UIs:
LaravelAI::stream('Generate a poem about Laravel', function ($chunk) {
echo $chunk->content;
});
Override default models per provider:
LaravelAI::use('openai')->withModel('gpt-4')->send('Analyze this code...');
Respect API limits with built-in retries:
LaravelAI::withRetry(3)->send('Complex query...');
Offload AI calls to queues to avoid timeouts:
LaravelAI::dispatch('Generate report')->onQueue('ai');
Use Laravel Echo for real-time updates:
// Frontend (Vue)
Echo.channel('ai-responses')
.listen('AiResponseEvent', (response) => {
console.log(response);
});
Mock responses in tests:
LaravelAI::fake()->shouldReturn([
'choices' => [['message' => ['content' => 'Mocked response']]],
]);
API Key Leaks:
.env or hardcode keys. Use Laravel’s env() or .env files.config/laravelai.php:
'validate_keys' => true,
Rate Limits:
withRetry() or implement exponential backoff.config/laravelai.php:
'log_usage' => true,
Streaming Quirks:
chunk() for partial responses:
$response = LaravelAI::stream('...')->chunk();
Model Availability:
config/laravelai.php:
'valid_models' => [
'openai' => ['gpt-3.5-turbo', 'gpt-4'],
'ollama' => ['llama3'],
],
Enable Logging:
LaravelAI::debug(true); // Logs all requests/responses to storage/logs/laravelai.log
Validate Responses:
LaravelAI::lastResponse() to inspect raw API responses:
$last = LaravelAI::lastResponse();
dd($last->raw);
Provider-Specific Errors:
config/laravelai.php for provider-specific error handling:
'exceptions' => [
'openai' => \Muradbdinfo\LaravelAI\Exceptions\OpenAIException::class,
],
Custom Providers:
Extend the base Provider class to add support for new AI services:
namespace App\Providers;
use Muradbdinfo\LaravelAI\Contracts\Provider;
class CustomProvider implements Provider {
public function send($prompt, array $options = []) {
// Custom logic
}
}
Register in config/laravelai.php:
'providers' => [
'custom' => \App\Providers\CustomProvider::class,
],
Middleware: Add middleware to modify prompts/responses:
LaravelAI::middleware(function ($prompt, $next) {
return $next($prompt . " Add this context: ...");
});
Events:
Listen for AI events (e.g., AiRequestSent, AiResponseReceived):
LaravelAI::listen(function ($event) {
Log::info('AI Event:', $event->data);
});
How can I help you explore Laravel packages today?