Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 135a9b3

Browse filesBrowse files
committed
thread run response: add usage property
1 parent 081ea0b commit 135a9b3
Copy full SHA for 135a9b3

File tree

Expand file treeCollapse file tree

7 files changed

+133
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+133
-7
lines changed
Open diff view settings
Collapse file

‎src/Resources/ThreadsRuns.php‎

Copy file name to clipboardExpand all lines: src/Resources/ThreadsRuns.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function retrieve(string $threadId, string $runId): ThreadRunResponse
4141
{
4242
$payload = Payload::retrieve('threads/'.$threadId.'/runs', $runId);
4343

44-
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
44+
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
4545
$response = $this->transporter->requestObject($payload);
4646

4747
return ThreadRunResponse::from($response->data(), $response->meta());
@@ -58,7 +58,7 @@ public function modify(string $threadId, string $runId, array $parameters): Thre
5858
{
5959
$payload = Payload::modify('threads/'.$threadId.'/runs', $runId, $parameters);
6060

61-
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
61+
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
6262
$response = $this->transporter->requestObject($payload);
6363

6464
return ThreadRunResponse::from($response->data(), $response->meta());
@@ -75,7 +75,7 @@ public function submitToolOutputs(string $threadId, string $runId, array $parame
7575
{
7676
$payload = Payload::create('threads/'.$threadId.'/runs/'.$runId.'/submit_tool_outputs', $parameters);
7777

78-
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
78+
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
7979
$response = $this->transporter->requestObject($payload);
8080

8181
return ThreadRunResponse::from($response->data(), $response->meta());
@@ -90,7 +90,7 @@ public function cancel(string $threadId, string $runId): ThreadRunResponse
9090
{
9191
$payload = Payload::cancel('threads/'.$threadId.'/runs', $runId);
9292

93-
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}> $response */
93+
/** @var Response<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
9494
$response = $this->transporter->requestObject($payload);
9595

9696
return ThreadRunResponse::from($response->data(), $response->meta());
Collapse file

‎src/Responses/Threads/Runs/ThreadRunResponse.php‎

Copy file name to clipboardExpand all lines: src/Responses/Threads/Runs/ThreadRunResponse.php
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @implements ResponseContract<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: string}|array{type: string}|array{type: string, function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}>
15+
* @implements ResponseContract<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: string}|array{type: string}|array{type: string, function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
1616
*/
1717
final class ThreadRunResponse implements ResponseContract, ResponseHasMetaInformationContract
1818
{
1919
/**
20-
* @use ArrayAccessible<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: string}|array{type: string}|array{type: string, function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>}>
20+
* @use ArrayAccessible<array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: string}|array{type: string}|array{type: string, function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
2121
*/
2222
use ArrayAccessible;
2323

@@ -48,14 +48,15 @@ private function __construct(
4848
public array $tools,
4949
public array $fileIds,
5050
public array $metadata,
51+
public ?ThreadRunResponseUsage $usage,
5152
private readonly MetaInformation $meta,
5253
) {
5354
}
5455

5556
/**
5657
* Acts as static factory, and returns a new Response instance.
5758
*
58-
* @param array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>} $attributes
59+
* @param array{id: string, object: string, created_at: int, thread_id: string, assistant_id: string, status: string, required_action?: array{type: string, submit_tool_outputs: array{tool_calls: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}}, last_error: ?array{code: string, message: string}, expires_at: ?int, started_at: ?int, cancelled_at: ?int, failed_at: ?int, completed_at: ?int, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'retrieval'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, file_ids: array<int, string>, metadata: array<string, string>, usage?: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}} $attributes
5960
*/
6061
public static function from(array $attributes, MetaInformation $meta): self
6162
{
@@ -87,6 +88,7 @@ public static function from(array $attributes, MetaInformation $meta): self
8788
$tools,
8889
$attributes['file_ids'],
8990
$attributes['metadata'],
91+
isset($attributes['usage']) ? ThreadRunResponseUsage::from($attributes['usage']) : null,
9092
$meta,
9193
);
9294
}
@@ -118,12 +120,17 @@ public function toArray(): array
118120
),
119121
'file_ids' => $this->fileIds,
120122
'metadata' => $this->metadata,
123+
'usage' => $this->usage?->toArray(),
121124
];
122125

123126
if ($data['required_action'] === null) {
124127
unset($data['required_action']);
125128
}
126129

130+
if ($data['usage'] === null) {
131+
unset($data['usage']);
132+
}
133+
127134
return $data;
128135
}
129136
}
Collapse file
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Threads\Runs;
6+
7+
final class ThreadRunResponseUsage
8+
{
9+
private function __construct(
10+
public readonly int $promptTokens,
11+
public readonly ?int $completionTokens,
12+
public readonly int $totalTokens,
13+
) {
14+
}
15+
16+
/**
17+
* @param array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int} $attributes
18+
*/
19+
public static function from(array $attributes): self
20+
{
21+
return new self(
22+
$attributes['prompt_tokens'],
23+
$attributes['completion_tokens'] ?? null,
24+
$attributes['total_tokens'],
25+
);
26+
}
27+
28+
/**
29+
* @return array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}
30+
*/
31+
public function toArray(): array
32+
{
33+
return [
34+
'prompt_tokens' => $this->promptTokens,
35+
'completion_tokens' => $this->completionTokens,
36+
'total_tokens' => $this->totalTokens,
37+
];
38+
}
39+
}
Collapse file

‎src/Testing/Responses/Fixtures/Threads/Runs/ThreadRunResponseFixture.php‎

Copy file name to clipboardExpand all lines: src/Testing/Responses/Fixtures/Threads/Runs/ThreadRunResponseFixture.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ final class ThreadRunResponseFixture
2828
'file-6EsV79Y261TEmi0PY5iHbZdS',
2929
],
3030
'metadata' => [],
31+
'usage' => [
32+
'prompt_tokens' => 5,
33+
'completion_tokens' => 7,
34+
'total_tokens' => 12,
35+
],
3136
];
3237
}
Collapse file

‎tests/Fixtures/ThreadRun.php‎

Copy file name to clipboardExpand all lines: tests/Fixtures/ThreadRun.php
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,43 @@ function threadRunWithSubmitToolOutputsResource(): array
166166
];
167167
}
168168

169+
/**
170+
* @return array<string, mixed>
171+
*/
172+
function threadRunWithUsageResource(): array
173+
{
174+
return [
175+
'id' => 'run_4RCYyYzX9m41WQicoJtUQAb8',
176+
'object' => 'thread.run',
177+
'created_at' => 1699621735,
178+
'assistant_id' => 'asst_EopvUEMh90bxkNRYEYM81Orc',
179+
'thread_id' => 'thread_EKt7MjGOC6bwKWmenQv5VD6r',
180+
'status' => 'queued',
181+
'started_at' => null,
182+
'expires_at' => 1699622335,
183+
'cancelled_at' => null,
184+
'failed_at' => null,
185+
'completed_at' => null,
186+
'last_error' => null,
187+
'model' => 'gpt-4',
188+
'instructions' => 'You are a personal math tutor. When asked a question, write and run Python code to answer the question.',
189+
'tools' => [
190+
[
191+
'type' => 'code_interpreter',
192+
],
193+
],
194+
'file_ids' => [
195+
'file-6EsV79Y261TEmi0PY5iHbZdS',
196+
],
197+
'metadata' => [],
198+
'usage' => [
199+
'prompt_tokens' => 1,
200+
'completion_tokens' => 16,
201+
'total_tokens' => 17,
202+
],
203+
];
204+
}
205+
169206
/**
170207
* @return array<string, mixed>
171208
*/
Collapse file

‎tests/Responses/Threads/Runs/ThreadRunResponse.php‎

Copy file name to clipboardExpand all lines: tests/Responses/Threads/Runs/ThreadRunResponse.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use OpenAI\Responses\Threads\Runs\ThreadRunResponseToolCodeInterpreter;
66
use OpenAI\Responses\Threads\Runs\ThreadRunResponseToolFunction;
77
use OpenAI\Responses\Threads\Runs\ThreadRunResponseToolRetrieval;
8+
use OpenAI\Responses\Threads\Runs\ThreadRunResponseUsage;
89

910
test('from', function () {
1011
$result = ThreadRunResponse::from(threadRunResource(), meta());
@@ -29,6 +30,7 @@
2930
->tools->{0}->toBeInstanceOf(ThreadRunResponseToolCodeInterpreter::class)
3031
->fileIds->toBe(['file-6EsV79Y261TEmi0PY5iHbZdS'])
3132
->metadata->toBe([])
33+
->usage->toBeNull()
3234
->meta()->toBeInstanceOf(MetaInformation::class);
3335
});
3436

@@ -59,6 +61,16 @@
5961
->meta()->toBeInstanceOf(MetaInformation::class);
6062
});
6163

64+
test('from with usage', function () {
65+
$result = ThreadRunResponse::from(threadRunWithUsageResource(), meta());
66+
67+
expect($result)
68+
->usage->toBeInstanceOf(ThreadRunResponseUsage::class)
69+
->usage->promptTokens->toBe(1)
70+
->usage->completionTokens->toBe(16)
71+
->usage->totalTokens->toBe(17);
72+
});
73+
6274
test('as array accessible', function () {
6375
$result = ThreadRunResponse::from(threadRunResource(), meta());
6476

@@ -80,6 +92,13 @@
8092
->toBe(threadRunWithSubmitToolOutputsResource());
8193
});
8294

95+
test('to array with usage', function () {
96+
$result = ThreadRunResponse::from(threadRunWithUsageResource(), meta());
97+
98+
expect($result->toArray())
99+
->toBe(threadRunWithUsageResource());
100+
});
101+
83102
test('fake', function () {
84103
$response = ThreadRunResponse::fake();
85104

Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use OpenAI\Responses\Threads\Runs\ThreadRunResponseUsage;
4+
5+
test('from', function () {
6+
$result = ThreadRunResponseUsage::from(threadRunWithUsageResource()['usage']);
7+
8+
expect($result)
9+
->promptTokens->toBe(1)
10+
->completionTokens->toBe(16)
11+
->totalTokens->toBe(17);
12+
});
13+
14+
test('to array', function () {
15+
$result = ThreadRunResponseUsage::from(completion()['usage']);
16+
17+
expect($result->toArray())
18+
->toBe(completion()['usage']);
19+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.