PHPackages                             aitoolkit/laravel-ai-toolkit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. aitoolkit/laravel-ai-toolkit

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aitoolkit/laravel-ai-toolkit
============================

A professional, production-ready Laravel package for integrating multiple AI providers with advanced features like asyncprocessing, real-time streaming, intelligent caching, and error handling.

1.1.0(6mo ago)00PHPPHP ^8.3

Since Nov 1Pushed 6mo agoCompare

[ Source](https://github.com/dvictor357/laravel-ai-toolkit)[ Packagist](https://packagist.org/packages/aitoolkit/laravel-ai-toolkit)[ RSS](/packages/aitoolkit-laravel-ai-toolkit/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (4)Used By (0)

Laravel AI Toolkit
==================

[](#laravel-ai-toolkit)

A professional, production-ready Laravel package for integrating multiple AI providers with advanced features like async processing, real-time streaming, intelligent caching, and error handling.

[![Laravel](https://camo.githubusercontent.com/fecd29a080645e70730c97891b96f6cbb37ca29654d16787e5caca3c8dcb1728/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d313125323025374325323031322d4646324432303f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c)](https://laravel.com)[![PHP](https://camo.githubusercontent.com/291d4dd34c11534597fc8d0613fefbbf23cac46014e8f388a477f26219260e99/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332532422d3737374242343f7374796c653d666f722d7468652d6261646765266c6f676f3d706870)](https://php.net)[![License](https://camo.githubusercontent.com/9218332452902d9e542a100d0af126fd3174a116456614d2cf093546a13783db/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e7376673f7374796c653d666f722d7468652d6261646765)](LICENSE)[![Tests](https://camo.githubusercontent.com/dcdfe817f1710311da9dec2e8eaa746614d923c8fe46b6a44913b3f21e2c0d7c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54657374732d506573742d627269676874677265656e2e7376673f7374796c653d666f722d7468652d6261646765)](tests/)

✨ Features
----------

[](#-features)

### 🤖 Multi-Provider AI Support

[](#-multi-provider-ai-support)

- **OpenAI** - GPT-4, GPT-3.5, and embeddings
- **Anthropic** - Claude 4 Sonnet, Claude 3 Opus, and more
- **Groq** - Ultra-fast inference with Mixtral and Llama models
- Easy provider switching via configuration

### ⚡ Performance &amp; Reliability

[](#-performance--reliability)

- **Async Queue Jobs** - Non-blocking AI operations
- **Intelligent Caching** - Redis/Database cache with TTL and invalidation
- **Circuit Breaker Pattern** - Automatic failure protection
- **Exponential Backoff** - Smart retry logic with jitter
- **Rate Limiting** - Built-in API rate limit handling

### 🔄 Real-Time Features

[](#-real-time-features)

- **Streaming Responses** - Real-time AI response streaming
- **Laravel Reverb Integration** - WebSocket broadcasting for live updates
- **Event-Driven Architecture** - Dispatch events for UI updates

### 🔧 Developer Experience

[](#-developer-experience)

- **Type-Safe Contracts** - Interface-driven provider architecture
- **Comprehensive CLI** - Command-line tools for testing and management
- **Laravel Facades** - Easy dependency injection
- **Rich Configuration** - Environment-based provider settings
- **Extensible Design** - Easy to add new providers

📦 Installation
--------------

[](#-installation)

### Requirements

[](#requirements)

- PHP 8.3 or higher
- Laravel 11 or 12
- Composer

### Install via Composer

[](#install-via-composer)

```
composer require dvictor357/laravel-ai-toolkit
```

### Publish Configuration

[](#publish-configuration)

```
php artisan vendor:publish --provider="AIToolkit\\AIToolkit\\AiToolkitServiceProvider" --tag="config"
```

This creates `config/ai-toolkit.php` where you can configure your providers.

### Environment Variables

[](#environment-variables)

Add your API keys to `.env`:

```
# Default provider
AI_DEFAULT_PROVIDER=openai

# OpenAI
OPENAI_API_KEY=sk-your-openai-key

# Anthropic
ANTHROPIC_API_KEY=sk-ant-your-anthropic-key

# Groq
GROQ_API_KEY=gsk_your-groq-key
```

🚀 Quick Start
-------------

[](#-quick-start)

### Basic Usage

[](#basic-usage)

```
use AIToolkit\AIToolkit\Contracts\AIProviderContract;

// Dependency injection
public function chat(AIProviderContract $provider)
{
    $response = $provider->chat('Tell me a joke about programming');

    return [
        'content' => $response['content'],
        'usage' => $response['usage'], // Token usage stats
        'model' => $response['model'],
    ];
}
```

### Using Facades

[](#using-facades)

```
use AIToolkit\AIToolkit\Facades\AiCache;

// Cache AI responses
$result = AiCache::remember('chat', 'openai', 'Your prompt', function () {
    return app(AIProviderContract::class)->chat('Your prompt');
});
```

### Async Jobs

[](#async-jobs)

```
use AIToolkit\AIToolkit\Jobs\AiChatJob;

// Dispatch async job
AiChatJob::dispatch('Generate a report about AI trends', [
    'max_tokens' => 2000,
    'temperature' => 0.7
], 'unique-result-id');

// Listen for completion
event(new AiChatCompleted($response, 'unique-result-id'));
```

### Streaming Responses

[](#streaming-responses)

```
// Direct streaming
$stream = $provider->stream('Tell me a story about...');

return $stream; // Returns StreamedResponse

// Broadcasting via Reverb
$stream = $provider->streamBroadcast('Your prompt', [], 'my-channel');

// Frontend JavaScript
window.Echo.channel('ai-stream')
    .listen('AiResponseChunk', (e) => {
        if (e.chunk === '__START__') {
            // Stream started
        } else {if (e.chunk === '__END__') {
            // Stream ended
        } else {
            // Append chunk to UI
            document.getElementById('response').innerHTML += e.chunk;
        }}
    });
```

### CLI Usage

[](#cli-usage)

```
# Basic chat
php artisan ai:chat "What's the weather like today?"

# With options
php artisan ai:chat "Explain quantum computing" \
    --provider=anthropic \
    --model=claude-3-5-sonnet-20241022 \
    --max-tokens=1000 \
    --temperature=0.7

# Streaming response
php artisan ai:chat "Continue this story..." --stream

# JSON output
php artisan ai:chat "List the planets" --json
```

⚙️ Configuration
----------------

[](#️-configuration)

### Provider Settings

[](#provider-settings)

```
// config/ai-toolkit.php
return [
    'default_provider' => env('AI_DEFAULT_PROVIDER', 'openai'),

    'providers' => [
        'openai' => [
            'api_key' => env('OPENAI_API_KEY'),
            'default_model' => env('OPENAI_DEFAULT_MODEL', 'gpt-4o'),
            'default_max_tokens' => env('OPENAI_DEFAULT_MAX_TOKENS', 1024),
            'default_temperature' => env('OPENAI_DEFAULT_TEMPERATURE', 0.7),
        ],

        'anthropic' => [
            'api_key' => env('ANTHROPIC_API_KEY'),
            'default_model' => env('ANTHROPIC_DEFAULT_MODEL', 'claude-3-5-sonnet-20241022'),
            'default_max_tokens' => env('ANTHROPIC_DEFAULT_MAX_TOKENS', 1024),
            'default_temperature' => env('ANTHROPIC_DEFAULT_TEMPERATURE', 1.0),
        ],

        'groq' => [
            'api_key' => env('GROQ_API_KEY'),
            'default_model' => env('GROQ_DEFAULT_MODEL', 'mixtral-8x7b-32768'),
            'default_max_tokens' => env('GROQ_DEFAULT_MAX_TOKENS', 1024),
            'default_temperature' => env('GROQ_DEFAULT_TEMPERATURE', 0.7),
        ],
    ],
];
```

### Cache Settings

[](#cache-settings)

```
'cache' => [
    'enabled' => env('AI_CACHE_ENABLED', true),
    'ttl' => env('AI_CACHE_TTL', 3600), // 1 hour
    'prefix' => env('AI_CACHE_PREFIX', 'ai_toolkit'),
],
```

### Queue Settings

[](#queue-settings)

```
'queue' => [
    'connection' => env('AI_QUEUE_CONNECTION', null),
    'timeout' => env('AI_QUEUE_TIMEOUT', 60),
    'tries' => env('AI_QUEUE_TRIES', 3),
],
```

### Broadcasting Settings

[](#broadcasting-settings)

```
'broadcasting' => [
    'enabled' => env('AI_BROADCASTING_ENABLED', true),
    'channel' => env('AI_BROADCASTING_CHANNEL', 'ai-stream'),
],
```

🏗️ Architecture
---------------

[](#️-architecture)

### Provider Pattern

[](#provider-pattern)

All AI providers implement `AIProviderContract`:

```
interface AIProviderContract
{
    public function chat(string $prompt, array $options = []): array;
    public function stream(string $prompt, array $options = []): \Symfony\Component\HttpFoundation\StreamedResponse;
    public function embed(string $text): array;
}
```

### Service Architecture

[](#service-architecture)

```
┌─────────────────┐
│   Controllers   │
└────────┬────────┘
         │
┌────────▼────────┐
│ AIProviderContract │
└────────┬────────┘
         │
    ┌────┴─────┐
    │ Providers │
    ├──────────┤
    │ • OpenAI │
    │ • Claude │
    │ • Groq   │
    └──────────┘

```

### Caching Strategy

[](#caching-strategy)

```
Request ──┐
          │
          ├─→ Cache Hit ──→ Return Cached Response
          │
          └─→ Cache Miss ──→ Call API ──→ Store & Return

```

### Retry Logic

[](#retry-logic)

```
Request ──┐
          │
          ├─→ Success ──→ Return Response
          │
          └─→ Failure ──→ Wait (exponential backoff)
                     │
                     ├─→ Retry (up to max attempts)
                     │
                     └─→ Final Failure

```

🧪 Testing
---------

[](#-testing)

Run the test suite:

```
composer test
```

Run with coverage:

```
composer test-coverage
```

The package includes:

- **Unit Tests** - Individual component testing
- **Feature Tests** - Integration testing
- **Provider Tests** - Mock API testing
- **Service Tests** - Caching, retry logic, and jobs

📚 API Reference
---------------

[](#-api-reference)

### AIProviderContract

[](#aiprovidercontract)

#### `chat(string $prompt, array $options = []): array`

[](#chatstring-prompt-array-options---array)

Send a chat message to the AI provider.

**Parameters:**

- `$prompt` - The user message
- `$options` - Additional parameters (model, max\_tokens, temperature, etc.)

**Returns:**

```
[
    'content' => 'AI response text',
    'usage' => [
        'prompt_tokens' => 150,
        'completion_tokens' => 50,
        'total_tokens' => 200,
    ],
    'model' => 'gpt-4o',
]
```

#### `stream(string $prompt, array $options = []): StreamedResponse`

[](#streamstring-prompt-array-options---streamedresponse)

Stream a chat response in real-time.

#### `embed(string $text): array`

[](#embedstring-text-array)

Generate text embeddings (OpenAI only).

### AiCacheService

[](#aicacheservice)

#### `remember(string $operation, string $provider, string $input, callable $callback, array $options = []): mixed`

[](#rememberstring-operation-string-provider-string-input-callable-callback-array-options---mixed)

Cache AI responses with automatic TTL.

#### `generateKey(string $operation, string $provider, string $input, array $options = []): string`

[](#generatekeystring-operation-string-provider-string-input-array-options---string)

Generate consistent cache keys.

#### `invalidatePattern(string $pattern): int`

[](#invalidatepatternstring-pattern-int)

Invalidate cache by pattern.

### RetryService

[](#retryservice)

#### `execute(callable $callback, array $options = []): mixed`

[](#executecallable-callback-array-options---mixed)

Execute operations with retry logic.

**Options:**

- `max_retries` - Maximum retry attempts (default: 3)
- `base_delay` - Base delay in seconds (default: 1)
- `strategy` - 'exponential', 'linear', or 'fixed'
- `circuit_breaker` - Enable circuit breaker (default: true)

🔒 Security
----------

[](#-security)

- **API Key Validation** - Validates provider keys on initialization
- **Input Sanitization** - All inputs are sanitized before API calls
- **Rate Limiting** - Built-in rate limit handling
- **Error Handling** - No sensitive data in error messages
- **Caching** - Cached responses don't include API keys

🚀 Performance Tips
------------------

[](#-performance-tips)

### 1. Enable Caching

[](#1-enable-caching)

```
config(['ai.cache.enabled' => true]);
```

### 2. Use Async Jobs for Long Operations

[](#2-use-async-jobs-for-long-operations)

```
AiChatJob::dispatch($prompt, $options, $resultId);
```

### 3. Monitor Cache Hit Rates

[](#3-monitor-cache-hit-rates)

```
$stats = AiCache::getStats();
Log::info('Cache stats', $stats);
```

### 4. Use Circuit Breakers

[](#4-use-circuit-breakers)

```
// Circuit breaker automatically opens after 5 failures
// Resets after 60 seconds
```

📖 Advanced Usage
----------------

[](#-advanced-usage)

### Custom Providers

[](#custom-providers)

Create a custom provider by implementing the contract:

```
namespace App\AI;

use AIToolkit\AIToolkit\Contracts\AIProviderContract;

class CustomProvider implements AIProviderContract
{
    public function chat(string $prompt, array $options = []): array
    {
        // Your custom implementation
    }

    // ... implement other methods
}
```

Register in your service provider:

```
$this->app->singleton(AIProviderContract::class, function () {
    return new CustomProvider();
});
```

### Event Listeners

[](#event-listeners)

```
// Listen for AI chat completion
Event::listen(AiChatCompleted::class, function (AiChatCompleted $event) {
    if ($event->failed) {
        Log::error('AI chat failed', $event->broadcastWith());
    } else {
        Log::info('AI chat completed', $event->broadcastWith());
    }
});
```

### Custom Caching

[](#custom-caching)

```
$cacheService = app('ai-cache');

// Pre-warm cache
$cacheService->warmCache([
    [
        'prompt' => 'What is Laravel?',
        'operations' => ['chat'],
        'options' => ['temperature' => 0.7]
    ]
]);
```

🐛 Troubleshooting
-----------------

[](#-troubleshooting)

### Common Issues

[](#common-issues)

**1. "Invalid AI provider" Error**

- Check your `ai.default_provider` configuration
- Ensure the provider name matches exactly: 'openai', 'anthropic', or 'groq'

**2. API Key Not Found**

- Verify environment variables are set
- Check for typos in variable names
- Ensure the config file is published

**3. Streaming Not Working**

- Enable broadcasting in config
- Set up Laravel Reverb or Pusher
- Check browser console for WebSocket errors

**4. Queue Jobs Not Running**

- Ensure queue driver is configured
- Run `php artisan queue:work`
- Check Laravel logs for errors

### Debug Mode

[](#debug-mode)

Enable detailed logging:

```
config([
    'ai.logging.enabled' => true,
    'ai.logging.channel' => 'stack',
    'app.debug' => true,
]);
```

🤝 Contributing
--------------

[](#-contributing)

1. Fork the repository
2. Create a feature branch
3. Write tests for new functionality
4. Ensure all tests pass
5. Submit a pull request

### Development Setup

[](#development-setup)

```
git clone https://github.com/dvictor357/laravel-ai-toolkit.git
cd laravel-ai-toolkit
composer install
cp .env.example .env
php artisan key:generate
composer test
```

📄 License
---------

[](#-license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

🆘 Support
---------

[](#-support)

- 🐛 Issues: [GitHub Issues](https://github.com/dvictor357/laravel-ai-toolkit/issues)

🙏 Acknowledgments
-----------------

[](#-acknowledgments)

- [OpenAI](https://openai.com) for their excellent APIs
- [Anthropic](https://anthropic.com) for Claude
- [Groq](https://groq.com) for lightning-fast inference
- [Laravel](https://laravel.com) for the amazing framework

---

🎛️ Filament Admin Panel Integration
-----------------------------------

[](#️-filament-admin-panel-integration)

The Laravel AI Toolkit includes optional **Filament Admin Panel** integration for a beautiful web interface to manage AI providers, monitor usage, and test AI operations.

### Quick Setup

[](#quick-setup)

```
# 1. Install Filament (if not already installed)
composer require filament/filament:"^4.0"

# 2. Publish and run migration
php artisan vendor:publish --provider="AIToolkit\AIToolkit\AIToolkitServiceProvider" --tag="ai-toolkit-migrations"
php artisan migrate

# 3. Seed initial providers (optional)
php artisan db:seed --class="AIToolkit\AIToolkit\Database\Seeders\AIProviderSeeder"

# 4. Access the admin panel at /admin
```

### Features

[](#features)

- ✅ **AI Provider Management** - Configure multiple providers (OpenAI, Anthropic, Groq)
- ✅ **Real-time Chat Dashboard** - Interactive AI testing interface
- ✅ **Usage Analytics** - Monitor requests, cache hit rates, response times
- ✅ **Provider Testing** - Built-in connection testing for all providers
- ✅ **Encrypted Storage** - API keys encrypted in database
- ✅ **Dynamic Configuration** - Database-driven provider management

### Database-Driven Architecture

[](#database-driven-architecture)

The Filament integration uses a **database-first approach**:

1. **Providers stored in database** (not config files)
2. **Encrypted API keys** for security
3. **Dynamic configuration** via admin UI
4. **Seeder creates initial providers** from `.env` values

### Documentation

[](#documentation)

📖 **Full documentation available in:** [`docs/FILAMENT_INTEGRATION.md`](docs/FILAMENT_INTEGRATION.md)

Includes detailed setup, configuration, troubleshooting, and advanced usage examples.

---

**Made with ❤️ for the Laravel community**

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance67

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

192d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bc63d4428a6985b06416109b00d9a5947df245f43b3897b16168815f330b4383?d=identicon)[dvictor357](/maintainers/dvictor357)

---

Top Contributors

[![dvictor357](https://avatars.githubusercontent.com/u/187467850?v=4)](https://github.com/dvictor357 "dvictor357 (10 commits)")

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/aitoolkit-laravel-ai-toolkit/health.svg)

```
[![Health](https://phpackages.com/badges/aitoolkit-laravel-ai-toolkit/health.svg)](https://phpackages.com/packages/aitoolkit-laravel-ai-toolkit)
```

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)[eduardoribeirodev/filament-leaflet

Um widget de mapa para FilamentPHP.

134.6k](/packages/eduardoribeirodev-filament-leaflet)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
