-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.php
More file actions
102 lines (82 loc) · 2.49 KB
/
Copy pathstreaming.php
File metadata and controls
102 lines (82 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* This file is part of the xAI PHP SDK.
*
* (c) 2026 Displace Technologies, LLC
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* This work was inspired by X.AI LLC's Python SDK.
*
* Streaming Example
* =================
*
* This example demonstrates streaming responses from the xAI API.
* Streaming allows you to receive tokens as they're generated,
* providing a more responsive user experience.
*
* Usage:
* php examples/streaming.php
*
* Requirements:
* - Set the XAI_API_KEY environment variable
*/
declare(strict_types=1);
require_once dirname(__DIR__) . '/vendor/autoload.php';
use Displace\XaiSdk\XaiClient;
use function Displace\XaiSdk\Chat\system;
use function Displace\XaiSdk\Chat\user;
// Create the client
try {
$client = new XaiClient();
} catch (RuntimeException $e) {
echo "Error: {$e->getMessage()}\n";
echo "Please set the XAI_API_KEY environment variable.\n";
exit(1);
}
echo "xAI Streaming Example\n";
echo "=====================\n\n";
// Create a chat session
$chat = $client->chat->create(
model: 'grok-3',
messages: [
system('You are a helpful assistant. Give detailed explanations.'),
],
);
// Add a user message that will generate a longer response
$chat->append(user('Explain how photosynthesis works in about 100 words.'));
echo "Streaming response:\n\n";
// Stream the response
$startTime = microtime(true);
$tokenCount = 0;
foreach ($chat->stream() as [$response, $chunk]) {
// Print each token as it arrives
echo $chunk->content;
$tokenCount++;
// Show progress indicator
if ($chunk->finishReason !== null) {
$duration = microtime(true) - $startTime;
echo "\n\n";
echo "---\n";
echo "Finish reason: {$chunk->finishReason}\n";
echo sprintf("Duration: %.2f seconds\n", $duration);
echo "Tokens: {$response->usage->completionTokens}\n";
}
}
echo "\n";
// Continue the conversation
echo "Continuing conversation...\n\n";
$chat->append($response);
$chat->append(user('Now explain cellular respiration in the same detail.'));
echo "Streaming second response:\n\n";
foreach ($chat->stream() as [$response, $chunk]) {
echo $chunk->content;
if ($chunk->finishReason !== null) {
echo "\n\n";
echo "---\n";
echo "Finish reason: {$chunk->finishReason}\n";
echo "Total tokens used: {$response->usage->totalTokens}\n";
}
}
echo "\nDone!\n";