PHP code example of grok-php / laravel

1. Go to this page and download the library: Download grok-php/laravel 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/ */

    

grok-php / laravel example snippets


use GrokPHP\Laravel\Facades\GrokAI;
use GrokPHP\Client\Config\ChatOptions;
use GrokPHP\Client\Enums\Model;

$response = GrokAI::chat(
    [['role' => 'user', 'content' => 'Hello Grok!']],
    new ChatOptions(model: Model::GROK_2)
);

echo $response['choices'][0]['message']['content'];

return [
    'api_key' => env('GROK_API_KEY'),
    'base_uri' => env('GROK_BASE_URI', 'https://api.grok.com/v1'),
    'default_model' => env('GROK_DEFAULT_MODEL', 'grok-2'),
    'default_temperature' => env('GROK_DEFAULT_TEMPERATURE', 0.7),
    'enable_streaming' => env('GROK_ENABLE_STREAMING', false),
    'timeout' => env('GROK_API_TIMEOUT', 30),
];

$response = GrokAI::chat(
    [['role' => 'user', 'content' => 'Explain black holes']],
    new ChatOptions(model: Model::GROK_2_LATEST, temperature: 1.2, stream: true)
);

$response = GrokAI::chat(
    [['role' => 'user', 'content' => 'Tell me a story']],
    new ChatOptions(model: Model::GROK_2, stream: true)
);
sh
php artisan grok:install