Skip to content

Commit ace546c

Browse files
committed
feat: new compatibility with Laravel 11+ applications
- Updated docs. - Added Laravel 11+ compatibility - Dropped support for PHP 8.1. - Updated PHP version to 8.2 or higher. - Added command for Laravel integration for publishing config file and adds the relevant environment variables to your .env file. - Made bug fixes and minor changes.
1 parent b87820d commit ace546c

36 files changed

+490
-255
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
php-version: ['8.1', '8.2']
19+
php-version: ['8.2', '8.3', '8.4']
2020
dependency-version: ['latest', 'lowest']
2121

2222
steps:
@@ -32,7 +32,7 @@ jobs:
3232
- name: Get Composer Cache Directory
3333
id: composer-cache
3434
run: |
35-
echo "::set-output name=dir::$(composer config cache-files-dir)"
35+
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_ENV
3636
3737
- name: Cache Composer dependencies
3838
uses: actions/cache@v3

README.md

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
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>
22

33
<p align="center">
44
<a href="https://packagist.org/packages/alvincoded/grok-php-client">
55
<img src="https://img.shields.io/packagist/v/alvincoded/grok-php-client" alt="Latest Version">
66
</a>
77
<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">
912
</a>
1013
<a href="LICENSE.md">
1114
<img src="https://img.shields.io/badge/license-MIT-brightgreen" alt="License">
1215
</a>
1316
</p>
1417

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.
1619

1720
## Features
1821

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
2026
- **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
2327
- **Model Flexibility:** Support for multiple Grok models (Grok-2, Grok-2-Vision, etc.)
2428
- **Enterprise Ready:** Secure API handling with proper authentication
25-
- **Response Management:** Rich response objects with detailed analytics and metadata
2629
- **Easy Configuration:** Simple setup with minimal dependencies
2730

2831
## Requirements
2932

30-
- PHP 8.1 or higher
33+
- PHP 8.2 or higher
34+
- Laravel 11+ _(For Laravel applications)_
3135
- [Composer](https://getcomposer.org)
3236
- [Grok AI API key](https://docs.x.ai/docs/overview)
3337

@@ -38,22 +42,24 @@ Install Grok PHP via Composer:
3842
```bash
3943
composer require alvincoded/grok-php-client
4044
```
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+
4151

4252
## Quick Start
4353

44-
> **Here's how simple it is to use Grok PHP :**
54+
__***Framework-agnostic PHP Usage :***__
4555

46-
#### _Chat Completion_
56+
##### _Chat Completion_
4757

4858
```php
49-
<?php
50-
51-
require_once 'vendor/autoload.php';
52-
5359
use GrokPHP\Client\GrokClient;
5460
use GrokPHP\Params;
5561

56-
$client = new GrokClient();
62+
$client = new GrokClient($apiKey);
5763

5864
// Simple chat
5965
$response = $client->chat()->generate("Tell me a joke about AI");
@@ -85,17 +91,13 @@ $response = $chat->send('Give me an example');
8591
echo $response->text();
8692
```
8793

88-
#### _Text Completions_
94+
##### _Text Completions_
8995

9096
```php
91-
<?php
92-
93-
require_once 'vendor/autoload.php';
94-
9597
use GrokPHP\Client\GrokClient;
9698
use GrokPHP\Params;
9799

98-
$client = new GrokClient();
100+
$client = new GrokClient($apiKey);
99101

100102
// Basic completion
101103
$response = $client->completions()->create(
@@ -114,17 +116,13 @@ $responses = $client->completions()->createMultiple(
114116
$tokenCount = $client->completions()->getTokenCount("Sample text");
115117
```
116118

117-
#### _Image Understanding_
119+
##### _Image Understanding_
118120

119121
```php
120-
<?php
121-
122-
require_once 'vendor/autoload.php';
123-
124122
use GrokPHP\Client\GrokClient;
125123
use GrokPHP\Params;
126124

127-
$client = new GrokClient();
125+
$client = new GrokClient($apiKey);
128126

129127
// Basic image analysis
130128
$response = $client->images()->analyze('https://picsum.photos/200/300');
@@ -139,32 +137,24 @@ $response = $client->images()->analyze(
139137
// Check image content
140138
$containsPeople = $response->containsContent('person');
141139
```
142-
#### _Embeddings_
140+
##### _Embeddings_
143141

144142
```php
145-
<?php
146-
147-
require_once 'vendor/autoload.php';
148-
149143
use GrokPHP\Client\GrokClient;
150144

151-
$client = new GrokClient();
145+
$client = new GrokClient($apiKey);
152146

153147
$embeddingResponse = $client->embeddings()->create('Hello, world!');
154148
$embeddings = $embeddingResponse->getEmbeddings();
155149
```
156150

157-
#### _Model-specific executions_
151+
##### _Model-specific executions_
158152

159153
```php
160-
<?php
161-
162-
require_once 'vendor/autoload.php';
163-
164154
use GrokPHP\Client\GrokClient;
165155
use GrokPHP\Enums\Model;
166156

167-
$client = new GrokClient();
157+
$client = new GrokClient($apiKey);
168158

169159
// Simple chat (with model specification)
170160
$response = $client->model(Model::GROK_2_1212)->generate('Tell me a joke about AI');
@@ -179,10 +169,8 @@ echo $config->modelSupportsStreaming($model) // true
179169
echo $config->modelSupportsFunctions($model) // false
180170
```
181171

182-
#### _Structured Output_
172+
##### _Structured Output_
183173
```php
184-
<?php
185-
186174
use GrokPHP\Client\GrokClient;
187175
use GrokPHP\Enums\Model;
188176

@@ -205,7 +193,7 @@ $jsonSchema = [
205193

206194

207195
// 2. Process documents
208-
$client = new GrokClient();
196+
$client = new GrokClient($apiKey);
209197

210198
foreach ($researchPapers as $paperText) {
211199
$metadata = $client->chat()->generateStructured($paperText, $jsonSchema);
@@ -221,7 +209,7 @@ foreach ($researchPapers as $paperText) {
221209
}
222210
```
223211

224-
#### _Structured Output (alt. option with PHP class)_
212+
##### _Structured Output (alt. option with PHP class)_
225213

226214
```php
227215
// Define your schema as a PHP class
@@ -249,6 +237,35 @@ echo $result->authors[0];
249237

250238
```
251239

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+
252269

253270
## Response Handling
254271

@@ -289,7 +306,6 @@ try {
289306
echo "Error: " . $e->getMessage();
290307
}
291308
```
292-
<br>
293309

294310
## Supported Models
295311

@@ -324,8 +340,12 @@ try {
324340

325341
## Environment Variables
326342
Add the following to your `.env` file:
327-
```
343+
```bash
328344
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
329349
```
330350
<br>
331351

composer.json

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
{
22
"name": "alvincoded/grok-php-client",
3-
"description": "A robust and feature-rich PHP wrapper for the Grok AI API",
3+
"description": "A robust and feature-rich 2-in-1 PHP SDK for the Grok AI API",
44
"type": "library",
55
"license": "MIT",
6-
"keywords": ["grok", "ai", "xai", "machine learning", "artificial intelligence", "chat", "api", "wrapper"],
6+
"keywords": ["grok", "ai", "xai", "machine learning", "artificial intelligence", "laravel", "api", "sdk"],
77
"require": {
8-
"php": "^8.1",
9-
"guzzlehttp/guzzle": "^7.8",
10-
"ext-json": "*",
11-
"psr/http-message": "^1.1|^2.0",
8+
"php": "^8.2",
9+
"guzzlehttp/guzzle": "^7.9",
10+
"psr/http-message": "^2.0",
1211
"psr/http-client": "^1.0"
1312
},
1413
"require-dev": {
15-
"phpunit/phpunit": "^10.0",
16-
"phpstan/phpstan": "^1.10",
17-
"squizlabs/php_codesniffer": "^3.7",
14+
"phpunit/phpunit": "^10.5",
15+
"phpstan/phpstan": "^1.11",
16+
"squizlabs/php_codesniffer": "^3.8",
1817
"mockery/mockery": "^1.6",
19-
"symfony/var-dumper": "^6.0",
20-
"vlucas/phpdotenv": "^5.4"
18+
"symfony/var-dumper": "^7.0",
19+
"vlucas/phpdotenv": "^5.6"
20+
},
21+
"suggest": {
22+
"laravel/framework": "Required for Laravel integration (11.x or higher)"
23+
},
24+
"extra": {
25+
"laravel": {
26+
"providers": [
27+
"GrokPHP\\Laravel\\GrokServiceProvider"
28+
],
29+
"aliases": {
30+
"Grok": "GrokPHP\\Laravel\\Facades\\Grok"
31+
}
32+
}
2133
},
2234
"autoload": {
2335
"psr-4": {
@@ -34,11 +46,17 @@
3446
"test-coverage": "phpunit --coverage-html coverage",
3547
"phpstan": "phpstan analyse src tests",
3648
"cs-check": "phpcs",
37-
"cs-fix": "phpcbf"
49+
"cs-fix": "phpcbf",
50+
"post-autoload-dump": [
51+
"@php artisan package:discover --ansi"
52+
]
3853
},
3954
"config": {
4055
"sort-packages": true,
41-
"optimize-autoloader": true
56+
"optimize-autoloader": true,
57+
"allow-plugins": {
58+
"php-http/discovery": true
59+
}
4260
},
4361
"minimum-stability": "stable",
4462
"prefer-stable": true,

phpunit.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,15 @@
3030
<report>
3131
<html outputDirectory="coverage"/>
3232
<clover outputFile="coverage.xml"/>
33+
<text/>
3334
</report>
3435
</coverage>
3536

3637
<php>
3738
<env name="APP_ENV" value="testing"/>
3839
<env name="GROK_API_KEY" value="test-key"/>
39-
<ini name="error_reporting" value="-1"/>
40-
<ini name="display_errors" value="On"/>
41-
<ini name="display_startup_errors" value="On"/>
4240
</php>
4341

44-
<logging>
45-
<junit outputFile="junit.xml"/>
46-
<testdoxHtml outputFile="testdox.html"/>
47-
<testdoxText outputFile="testdox.txt"/>
48-
</logging>
49-
5042
<source>
5143
<include>
5244
<directory suffix=".php">src</directory>

src/Client/ClientInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* This interface ensures consistent implementation across different client versions
1717
* and provides a stable API for dependency injection.
1818
*
19-
* @package GrokPHP\Client
20-
* @author Alvin Panford <[email protected]>
19+
* @package GrokPHP\Client.
2120
* @see https://docs.x.ai/docs/api-reference
2221
*/
2322
interface ClientInterface

0 commit comments

Comments
 (0)