8000 fix(OpenAI): add support for reasoningEffort to Assistants (#606) · openai-php/client@d9901e8 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit d9901e8

Browse files
authored
fix(OpenAI): add support for reasoningEffort to Assistants (#606)
1 parent bc4cd93 commit d9901e8

File tree

5 files changed

+79
-8
lines changed

5 files changed

+79
-8
lines changed

src/Resources/Assistants.php

Lines changed: 4 additions & 4 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function create(array $parameters): AssistantResponse
2626
{
2727
$payload = Payload::create('assistants', $parameters);
2828

29-
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
29+
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
3030
$response = $this->transporter->requestObject($payload);
3131

3232
return AssistantResponse::from($response->data(), $response->meta());
@@ -41,7 +41,7 @@ public function retrieve(string $id): AssistantResponse
4141
{
4242
$payload = Payload::retrieve('assistants', $id);
4343

44-
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
44+
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
4545
$response = $this->transporter->requestObject($payload);
4646

4747
return AssistantResponse::from($response->data(), $response->meta());
@@ -58,7 +58,7 @@ public function modify(string $id, array $parameters): AssistantResponse
5858
{
5959
$payload = Payload::modify('assistants', $id, $parameters);
6060

61-
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
61+
/** @var Response<array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}> $response */
6262
$response = $this->transporter->requestObject($payload);
6363

6464
return AssistantResponse::from($response->data(), $response->meta());
@@ -90,7 +90,7 @@ public function list(array $parameters = []): AssistantListResponse
9090
{
9191
$payload = Payload::list('assistants', $parameters);
9292

93-
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
93+
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: string, name: string, parameters: array<string, mixed>}}>, tool_resources: array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}}>, first_id: ?string, last_id: ?string, has_more: bool}> $response */
9494
$response = $this->transporter->requestObject($payload);
9595

9696
return AssistantListResponse::from($response->data(), $response->meta());

src/Responses/Assistants/AssistantResponse.php

Lines changed: 14 additions & 4 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, name: ?string, description: ?string, 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>}}>, tool_resources: ?array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: string}}>
15+
* @implements ResponseContract<array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, 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>}}>, tool_resources: ?array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: string}}>
1616
*/
1717
final class AssistantResponse implements ResponseContract, ResponseHasMetaInformationContract
1818
{
1919
/**
20-
* @use ArrayAccessible<array{id: string, object: string, created_at: int, name: ?string, description: ?string, 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>}}>, tool_resources: ?array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: string}}>
20+
* @use ArrayAccessible<array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, 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>}}>, tool_resources: ?array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: string}}>
2121
*/
2222
use ArrayAccessible;
2323

@@ -33,6 +33,7 @@ private function __construct(
3333
public string $object,
3434
public int $createdAt,
3535
public ?string $name,
36+
public ?string $reasoningEffort,
3637
public ?string $description,
3738
public string $model,
3839
public ?string $instructions,
@@ -48,7 +49,7 @@ private function __construct(
4849
/**
4950
* Acts as static factory, and returns a new Response instance.
5051
*
51-
* @param array{id: string, object: string, created_at: int, name: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: ?string, name: string, parameters: array<string, mixed>}}>, tool_resources: ?array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}} $attributes
52+
* @param array{id: string, object: string, created_at: int, name: ?string, reasoning_effort?: ?string, description: ?string, model: string, instructions: ?string, tools: array<int, array{type: 'code_interpreter'}|array{type: 'file_search'}|array{type: 'function', function: array{description: ?string, name: string, parameters: array<string, mixed>}}>, tool_resources: ?array{code_interpreter?: array{file_ids: array<int,string>}, file_search?: array{vector_store_ids: array<int,string>}}, metadata: array<string, string>, temperature: ?float, top_p: ?float, response_format: string|array{type: 'text'|'json_object'}} $attributes
5253
*/
5354
public static function from(array $attributes, MetaInformation $meta): self
5455
{
@@ -70,6 +71,7 @@ public static function from(array $attributes, MetaInformation $meta): self
7071
$attributes['object'],
7172
$attributes['created_at'],
7273
$attributes['name'],
74+
$attributes['reasoning_effort'] ?? null,
7375
$attributes['description'],
7476
$attributes['model'],
7577
$attributes['instructions'],
@@ -88,11 +90,12 @@ public static function from(array $attributes, MetaInformation $meta): self
8890
*/
8991
public function toArray(): array
9092
{
91-
return [
93+
$response = [
9294
'id' => $this->id,
9395
'object' => $this->object,
9496
'created_at' => $this->createdAt,
9597
'name' => $this->name,
98+
'reasoning_effort' => $this->reasoningEffort,
9699
'description' => $this->description,
97100
'model' => $this->model,
98101
'instructions' => $this->instructions,
@@ -103,5 +106,12 @@ public function toArray(): array
103106
'top_p' => $this->topP,
104107
'response_format' => is_string($this->responseFormat) ? $this->responseFormat : $this->responseFormat->toArray(),
105108
];
109+
110+
// Only reasoning models will have this property.
111+
if (! $this->reasoningEffort) {
112+
unset($response['reasoning_effort']);
113+
}
114+
115+
return $response;
106116
}
107117
}

tests/Fixtures/Assistant.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ function assistantResource(): array
2626
];
2727
}
2828

29+
/**
30+
* @return array<string, mixed>
31+
*/
32+
function assistantReasoningModelResource(): array
33+
{
34+
return [
35+
'id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
36+
'object' => 'assistant',
37+
'created_at' => 1699619403,
38+
'name' => 'Math Tutor',
39+
'reasoning_effort' => 'high',
40+
'description' => null,
41+
'model' => 'gpt-o3',
42+
'instructions' => 'You are a personal math tutor.',
43+
'tools' => [
44+
[
45+
'type' => 'code_interpreter',
46+
],
47+
],
48+
'tool_resources' => null,
49+
'metadata' => [],
50+
'temperature' => 0.7,
51+
'top_p' => 1.0,
52+
'response_format' => 'text',
53+
];
54+
}
55+
2956
/**
3057
* @return array<string, mixed>
3158
*/
@@ -217,6 +244,23 @@ function assistantListResource(): array
217244
];
218245
}
219246

247+
/**
248+
* @return array<string, mixed>
249+
*/
250+
function assistantReasoningListResource(): array
251+
{
252+
return [
253+
'object' => 'list',
254+
'data' => [
255+
assistantReasoningModelResource(),
256+
assistantReasoningModelResource(),
257+
],
258+
'first_id' => 'asst_SMzoVX8XmCZEg1EbMHoAm8tc',
259+
'last_id' => 'asst_y49lAdZDiaQUxEBR6zrG846Q',
260+
'has_more' => true,
261+
];
262+
}
263+
220264
/**
221265
* @return array<string, mixed>
222266
*/

tests/Responses/Assistants/AssistantListResponse.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
->meta()->toBeInstanceOf(MetaInformation::class);
1616
});
1717

18+
test('from reasoning models', function () {
19+
$response = AssistantListResponse::from(assistantReasoningListResource(), meta());
20+
21+
expect($response)
22+
->toBeInstanceOf(AssistantListResponse::class)
23+
->data->each->toBeInstanceOf(AssistantResponse::class);
24+
});
25+
1826
test('as array accessible', function () {
1927
$response = AssistantListResponse::from(assistantListResource(), meta());
2028

tests/Responses/Assistants/AssistantResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
->object->toBe('assistant')
1616
->createdAt->toBe(1699619403)
1717
->name->toBe('Math Tutor')
18+
->reasoningEffort->toBeNull()
1819
->description->toBeNull()
1920
->model->toBe('gpt-4')
2021
->instructions->toBe('You are a personal math tutor.')
@@ -28,6 +29,14 @@
2829
->responseFormat->toBe('text');
2930
});
3031

32+
test('from reasoning model', function () {
33+
$result = AssistantResponse::from(assistantReasoningModelResource(), meta());
34+
35+
expect($result)
36+
->id->toBe('asst_SMzoVX8XmCZEg1EbMHoAm8tc')
37+
->reasoningEffort->toBe('high');
38+
});
39+
3140
test('with file search', function () {
3241
$result = AssistantResponse::from(assistantWithToolResources(), meta());
3342

0 commit comments

Comments
 (0)
0