1
- <h1 align =" center " >Grok PHP: The Ultimate PHP Library for Grok AI</h1 >
1
+ <h1 align =" center " >Grok PHP: The 2-in-1 PHP SDK for Grok AI</h1 >
2
2
3
3
<p align =" center " >
4
4
<a href="https://packagist.org/packages/alvincoded/grok-php-client">
5
5
<img src="https://img.shields.io/packagist/v/alvincoded/grok-php-client" alt="Latest Version">
6
6
</a>
7
7
<a href="https://php.net">
8
- <img src="https://img.shields.io/badge/PHP-8.1%2B-blue" alt="PHP Version">
8
+ <img src="https://img.shields.io/badge/PHP-8.2%2B-blue" alt="PHP Version">
9
+ </a>
10
+ <a href="https://laravel.com">
11
+ <img src="https://img.shields.io/badge/Laravel-11%2B-red" alt="PHP Version">
9
12
</a>
10
13
<a href="LICENSE.md">
11
14
<img src="https://img.shields.io/badge/license-MIT-brightgreen" alt="License">
12
15
</a>
13
16
</p >
14
17
15
- Grok PHP is a robust, flexible, and feature-rich PHP library designed to interact seamlessly with the Grok AI API.
18
+ Grok PHP is a 2-in-1 PHP SDK offering seamless integration with Grok AI API for both ** framework-agnostic PHP ** and ** Laravel 11+ ** applications .
16
19
17
20
## Features
18
21
19
- - ** Seamless Integration:** Elegant PHP-first interface with intuitive methods for Grok AI
22
+ - ** Dual Architecture** : Use as framework-agnostic PHP library or first-class Laravel package with extensive error handling
23
+ - ** Full API Coverage** : Chat, completions, images, embeddings, and structured outputs
24
+ - ** Modern PHP** : Strict types, enums, and attributes for schema definition
25
+ - ** Laravel Integration** : Auto-discovery, config publishing, and facade support
20
26
- ** Advanced Chat Capabilities:** Full support for multi-turn conversations and real-time streaming
21
- - ** Comprehensive Features:** Text completions, image analysis, and embeddings all in one!
22
- - ** Robust Architecture:** Type-safe implementations with extensive error handling
23
27
- ** Model Flexibility:** Support for multiple Grok models (Grok-2, Grok-2-Vision, etc.)
24
28
- ** Enterprise Ready:** Secure API handling with proper authentication
25
- - ** Response Management:** Rich response objects with detailed analytics and metadata
26
29
- ** Easy Configuration:** Simple setup with minimal dependencies
27
30
28
31
## Requirements
29
32
30
- - PHP 8.1 or higher
33
+ - PHP 8.2 or higher
34
+ - Laravel 11+ _ (For Laravel applications)_
31
35
- [ Composer] ( https://getcomposer.org )
32
36
- [ Grok AI API key] ( https://docs.x.ai/docs/overview )
33
37
@@ -38,22 +42,24 @@ Install Grok PHP via Composer:
38
42
``` bash
39
43
composer require alvincoded/grok-php-client
40
44
```
45
+ <img src =" https://img.shields.io/badge/new-brightgreen " alt =" New " width =' 20 ' > _ Do the following with __ Laravel applications only__ :_
46
+ ``` bash
47
+ php artisan grok:install
48
+ ```
49
+ > ** Note:** This command publishes the configuration file and adds the relevant environment variables to your ` .env ` file.
50
+
41
51
42
52
## Quick Start
43
53
44
- > ** Here's how simple it is to use Grok PHP :**
54
+ __ *** Framework-agnostic PHP Usage :*** __
45
55
46
- #### _ Chat Completion_
56
+ ##### _ Chat Completion_
47
57
48
58
``` php
49
- <?php
50
-
51
- require_once 'vendor/autoload.php';
52
-
53
59
use GrokPHP\Client\GrokClient;
54
60
use GrokPHP\Params;
55
61
56
- $client = new GrokClient();
62
+ $client = new GrokClient($apiKey );
57
63
58
64
// Simple chat
59
65
$response = $client->chat()->generate("Tell me a joke about AI");
@@ -85,17 +91,13 @@ $response = $chat->send('Give me an example');
85
91
echo $response->text();
86
92
```
87
93
88
- #### _ Text Completions_
94
+ ##### _ Text Completions_
89
95
90
96
``` php
91
- <?php
92
-
93
- require_once 'vendor/autoload.php';
94
-
95
97
use GrokPHP\Client\GrokClient;
96
98
use GrokPHP\Params;
97
99
98
- $client = new GrokClient();
100
+ $client = new GrokClient($apiKey );
99
101
100
102
// Basic completion
101
103
$response = $client->completions()->create(
@@ -114,17 +116,13 @@ $responses = $client->completions()->createMultiple(
114
116
$tokenCount = $client->completions()->getTokenCount("Sample text");
115
117
```
116
118
117
- #### _ Image Understanding_
119
+ ##### _ Image Understanding_
118
120
119
121
``` php
120
- <?php
121
-
122
- require_once 'vendor/autoload.php';
123
-
124
122
use GrokPHP\Client\GrokClient;
125
123
use GrokPHP\Params;
126
124
127
- $client = new GrokClient();
125
+ $client = new GrokClient($apiKey );
128
126
129
127
// Basic image analysis
130
128
$response = $client->images()->analyze('https://picsum.photos/200/300');
@@ -139,32 +137,24 @@ $response = $client->images()->analyze(
139
137
// Check image content
140
138
$containsPeople = $response->containsContent('person');
141
139
```
142
- #### _ Embeddings_
140
+ ##### _ Embeddings_
143
141
144
142
``` php
145
- <?php
146
-
147
- require_once 'vendor/autoload.php';
148
-
149
143
use GrokPHP\Client\GrokClient;
150
144
151
- $client = new GrokClient();
145
+ $client = new GrokClient($apiKey );
152
146
153
147
$embeddingResponse = $client->embeddings()->create('Hello, world!');
154
148
$embeddings = $embeddingResponse->getEmbeddings();
155
149
```
156
150
157
- #### _ Model-specific executions_
151
+ ##### _ Model-specific executions_
158
152
159
153
``` php
160
- <?php
161
-
162
- require_once 'vendor/autoload.php';
163
-
164
154
use GrokPHP\Client\GrokClient;
165
155
use GrokPHP\Enums\Model;
166
156
167
- $client = new GrokClient();
157
+ $client = new GrokClient($apiKey );
168
158
169
159
// Simple chat (with model specification)
170
160
$response = $client->model(Model::GROK_2_1212)->generate('Tell me a joke about AI');
@@ -179,10 +169,8 @@ echo $config->modelSupportsStreaming($model) // true
179
169
echo $config->modelSupportsFunctions($model) // false
180
170
```
181
171
182
- #### _ Structured Output_
172
+ ##### _ Structured Output_
183
173
``` php
184
- <?php
185
-
186
174
use GrokPHP\Client\GrokClient;
187
175
use GrokPHP\Enums\Model;
188
176
@@ -205,7 +193,7 @@ $jsonSchema = [
205
193
206
194
207
195
// 2. Process documents
208
- $client = new GrokClient();
196
+ $client = new GrokClient($apiKey );
209
197
210
198
foreach ($researchPapers as $paperText) {
211
199
$metadata = $client->chat()->generateStructured($paperText, $jsonSchema);
@@ -221,7 +209,7 @@ foreach ($researchPapers as $paperText) {
221
209
}
222
210
```
223
211
224
- #### _ Structured Output (alt. option with PHP class)_
212
+ ##### _ Structured Output (alt. option with PHP class)_
225
213
226
214
``` php
227
215
// Define your schema as a PHP class
@@ -249,6 +237,35 @@ echo $result->authors[0];
249
237
250
238
```
251
239
240
+ <img src =" https://img.shields.io/badge/new-brightgreen " alt =" New " width =' 20 ' > __ *** Laravel Usage :*** __
241
+
242
+ The coolest part about using Laravel with Grok PHP? You don't have to learn any new tricks! Just use it the same way you would with the framework-agnostic PHP and you're good to go. It's like magic, but better! ✨
243
+
244
+ ``` php
245
+ use GrokPHP\Enums\Model;
246
+ use GrokPHP\Facades\Grok;
247
+ use GrokPHP\Client\GrokClient;
248
+ use GrokPHP\Params;
249
+
250
+ public function __construct(
251
+ private GrokClient $grok
252
+ ) {}
253
+
254
+ public function analyzeImage(): Response
255
+ {
256
+ return $this->grok->model(Model::GROK_2_VISION_1212)->images()->analyze('https://picsum.photos/200/300.jpg');
257
+ }
258
+
259
+ // Using the facade
260
+ public function ask(): Response
261
+ {
262
+ $prompt = "Do you know the muffin man?";
263
+ $params = Params::create()->maxTokens(300)->temperature(0.8);
264
+
265
+ return Grok::model(Model::GROK_2_1212)->chat()->generate($prompt, $params);
266
+ }
267
+ ```
268
+
252
269
253
270
## Response Handling
254
271
@@ -289,7 +306,6 @@ try {
289
306
echo "Error: " . $e->getMessage();
290
307
}
291
308
```
292
- <br >
293
309
294
310
## Supported Models
295
311
@@ -324,8 +340,12 @@ try {
324
340
325
341
## Environment Variables
326
342
Add the following to your ` .env ` file:
327
- ```
343
+ ``` bash
328
344
GROK_API_KEY=your-api-key
345
+
346
+ # Include if Laravel is used
347
+ GROK_DEFAULT_MODEL=grok-2-latest
348
+ GROK_BASE_URL=https://api.x.ai
329
349
```
330
350
<br >
331
351
0 commit comments