matrixbrains/laravel-ai

Laravel AI wrapper package with multiple AI provider drivers (OpenAI, Gemini, Claude, Mistral)

1.0.0 2025-09-09 07:37 UTC

This package is auto-updated.

Last update: 2025-09-16 06:44:48 UTC


README

License PHP Laravel

A simple, elegant Laravel package to integrate multiple AI providers with a unified interface. Supports OpenAI, Google Gemini, Anthropic Claude, and Mistral AI.

Features

  • Unified Ai::ask() method for all providers
  • Supports text generation & chat prompts
  • Easily switch between AI providers via .env
  • Extensible: add your own AI driver if needed
  • Free tier support: Google Gemini & Mistral
  • Fully ready for Laravel applications

Installation

Require the package via Composer:

composer require matrixbrains/laravel-ai

Publish the config file:

php artisan vendor:publish --provider="Matrixbrains\LaravelAi\AiServiceProvider" --tag="config"

Configuration

Edit your .env:

AI_DRIVER=gemini   # Options: openai, gemini, claude, mistral

# OpenAI
OPENAI_API_KEY=your_openai_key
OPENAI_MODEL=gpt-4o-mini

# Google Gemini
GEMINI_API_KEY=your_gemini_key
GEMINI_MODEL=gemini-2.0-flash

# Anthropic Claude
CLAUDE_API_KEY=your_claude_key
CLAUDE_MODEL=claude-3-5-sonnet-20240620

# Mistral
MISTRAL_API_KEY=your_mistral_key
MISTRAL_MODEL=mistral-small

Usage

Use the facade to send a prompt:

use Matrixbrains\LaravelAi\Facades\Ai;

$response = Ai::ask("Write a short poem about Laravel.");
echo $response;

The same method works for all drivers. Just switch AI_DRIVER in .env to change the provider.

Example with multiple drivers

// OpenAI
config(['ai.default' => 'openai']);
$response = Ai::ask("Explain AI in simple terms.");

// Google Gemini
config(['ai.default' => 'gemini']);
$response = Ai::ask("Generate a motivational quote.");

// Claude
config(['ai.default' => 'claude']);
$response = Ai::ask("Write a haiku about coding.");

// Mistral
config(['ai.default' => 'mistral']);
$response = Ai::ask("Summarize the latest Laravel release notes.");

Extending with custom drivers

Add a new driver in src/Drivers and register it in AiManager.php. All drivers should implement the AiDriver interface:

class CustomAiDriver
{
    public function ask(string $prompt): string
    {
        // Your AI API integration
    }
}

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'Add new feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

License

This package is open-source and licensed under the MIT License.

Supported AI Providers

Provider Free Tier Notes
OpenAI Limited Requires API key
Google Gemini Yes 1,500 requests/day free
Anthropic Claude Limited Free credits for new users
Mistral AI Yes Small models free tier

Matrixbrains Laravel AI makes integrating multiple AI tools seamless for your Laravel apps.