9
9
use OpenAI \Testing \Responses \Concerns \FakeableForStreamedResponse ;
10
10
11
11
/**
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} }>
13
13
*/
14
14
final class CreateStreamedResponse implements ResponseContract
15
15
{
16
16
/**
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} }>
18
18
*/
19
19
use ArrayAccessible;
20
20
@@ -36,7 +36,7 @@ private function __construct(
36
36
/**
37
37
* Acts as static factory, and returns a new Response instance.
38
38
*
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
40
40
*/
41
41
public static function from (array $ attributes ): self
42
42
{
@@ -59,7 +59,7 @@ public static function from(array $attributes): self
59
59
*/
60
60
public function toArray (): array
61
61
{
62
- return [
62
+ $ data = [
63
63
'id ' => $ this ->id ,
64
64
'object ' => $ this ->object ,
65
65
'created ' => $ this ->created ,
@@ -68,7 +68,12 @@ public function toArray(): array
68
68
static fn (CreateStreamedResponseChoice $ result ): array => $ result ->toArray (),
69
69
$ this ->choices ,
70
70
),
71
- 'usage ' => $ this ->usage ?->toArray() ?? null ,
72
71
];
72
+
73
+ if ($ this ->usage instanceof \OpenAI \Responses \Chat \CreateResponseUsage) {
74
+ $ data ['usage ' ] = $ this ->usage ->toArray ();
75
+ }
76
+
77
+ return $ data ;
73
78
}
74
79
}
0 commit comments