1. Go to this page and download the library: Download alvincoded/grok-php-client 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/ */
alvincoded / grok-php-client example snippets
use GrokPHP\Client\GrokClient;
use GrokPHP\Params;
$client = new GrokClient($apiKey);
// Simple chat
$response = $client->chat()->generate("Tell me a joke about AI");
echo $response->getContent();
// With system message
$response = $client->chat()->generate(
"What's the best programming language?",
Params::create()
->systemMessage('You are an experienced programmer.')
->temperature(0.7)
);
// Streaming response
$client->chat()->streamChat(
'Tell me something about Grok PHP',
function (ChatMessage $chunk) {
echo $chunk->text();
}
);
// Multi-turn conversation
$chat = $client->beginConvo();
$response = $chat->send('What is machine learning?');
echo $response->text();
$response = $chat->send('Give me an example');
echo $response->text();
use GrokPHP\Client\GrokClient;
use GrokPHP\Params;
$client = new GrokClient($apiKey);
// Basic completion
$response = $client->completions()->create(
"The future of AI will",
Params::create()->maxTokens(100)->temperature(0.7)
);
// Multiple completions
$responses = $client->completions()->createMultiple(
"Write a creative title for a sci-fi novel",
3,
Params::create()->temperature(1.0)
);
// Get token count
$tokenCount = $client->completions()->getTokenCount("Sample text");
use GrokPHP\Client\GrokClient;
use GrokPHP\Params;
$client = new GrokClient($apiKey);
// Basic image analysis
$response = $client->images()->analyze('https://picsum.photos/200/300');
// Detailed analysis with prompt
$response = $client->images()->analyze(
'https://picsum.photos/200/300',
'What objects can you identify in this image?',
Params::create()->maxTokens(300)->temperature(0.8)
);
// Check image content
$containsPeople = $response->containsContent('person');
use GrokPHP\Client\GrokClient;
$client = new GrokClient($apiKey);
$embeddingResponse = $client->embeddings()->create('Hello, world!');
$embeddings = $embeddingResponse->getEmbeddings();
use GrokPHP\Client\GrokClient;
use GrokPHP\Enums\Model;
$client = new GrokClient($apiKey);
// Simple chat (with model specification)
$response = $client->model(Model::GROK_2_1212)->generate('Tell me a joke about AI');
echo $response->text();
// Get model capabilities
$model = Model::GROK_2_1212
$config = $client->getConfig();
echo $config->getModelMaxTokens($model) // 32,768
echo $config->modelSupportsStreaming($model) // true
echo $config->modelSupportsFunctions($model) // false
use GrokPHP\Client\GrokClient;
use GrokPHP\Enums\Model;
// Scenario example: A university library needs to process 50,000 research papers into their new digital repository.
// Each entry " => "string"]],
"publication_year" => ["type" => "integer"],
"doi" => ["type" => "string"],
"keywords" => ["type" => "array", "items" => ["type" => "string"]],
"citation_count" => ["type" => "integer"]
],
" keywords: $metadata['keywords'] ?? []
);
}
// Define your schema as a PHP class
class ResearchPaper extends \GrokPHP\Utils\DataModel
{
#[SchemaProperty(type: 'string', description: 'Paper title')]
public string $title;
#[SchemaProperty(type: 'array', description: 'List of authors')]
public array $authors;
#[SchemaProperty(type: 'integer', description: 'Year of publication',
use GrokPHP\Enums\Model;
use GrokPHP\Facades\Grok;
use GrokPHP\Client\GrokClient;
use GrokPHP\Params;
public function __construct(
private GrokClient $grok
) {}
public function analyzeImage(): Response
{
return $this->grok->model(Model::GROK_2_VISION_1212)->images()->analyze('https://picsum.photos/200/300.jpg');
}
// Using the facade
public function ask(): Response
{
$prompt = "Do you know the muffin man?";
$params = Params::create()->maxTokens(300)->temperature(0.8);
return Grok::model(Model::GROK_2_1212)->chat()->generate($prompt, $params);
}
$response->getContent(); // Get response content
$response->getRole(); // Get message role
$response->getFinishReason(); // Get completion finish reason
$response->getId(); // Get response ID
$response->getModel(); // Get model used
$response->getUsage(); // Get token usage statistics
$response->getAnalysis(); // Get analysis text
$response->getImageUrl(); // Get analyzed image URL
$response->getMetadata(); // Get image metadata
$response->getUsage(); // Get token usage
$response->getEmbeddings(); // Get embeddings
$response->getUsage(); // Get token usage