grok-php/laravel
Laravel integration for the GrokPHP client. Add Grok AI chat and vision to Laravel apps with a clean facade API, configurable defaults (model, temperature, timeout), streaming responses, and simple install via artisan. Supports Laravel 10–12, PHP 8.2+.
Install the package via Composer and run the php artisan grok:install command to publish config and set up environment variables. Update your .env with a valid Grok API key: GROK_API_KEY=sk-.... Start using the GrokAI facade for basic chat or vision requests—no service provider bootstrapping needed beyond auto-discovery.
Use GrokAI::chat() for standard conversation flows, passing structured messages and ChatOptions for model selection (e.g., Model::GROK_2), temperature, and streaming. For multi-modal tasks like analyzing uploaded images, use GrokAI::vision()->analyze($urlOrPath, $prompt). Integrate into jobs, commands, or middleware for async or on-demand AI processing—such as automatically tagging user-uploaded content or summarizing long-form inputs. Configure defaults in config/grok.php to set fallback models, timeouts, and retry behavior project-wide.
⚠️ The vision endpoint only accepts image-capable models (grok-2-vision* or grok-vision-beta)—passing text-only models like GROK_2 with image payloads throws a clear GrokException. Always wrap calls in try/catch for GrokException and check for network or rate-limit errors. Use stream: true in ChatOptions for real-time UX in chat interfaces, but note streaming responses require frontend adaptability (e.g., SSE or WebSockets). Update GROK_API_KEY in .env before running tests—env values aren’t inherited from phpunit.xml unless explicitly declared. For caching or mock testing, extend the GrokClient or use Laravel’s App::swap() in phpunit.xml’s bootstrap. Finally, keep an eye on model enums (e.g., GROK_2_LATEST) as Grok’s API evolves—use Model::GROK_2_VISION_LATEST only when you need the newest vision features.
How can I help you explore Laravel packages today?