Skip to content

Commit faddd9b

Browse files
committed
Chat stream usage: update annotations and add test
1 parent 769be96 commit faddd9b

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/Responses/Chat/CreateStreamedResponse.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
use OpenAI\Testing\Responses\Concerns\FakeableForStreamedResponse;
1010

1111
/**
12-
* @implements ResponseContract<array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, finish_reason: string|null}>}>
12+
* @implements ResponseContract<array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, finish_reason: string|null}>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
1313
*/
1414
final class CreateStreamedResponse implements ResponseContract
1515
{
1616
/**
17-
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, finish_reason: string|null}>}>
17+
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, finish_reason: string|null}>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
1818
*/
1919
use ArrayAccessible;
2020

@@ -36,7 +36,7 @@ private function __construct(
3636
/**
3737
* Acts as static factory, and returns a new Response instance.
3838
*
39-
* @param array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, delta: array{role?: string, content?: string}, finish_reason: string|null}>} $attributes
39+
* @param array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, delta: array{role?: string, content?: string}, finish_reason: string|null}>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}} $attributes
4040
*/
4141
public static function from(array $attributes): self
4242
{
@@ -59,7 +59,7 @@ public static function from(array $attributes): self
5959
*/
6060
public function toArray(): array
6161
{
62-
return [
62+
$data = [
6363
'id' => $this->id,
6464
'object' => $this->object,
6565
'created' => $this->created,
@@ -68,7 +68,12 @@ public function toArray(): array
6868
static fn (CreateStreamedResponseChoice $result): array => $result->toArray(),
6969
$this->choices,
7070
),
71-
'usage' => $this->usage?->toArray() ?? null,
7271
];
72+
73+
if ($this->usage instanceof \OpenAI\Responses\Chat\CreateResponseUsage) {
74+
$data['usage'] = $this->usage->toArray();
75+
}
76+
77+
return $data;
7378
}
7479
}

tests/Fixtures/Chat.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ function chatCompletionStreamContentChunk(): array
206206
];
207207
}
208208

209+
function chatCompletionStreamUsageChunk(): array
210+
{
211+
return [
212+
'id' => 'chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz',
213+
'object' => 'chat.completion.chunk',
214+
'created' => 1679432086,
215+
'model' => 'gpt-4-0314',
216+
'choices' => [],
217+
'usage' => [
218+
'prompt_tokens' => 9,
219+
'completion_tokens' => 12,
220+
'total_tokens' => 21,
221+
],
222+
];
223+
}
224+
209225
function chatCompletionStreamFunctionCallChunk(): array
210226
{
211227
return [

tests/Responses/Chat/CreateStreamedResponse.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use OpenAI\Responses\Chat\CreateResponseUsage;
34
use OpenAI\Responses\Chat\CreateStreamedResponse;
45
use OpenAI\Responses\Chat\CreateStreamedResponseChoice;
56

@@ -16,6 +17,22 @@
1617
->choices->each->toBeInstanceOf(CreateStreamedResponseChoice::class);
1718
});
1819

20+
test('from usage chunk', function () {
21+
$completion = CreateStreamedResponse::from(chatCompletionStreamUsageChunk());
22+
23+
expect($completion)
24+
->toBeInstanceOf(CreateStreamedResponse::class)
25+
->id->toBe('chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz')
26+
->object->toBe('chat.completion.chunk')
27+
->created->toBe(1679432086)
28+
->model->toBe('gpt-4-0314')
29+
->choices->toBeArray()->toHaveCount(0)
30+
->usage->toBeInstanceOf(CreateResponseUsage::class)
31+
->usage->promptTokens->toBe(9)
32+
->usage->completionTokens->toBe(12)
33+
->usage->totalTokens->toBe(21);
34+
});
35+
1936
test('as array accessible', function () {
2037
$completion = CreateStreamedResponse::from(chatCompletionStreamFirstChunk());
2138

0 commit comments

Comments
 (0)