omer-hanaraey / laravel-ai-gateway
A unified interface for multiple AI providers in Laravel
v0.3.0
2025-04-15 11:10 UTC
Requires
- php: ^8.1 || ^8.2 || ^8.3 || ^8.4
- ext-json: *
- google/apiclient: ^2.18
- illuminate/support: ^9.0 || ^10.0 || ^11.0 || ^12.0
- nyholm/psr7: ^1.8
- openai-php/client: ^0.6
- symfony/http-client: ^6.4 || ^7.2
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
A unified interface for multiple AI providers in Laravel applications.
Features
- Support for multiple AI providers (OpenAI, Gemini, Claude, DeepSeek, Mistral, and Cohere)
- Unified API interface
- Easy driver switching
- Standardized response format
- Facade access
Installation
- Install via Composer:
composer require omer-hanaraey/laravel-ai-gateway
- Publish the configuration file:
php artisan vendor:publish --tag=ai-bridge-config
Configuration
Add your API keys to your .env file:
AI_DEFAULT_DRIVER=openai OPENAI_API_KEY=your_openai_key OPENAI_MODEL=gpt-3.5-turbo GEMINI_API_KEY=your_gemini_key GEMINI_MODEL=gemini-pro CLAUDE_API_KEY=your_claude_key CLAUDE_MODEL=claude-2 DEEPSEEK_API_KEY=your_deepseek_key DEEPSEEK_MODEL=deepseek-chat MISTRAL_API_KEY=your_mistral_key MISTRAL_MODEL=mistral-large-latest COHERE_API_KEY=your_cohere_key COHERE_MODEL=command-a-03-2025 COHERE_CLIENT_NAME=your_cohere_client
Usage
Usage Examples ๐งโ๐ป
use LaravelAiGateway\Ai\Facades\Ai; $response = Ai::chat([ ['role' => 'system', 'content' => 'You are a helpful assistant'], ['role' => 'user', 'content' => 'Tell me a joke about Laravel'] ]); echo $response->getContent();
Using Specific Drivers
$response = Ai::driver('claude') ->withModel('claude-3-haiku-20240307') ->chat([...]);
Available Methods
chat(array $messages)
: Send chat completiondriver(string $name)
: Switch between providers
Response Format ๐ฆ
All responses implement AiResponseInterface
with these methods:
getContent()
: string - Get the response contentgetUsage()
: array - Get usage statisticsisSuccessful()
: bool - Check if request succeeded
Testing ๐งช
Run the tests with:
composer test tests
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License ๐
MIT License - Free for commercial and personal use.