Seamlessly integrate Grok AI into Laravel applications with an elegant, developer-friendly package.
Leverage powerful AI models for chat, automation, and NLP, while maintaining Laravel's expressive simplicity.
- ✨ Features
- 📦 Installation
- 🚀 Quick Start
- 📌 Available Grok AI Models
- ⚡ Streaming Responses
- 🧪 Testing
- 🔒 Security
- 🤝 Contributing
- 📄 License
✅ Seamless Laravel Integration – Works effortlessly with Laravel 10+
✅ Simple API Client – Access Grok AI models with a fluent, clean syntax
✅ Supports All Grok AI Models – Choose from multiple LLM & vision models
✅ Streaming Capable – Enable real-time AI responses for interactive experiences
✅ Configurable Defaults – Set your preferred model, temperature, and more
Install via Composer:
composer require grok-php/laravel
After installation, run the setup command:
php artisan grok:install
This command will:
- Publish the configuration file (
config/grok.php
). - Add necessary environment variables to
.env
and.env.example
.
Add your API key in .env
:
GROK_API_KEY=your-api-key
use GrokPHP\Laravel\Facades\GrokAI;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;
$response = GrokAI::chat(
[['role' => 'user', 'content' => 'Hello Grok!']],
new ChatOptions(model: Model::GROK_2)
);
echo $response['choices'][0]['message']['content'];
Model: grok-2 Temperature: 0.7 Streaming: false
Modify your config/grok.php
file:
return [
'api_key' => env('GROK_API_KEY'),
'base_uri' => env('GROK_BASE_URI', 'https://api.grok.com/v1'),
'default_model' => env('GROK_DEFAULT_MODEL', 'grok-2'),
'default_temperature' => env('GROK_DEFAULT_TEMPERATURE', 0.7),
'enable_streaming' => env('GROK_ENABLE_STREAMING', false),
'timeout' => env('GROK_API_TIMEOUT', 30),
];
📌 You can override any setting dynamically:
$response = GrokAI::chat(
[['role' => 'user', 'content' => 'Explain black holes']],
new ChatOptions(model: Model::GROK_2_LATEST, temperature: 1.2, stream: true)
);
Grok AI offers multiple models, each optimized for different use cases.
These models are available in the Model enum inside our package:
📄 src/Enums/Model.php
Model Enum | API Model Name | Description |
---|---|---|
Model::GROK_VISION_BETA |
grok-vision-beta | Experimental vision-enabled model |
Model::GROK_2_VISION |
grok-2-vision | Advanced multi-modal vision model |
Model::GROK_2_VISION_LATEST |
grok-2-vision-latest | Latest iteration of Grok vision models |
Model::GROK_2_VISION_1212 |
grok-2-vision-1212 | Enhanced vision model with performance improvements |
Model::GROK_2_1212 |
grok-2-1212 | Optimized chat model |
Model::GROK_2 |
grok-2 | Default general-purpose Grok model |
Model::GROK_2_LATEST |
grok-2-latest | Latest iteration of Grok-2 |
Model::GROK_BETA |
grok-beta | Experimental beta model |
Enable real-time AI responses by setting stream: true
:
$response = GrokAI::chat(
[['role' => 'user', 'content' => 'Tell me a story']],
new ChatOptions(model: Model::GROK_2, stream: true)
);
Run tests using Pest PHP:
composer test
or
vendor/bin/phpunit
If you discover a security vulnerability, please report it via email: 📩 [email protected]
Want to improve this package? Check out CONTRIBUTE.md for contribution guidelines.
This package is open-source software licensed under the MIT License.