@@ -14,47 +14,49 @@ Supports **PHP 8.2+**, built with **OOP best practices**, and **fully type-safe*
1414---
1515
1616## 📖 Table of Contents
17- - [ ✨ Features] ( #- features )
18- - [ 📦 Installation] ( #- installation )
19- - [ 🚀 Quick Start] ( #- quick-start )
17+ - [ Features] ( #features )
18+ - [ Installation] ( #installation )
19+ - [ Quick Start] ( #quick-start )
2020 - [ Basic Usage] ( #basic-usage )
2121 - [ Advanced Configuration] ( #advanced-configuration )
22- - [ 📌 Available Grok AI Models] ( #-available-grok-ai-models )
23- - [ ⚡ Streaming Responses] ( #-streaming-responses )
24- - [ 🧪 Testing] ( #-testing )
25- - [ 🔒 Security] ( #-security )
26- - [ 🤝 Contributing] ( #-contributing )
27- - [ 📄 License] ( #-license )
22+ - [ Available Grok AI Models] ( #available-grok-ai-models )
23+ - [ Streaming Responses] ( #streaming-responses )
24+ - [ Error Handling] ( #error-handling )
25+ - [ Testing] ( #testing )
26+ - [ Security] ( #security )
27+ - [ Contributing] ( #contributing )
28+ - [ License] ( #license )
2829
2930---
3031
31- ## ✨ Features
32+ ## ** Features**
3233
3334![ Grok PHP Client Demo] ( assets/images/demo.gif )
3435
35- ✅ ** Plug & Play ** – Quickly integrates with Grok AI APIs.
36- ✅ ** Type-Safe & Modern ** – Fully utilizes ** PHP 8.1 + features** like enums, traits, and interfaces .
37- ✅ ** Works Everywhere ** – Compatible with ** CLI scripts, APIs, and any PHP project ** .
38- ✅ ** Streaming Ready ** – Built-in support for ** streaming API responses** .
39- ✅ ** Lightweight & Optimized ** – No unnecessary dependencies, ** PSR-4 autoloading** .
36+ - ** Easy Integration ** – Seamlessly connects with Grok AI APIs.
37+ - ** Modern PHP Features ** – Utilizes PHP 8.2 + features like enums and traits .
38+ - ** Framework Agnostic ** – Works with any PHP project, CLI scripts, or web applications .
39+ - ** Streaming Support ** – Built-in support for real-time responses.
40+ - ** Lightweight & Efficient ** – Optimized with PSR-4 autoloading and minimal dependencies .
4041
4142---
4243
43- ## 📦 Installation
44+ ## ** Installation**
4445
45- Install via ** Composer** :
46+ Install via Composer:
4647``` sh
4748composer require grok-php/client
4849```
4950
50- ### Requirements:
51-
52- - PHP 8.1+
51+ ### ** Requirements:**
52+ - PHP 8.2+
5353- Composer 2.0+
5454
55- ## 🚀 Quick Start
55+ ---
56+
57+ ## ** Quick Start**
5658
57- ### Basic Usage
59+ ### ** Basic Usage**
5860
5961``` php
6062use GrokPHP\Client\Clients\GrokClient;
@@ -76,18 +78,17 @@ $messages = [
7678$options = new ChatOptions(model: Model::GROK_2, temperature: 0.7, stream: false);
7779$response = $client->chat($messages, $options);
7880
79- echo "🤖 AI Response: " . $response['choices'][0]['message']['content'];
81+ echo "AI Response: " . $response['choices'][0]['message']['content'];
8082```
8183
82- ### 📌 Defaults Used:
83-
84- - Model: grok-2
85- - Temperature: 0.7
86- - Streaming: false
84+ ### ** Defaults Used:**
85+ - Model: ` grok-2 `
86+ - Temperature: ` 0.7 `
87+ - Streaming: ` false `
8788
8889---
8990
90- ### Advanced Configuration
91+ ### ** Advanced Configuration**
9192
9293``` php
9394use GrokPHP\Client\Clients\GrokClient;
@@ -115,17 +116,19 @@ $options = new ChatOptions(
115116);
116117
117118$response = $client->chat($messages, $options);
118- echo "🚀 AI Says: " . $response['choices'][0]['message']['content'];
119+ echo "AI Says: " . $response['choices'][0]['message']['content'];
119120```
121+
120122---
121123
122- ## 📌 Available Grok AI Models
123- Grok AI offers multiple models, each optimized for different use cases.
124- These models are available in the Model enum inside our package:
124+ ## ** Available Grok AI Models**
125+
126+ Grok AI offers multiple models optimized for different use cases.
127+ These models are available in the ` Model ` enum inside our package:
125128📄 ` src/Enums/Model.php `
126129
127- | Model Enum | API Model Name | Description |
128- | -----------------------------| ----------------------| -----------------------------------------------------|
130+ | Model Enum | API Model Name | Description |
131+ | ------------------------------ | ----------------------| -----------------------------------------------------|
129132| ` Model::GROK_VISION_BETA ` | grok-vision-beta | Experimental vision-enabled model |
130133| ` Model::GROK_2_VISION ` | grok-2-vision | Advanced multi-modal vision model |
131134| ` Model::GROK_2_VISION_LATEST ` | grok-2-vision-latest | Latest iteration of Grok vision models |
@@ -135,11 +138,13 @@ These models are available in the Model enum inside our package:
135138| ` Model::GROK_2_LATEST ` | grok-2-latest | Latest iteration of Grok-2 |
136139| ` Model::GROK_BETA ` | grok-beta | Experimental beta model |
137140
138- #### 📌 Default model used: ` Model::GROK_2 `
141+ #### ** Default model used:** ` Model::GROK_2 `
142+
139143---
140144
141- ## ⚡ Streaming Responses
142- The Grok API supports streaming responses for real-time interaction.
145+ ## ** Streaming Responses**
146+
147+ The Grok API supports streaming responses for real-time interaction.
143148Enable it by setting ` stream: true ` :
144149
145150``` php
@@ -150,23 +155,73 @@ Streaming can be useful for chatbots, real-time applications, and CLI assistants
150155
151156---
152157
153- ## 🧪 Testing
154- Run tests using Pest PHP:
158+ ## ** Error Handling**
159+
160+ This package includes built-in error handling with a dedicated exception class.
161+ Common errors and their messages:
162+
163+ | Error Type | HTTP Code | Message |
164+ | --------------------| ----------| -------------------------------------------|
165+ | ` Invalid API Key ` | 400 | No API key provided. Specify your API key. |
166+ | ` Invalid Request ` | 400 | Client specified an invalid argument. |
167+ | ` Invalid Role ` | 422 | Unknown role variant provided in messages. |
168+
169+ Example of handling exceptions:
170+
171+ ``` php
172+ use GrokPHP\Client\Exceptions\GrokException;
173+
174+ try {
175+ $response = $client->chat($messages, $options);
176+ } catch (GrokException $e) {
177+ echo "Error: " . $e->getMessage();
178+ }
179+ ```
180+
181+ ---
182+ ## ** Testing**
183+
184+ To run PHPUnit tests, you need to set up your API key. Follow these steps:
185+
186+ 1 . ** Copy the default PHPUnit configuration file:**
187+ ``` sh
188+ cp phpunit.xml.dist phpunit.xml
189+ ```
190+
191+ 2 . ** Update the API key in ` phpunit.xml ` :**
192+ Open the file and replace ` your-grok-api-key-here ` with your actual API key:
193+ ``` xml
194+ <php >
195+ <env name =" GROK_API_KEY" value =" your-grok-api-key-here" />
196+ </php >
197+ ```
198+
199+ 3 . ** Obtain an API Key:**
200+ If you don’t have an API key, sign up at [ Grok AI] ( https://x.ai/api/ ) and create one.
155201
202+ 4 . ** Run the tests with PHPUnit:**
156203``` sh
157204composer test
158- or
159- vendor/bin/pest
160205```
206+ Or run PHPUnit manually:
207+ ``` sh
208+ vendor/bin/phpunit
209+ ```
210+ ---
211+
212+ ## ** Security**
161213
162- ## 🔒 Security
163- If you discover a security vulnerability, please report it via email:
164- 📩 [ thefeqy@gmail.com ] ( mailto:thefeqy@gmail.com )
214+ If you discover a security vulnerability, please report it via email:
215+ 📩 [ thefeqy@gmail.com ] ( mailto:thefeqy@gmail.com )
165216
166- ## 🤝 Contributing
217+ ---
218+
219+ ## ** Contributing**
167220
168221Want to improve this package? Check out [ CONTRIBUTING.md] ( CONTRIBUTING.md ) for contribution guidelines.
169222
170- ## 📄 License
223+ ---
224+
225+ ## ** License**
171226
172227This package is open-source software licensed under the [ MIT License] ( LICENSE ) .
0 commit comments