1. Go to this page and download the library: Download alexpago/grok-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alexpago / grok-php example snippets
$chat->query('Hello world')->run();
$chat->query('Hello. I am Lucas')
->query('Who are you?')
->run();
$chat->queryImage(
image: '<external URL or base64>',
text: 'Describe the image'
)
$generate->generate('A beautiful sunset over a calm ocean')->getImage()
$chat->query('Let\'s talk about science')
->stream(function ($chunk) { ... });
use Pago\Grok\Client\GrokChat;
$chat = new GrokChat('apikey');
$text = $chat->query('Hello! How are you?')->send();
use Pago\Grok\Client\GrokChat;
use Pago\Grok\Enums\Role;
$chat = new GrokChat('apikey');
$text = $chat
->query('Hello! I am Lucas')
->query('Hello. I am Grok. How are you?', Role::SYSTEM)
->send();
use Pago\Grok\Client\GrokChat;
use Pago\Grok\Responses\ChatErrorResponse;
use Pago\Grok\Responses\ChatResponse;
$chat = new GrokChat('apikey');
$response = $chat->query('Hello!')->run();
if ($response instanceof ChatErrorResponse) {
throw new RuntimeException(
$response->getMessage(),
$response->getCode()
);
}
// Grok's answer
$text = $response->getContent();
// Full response as array
$responseData = $response->toArray();
use Pago\Grok\Client\GrokChat;
use Pago\Grok\Enums\Model;
$chat = new GrokChat('apikey');
$response = $chat
->query('2+2')
->setTemperature(0.3) // accepts float or Temperature enum
->setModel(Model::GROK_4) // default: "grok-4"
->run();
if ($response instanceof ChatErrorResponse) {
throw new RuntimeException(
$response->getMessage(),
$response->getCode()
);
}
$text = $response->getContent();
$responseData = $response->toArray();
use Pago\Grok\Client\GrokChat;
use Pago\Grok\Enums\ImageDetail;
$chat = new GrokChat('apikey');
$chat = $chat->queryImage(
'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png',
ImageDetail::LOW,
'Describe the image'
);
$describe = $chat->send();
use Pago\Grok\Client\GrokImageGeneration;
$chat = new GrokImageGeneration('apikey');
$image = $generate->generate('A beautiful sunset over a calm ocean')->getImage();
use Pago\Grok\Client\GrokChat;
use Pago\Grok\Enums\Role;
use Psr\Http\Message\StreamInterface;
$chat = new GrokChat('apikey');
$chat
->query('Hello! I am Lucas')
->query('Hello. I am Grok. How are you?', Role::SYSTEM)
->stream(function (StreamInterface $body) {
// Read chunks from the response
$line = trim($body->read(1024));
if ($line === '' || !str_starts_with($line, 'data: ')) {
return;
}
if ($line === 'data: [DONE]') {
return;
}
// Remove prefix "data: "
$json = substr($line, 6);
// Convert to array
$chunk = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return;
}
if (!empty($chunk['choices'][0]['delta']['content'])) {
$piece = $chunk['choices'][0]['delta']['content'];
echo $piece;
ob_flush();
flush();
}
});
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.