Skip to content

Commit ddfdc72

Browse files
docs: Add stream_options.include_usage property to chat completions
1 parent e9ca288 commit ddfdc72

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,37 @@ foreach($stream as $response){
355355
// ...
356356
```
357357

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+
358389
### `Audio` Resource
359390

360391
#### `speech`

0 commit comments

Comments
 (0)