File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -355,6 +355,37 @@ foreach($stream as $response){
355
355
// ...
356
356
```
357
357
358
+ To get usage report when using stream you can use ` include_usage ` in ` stream_options ` .
359
+
360
+ ``` php
361
+ $stream = $client->chat()->createStreamed([
362
+ 'model' => 'gpt-4',
363
+ 'messages' => [
364
+ ['role' => 'user', 'content' => 'Hello!'],
365
+ ],
366
+ 'stream_options'=>[
367
+ 'include_usage'=>true
368
+ ]
369
+ ]);
370
+
371
+ foreach($stream as $response){
372
+ if($response->usage){
373
+ $response->usage->promptTokens; // 9,
374
+ $response->usage->completionTokens; // 12,
375
+ $response->usage->totalTokens; // 21
376
+ }else{
377
+ $response->choices[0]->toArray();
378
+ }
379
+
380
+ }
381
+ // 1. iteration => ['index' => 0, 'delta' => ['role' => 'assistant'], 'finish_reason' => null]
382
+ // 2. iteration => ['index' => 0, 'delta' => ['content' => 'Hello'], 'finish_reason' => null]
383
+ // 3. iteration => ['index' => 0, 'delta' => ['content' => '!'], 'finish_reason' => null]
384
+ // ...
385
+ ```
386
+
387
+ When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request.
388
+
358
389
### ` Audio ` Resource
359
390
360
391
#### ` speech `
You can’t perform that action at this time.
0 commit comments