|
| 1 | +# 🧠 Grok AI Laravel |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +**Seamlessly integrate Grok AI into Laravel applications with an elegant, developer-friendly package.** |
| 6 | +Leverage **powerful AI models** for **chat, automation, and NLP**, while maintaining Laravel's expressive simplicity. |
| 7 | + |
| 8 | +[](https://packagist.org/packages/grok-php/laravel) |
| 9 | +[](https://php.net) |
| 10 | +[](https://laravel.com) |
| 11 | +[](LICENSE.md) |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 📖 Table of Contents |
| 16 | +- [✨ Features](#-features) |
| 17 | +- [📦 Installation](#-installation) |
| 18 | +- [🚀 Quick Start](#-quick-start) |
| 19 | + - [Basic Usage](#basic-usage) |
| 20 | + - [Advanced Configuration](#advanced-configuration) |
| 21 | +- [📌 Available Grok AI Models](#-available-grok-ai-models) |
| 22 | +- [⚡ Streaming Responses](#-streaming-responses) |
| 23 | +- [🧪 Testing](#-testing) |
| 24 | +- [🔒 Security](#-security) |
| 25 | +- [🤝 Contributing](#-contributing) |
| 26 | +- [📄 License](#-license) |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## ✨ Features |
| 31 | + |
| 32 | +✅ **Seamless Laravel Integration** – Works effortlessly with Laravel 10+ |
| 33 | +✅ **Simple API Client** – Access Grok AI models with a fluent, clean syntax |
| 34 | +✅ **Supports All Grok AI Models** – Choose from multiple **LLM & vision models** |
| 35 | +✅ **Streaming Capable** – Enable **real-time AI responses** for interactive experiences |
| 36 | +✅ **Configurable Defaults** – Set your preferred model, temperature, and more |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## 📦 Installation |
| 41 | + |
| 42 | +Install via **Composer**: |
| 43 | +```sh |
| 44 | +composer require grok-php/laravel |
| 45 | +``` |
| 46 | + |
| 47 | +After installation, run the setup command: |
| 48 | + |
| 49 | +```sh |
| 50 | +php artisan grok:install |
| 51 | +``` |
| 52 | +This command will: |
| 53 | + |
| 54 | +- Publish the configuration file (`config/grok.php`). |
| 55 | +- Add necessary environment variables to `.env` and `.env.example`. |
| 56 | + |
| 57 | +Add your API key in `.env`: |
| 58 | +```sh |
| 59 | +GROK_API_KEY=your-api-key |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | + |
| 65 | +## 🚀 Quick Start |
| 66 | + |
| 67 | +### Basic Usage |
| 68 | + |
| 69 | +```php |
| 70 | +use GrokPHP\Laravel\Facades\GrokAI; |
| 71 | +use GrokPHP\Client\Config\ChatOptions; |
| 72 | +use GrokPHP\Client\Enums\Model; |
| 73 | + |
| 74 | +$response = GrokAI::chat( |
| 75 | + [['role' => 'user', 'content' => 'Hello Grok!']], |
| 76 | + new ChatOptions(model: Model::GROK_2) |
| 77 | +); |
| 78 | + |
| 79 | +echo $response['choices'][0]['message']['content']; |
| 80 | +``` |
| 81 | + |
| 82 | +### 📌 Defaults Used: |
| 83 | +Model: grok-2 |
| 84 | +Temperature: 0.7 |
| 85 | +Streaming: false |
| 86 | + |
| 87 | +### Advanced Configuration |
| 88 | +Modify your `config/grok.php` file: |
| 89 | + |
| 90 | +```php |
| 91 | +return [ |
| 92 | + 'api_key' => env('GROK_API_KEY'), |
| 93 | + 'base_uri' => env('GROK_BASE_URI', 'https://api.grok.com/v1'), |
| 94 | + 'default_model' => env('GROK_DEFAULT_MODEL', 'grok-2'), |
| 95 | + 'default_temperature' => env('GROK_DEFAULT_TEMPERATURE', 0.7), |
| 96 | + 'enable_streaming' => env('GROK_ENABLE_STREAMING', false), |
| 97 | + 'timeout' => env('GROK_API_TIMEOUT', 30), |
| 98 | +]; |
| 99 | +``` |
| 100 | + |
| 101 | +📌 You can override any setting dynamically: |
| 102 | + |
| 103 | +```php |
| 104 | +$response = GrokAI::chat( |
| 105 | + [['role' => 'user', 'content' => 'Explain black holes']], |
| 106 | + new ChatOptions(model: Model::GROK_2_LATEST, temperature: 1.2, stream: true) |
| 107 | +); |
| 108 | +``` |
| 109 | +--- |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +## 📌 Available Grok AI Models |
| 115 | +Grok AI offers multiple models, each optimized for different use cases. |
| 116 | +These models are available in the Model enum inside our package: |
| 117 | +📄 `src/Enums/Model.php` |
| 118 | + |
| 119 | +| Model Enum | API Model Name | Description | |
| 120 | +|-----------------------------|----------------------|-----------------------------------------------------| |
| 121 | +| `Model::GROK_VISION_BETA` | grok-vision-beta | Experimental vision-enabled model | |
| 122 | +| `Model::GROK_2_VISION` | grok-2-vision | Advanced multi-modal vision model | |
| 123 | +| `Model::GROK_2_VISION_LATEST` | grok-2-vision-latest | Latest iteration of Grok vision models | |
| 124 | +| `Model::GROK_2_VISION_1212` | grok-2-vision-1212 | Enhanced vision model with performance improvements | |
| 125 | +| `Model::GROK_2_1212` | grok-2-1212 | Optimized chat model | |
| 126 | +| `Model::GROK_2` | grok-2 | Default general-purpose Grok model | |
| 127 | +| `Model::GROK_2_LATEST` | grok-2-latest | Latest iteration of Grok-2 | |
| 128 | +| `Model::GROK_BETA` | grok-beta | Experimental beta model | |
| 129 | + |
| 130 | +#### 📌 Default model used: `Model::GROK_2` |
| 131 | +--- |
| 132 | + |
| 133 | + |
| 134 | +## ⚡ Streaming Responses |
| 135 | +Enable real-time AI responses by setting `stream: true`: |
| 136 | + |
| 137 | +```php |
| 138 | +$response = GrokAI::chat( |
| 139 | + [['role' => 'user', 'content' => 'Tell me a story']], |
| 140 | + new ChatOptions(model: Model::GROK_2, stream: true) |
| 141 | +); |
| 142 | +``` |
| 143 | +Streaming is useful for chatbots, assistants, and real-time applications. |
| 144 | +--- |
| 145 | + |
| 146 | +## 🧪 Testing |
| 147 | +Run tests using Pest PHP: |
| 148 | + |
| 149 | +```sh |
| 150 | +composer test |
| 151 | +or |
| 152 | +vendor/bin/phpunit |
| 153 | +``` |
| 154 | + |
| 155 | +## 🔒 Security |
| 156 | +If you discover a security vulnerability, please report it via email: |
| 157 | +📩 [thefeqy@gmail.com](mailto:thefeqy@gmail.com) |
| 158 | + |
| 159 | +## 🤝 Contributing |
| 160 | + |
| 161 | +Want to improve this package? Check out [CONTRIBUTE.md](CONTRIBUTE.md) for contribution guidelines. |
| 162 | + |
| 163 | +## 📄 License |
| 164 | + |
| 165 | +This package is open-source software licensed under the [MIT License](LICENSE). |
0 commit comments