12
12
use OpenAI \Testing \Responses \Concerns \Fakeable ;
13
13
14
14
/**
15
- * @implements ResponseContract<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}>
15
+ * @implements ResponseContract<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>, usage?: array{total_tokens: int, input_tokens: int, output_tokens: int, input_tokens_details: array{text_tokens: int, image_tokens: int}} }>
16
16
*/
17
17
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
18
18
{
19
19
/**
20
- * @use ArrayAccessible<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}>
20
+ * @use ArrayAccessible<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>, usage?: array{total_tokens: int, input_tokens: int, output_tokens: int, input_tokens_details: array{text_tokens: int, image_tokens: int}} }>
21
21
*/
22
22
use ArrayAccessible;
23
23
@@ -31,12 +31,13 @@ private function __construct(
31
31
public readonly int $ created ,
32
32
public readonly array $ data ,
33
33
private readonly MetaInformation $ meta ,
34
+ public readonly ?ImageResponseUsage $ usage = null ,
34
35
) {}
35
36
36
37
/**
37
38
* Acts as static factory, and returns a new Response instance.
38
39
*
39
- * @param array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>} $attributes
40
+ * @param array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>, usage?: array{total_tokens: int, input_tokens: int, output_tokens: int, input_tokens_details: array{text_tokens: int, image_tokens: int}} } $attributes
40
41
*/
41
42
public static function from (array $ attributes , MetaInformation $ meta ): self
42
43
{
@@ -48,6 +49,7 @@ public static function from(array $attributes, MetaInformation $meta): self
48
49
$ attributes ['created ' ],
49
50
$ results ,
50
51
$ meta ,
52
+ isset ($ attributes ['usage ' ]) ? ImageResponseUsage::from ($ attributes ['usage ' ]) : null ,
51
53
);
52
54
}
53
55
@@ -56,12 +58,18 @@ public static function from(array $attributes, MetaInformation $meta): self
56
58
*/
57
59
public function toArray (): array
58
60
{
59
- return [
61
+ $ result = [
60
62
'created ' => $ this ->created ,
61
63
'data ' => array_map (
62
64
static fn (CreateResponseData $ result ): array => $ result ->toArray (),
63
65
$ this ->data ,
64
66
),
65
67
];
68
+
69
+ if ($ this ->usage !== null ) {
70
+ $ result ['usage ' ] = $ this ->usage ->toArray ();
71
+ }
72
+
73
+ return $ result ;
66
74
}
67
75
}
0 commit comments