LLM Magic provides seamless integration with Laravel Livewire to create reactive, real-time chat interfaces. Build chatbots and AI assistants with minimal code.
Livewire integration is currently experimental and under active development. The API may change in future versions.
Create an interactive chat component using the HasChat interface and InteractsWithChat trait:
<?phpuse Livewire\Component;use Mateffy\Magic;use Mateffy\Magic\Livewire\HasChat;use Mateffy\Magic\Livewire\InteractsWithChat;class LivewireChat extends Component implements HasChat{ use InteractsWithChat; public function getMagic() { return Magic::chat() ->model('google/gemini-2.0-flash-lite') ->system('You are a helpful assistant.'); } public function render() { return view('livewire.chat'); }}
use Mateffy\Magic\Tools\Widgets\ClosureToolWidget;protected static function getTools(): array{ return [ Tool::make('makeSandwich') ->widget(ClosureToolWidget::make(fn () => '<p>You just made a sandwich!</p>')) ->callback(function (string $bread, string $filling) { return [ 'sandwich' => "A sandwich with {$bread} and {$filling}." ]; }), ];}
Widgets allow you to display rich UI feedback when tools are executed, enhancing the user experience.
use Mateffy\Magic\Tools\Prebuilt\EloquentTools;class ProductAssistant extends Component implements HasChat{ use InteractsWithChat; protected static function getTools(): array { return [ // Full CRUD for products ...EloquentTools::crud(\App\Models\Product::class), // Read-only access to orders ...EloquentTools::crud( model: \App\Models\Order::class, create: false, update: false, delete: false, ), ]; } public function getMagic() { return Magic::chat() ->model('anthropic/claude-3-5-sonnet-latest') ->system('You are a product management assistant. You can query, create, update, and delete products as requested.'); }}
Security Notice: When exposing CRUD operations to LLMs, always implement proper authorization and validation. Consider using Laravel policies to restrict access.
use Livewire\WithFileUploads;class DocumentChat extends Component implements HasChat{ use InteractsWithChat, WithFileUploads; public $document; public function getMagic() { $artifacts = []; if ($this->document) { $artifacts[] = Magic\Extraction\Artifacts\Artifact::fromFile( $this->document->getRealPath() ); } return Magic::chat() ->model('google/gemini-2.0-flash') ->system('You are a document analysis assistant.') ->artifacts($artifacts); }}
use Mateffy\Magic\Chat\Messages\Step;class VisionChat extends Component implements HasChat{ use InteractsWithChat; public function analyzeImage(string $imageUrl) { $this->addMessage( Step::user([ Step\Text::make('What is in this image?'), Step\Image::make($imageUrl), ]) ); $this->sendMessage(); } public function getMagic() { return Magic::chat() ->model('google/gemini-2.0-flash') // Vision-capable model ->system('You are a vision analysis assistant.'); }}
Protect your application from excessive API usage:
use Illuminate\Support\Facades\RateLimiter;class RateLimitedChat extends Component implements HasChat{ use InteractsWithChat; public function sendMessage() { $key = 'chat:' . auth()->id(); if (RateLimiter::tooManyAttempts($key, 10)) { $this->addError('Too many requests. Please wait before sending more messages.'); return; } RateLimiter::hit($key, 60); // 10 requests per minute parent::sendMessage(); }}