Skip to content

Commit

Permalink
Adjust model response object to the latest API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Nov 6, 2023
1 parent 106a6a1 commit b693eca
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 208 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,6 @@ $response->id; // 'gpt-3.5-turbo-instruct'
$response->object; // 'model'
$response->created; // 1642018370
$response->ownedBy; // 'openai'
$response->root; // 'gpt-3.5-turbo-instruct'
$response->parent; // null

foreach ($response->permission as $result) {
$result->id; // 'modelperm-7E53j9OtnMZggjqlwMxW4QG7'
$result->object; // 'model_permission'
$result->created; // 1664307523
$result->allowCreateEngine; // false
$result->allowSampling; // true
$result->allowLogprobs; // true
$result->allowSearchIndices; // false
$result->allowView; // true
$result->allowFineTuning; // false
$result->organization; // '*'
$result->group; // null
$result->isBlocking; // false
}

$response->toArray(); // ['id' => 'gpt-3.5-turbo-instruct', ...]
```
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function list(): ListResponse
{
$payload = Payload::list('models');

/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}>}> $response */
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string}>}> $response */
$response = $this->transporter->requestObject($payload);

return ListResponse::from($response->data(), $response->meta());
Expand All @@ -39,7 +39,7 @@ public function retrieve(string $model): RetrieveResponse
{
$payload = Payload::retrieve('models', $model);

/** @var Response<array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}> $response */
/** @var Response<array{id: string, object: string, created: int, owned_by: string}> $response */
$response = $this->transporter->requestObject($payload);

return RetrieveResponse::from($response->data(), $response->meta());
Expand Down
6 changes: 3 additions & 3 deletions src/Responses/Models/ListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @implements ResponseContract<array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}>}>
* @implements ResponseContract<array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string}>}>
*/
final class ListResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}>}>
* @use ArrayAccessible<array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string}>}>
*/
use ArrayAccessible;

Expand All @@ -37,7 +37,7 @@ private function __construct(
/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}>} $attributes
* @param array{object: string, data: array<int, array{id: string, object: string, created: int, owned_by: string}>} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand Down
25 changes: 3 additions & 22 deletions src/Responses/Models/RetrieveResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,39 @@
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @implements ResponseContract<array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}>
* @implements ResponseContract<array{id: string, object: string, created: int, owned_by: string}>
*/
final class RetrieveResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string}>
* @use ArrayAccessible<array{id: string, object: string, created: int, owned_by: string}>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

/**
* @param array<int, RetrieveResponsePermission> $permission
*/
private function __construct(
public readonly string $id,
public readonly string $object,
public readonly int $created,
public readonly string $ownedBy,
public readonly array $permission,
public readonly string $root,
public readonly ?string $parent,
private readonly MetaInformation $meta,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, object: string, created: int, owned_by: string, permission: array<int, array{id: string, object: string, created: int, allow_create_engine: bool, allow_sampling: bool, allow_logprobs: bool, allow_search_indices: bool, allow_view: bool, allow_fine_tuning: bool, organization: string, group: ?string, is_blocking: bool}>, root: string, parent: ?string} $attributes
* @param array{id: string, object: string, created: int, owned_by: string} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
$permission = array_map(fn (array $result): RetrieveResponsePermission => RetrieveResponsePermission::from(
$result
), $attributes['permission']);

return new self(
$attributes['id'],
$attributes['object'],
$attributes['created'],
$attributes['owned_by'],
$permission,
$attributes['root'],
$attributes['parent'],
$meta,
);
}
Expand All @@ -72,12 +59,6 @@ public function toArray(): array
'object' => $this->object,
'created' => $this->created,
'owned_by' => $this->ownedBy,
'permission' => array_map(
static fn (RetrieveResponsePermission $result): array => $result->toArray(),
$this->permission,
),
'root' => $this->root,
'parent' => $this->parent,
];
}
}
66 changes: 0 additions & 66 deletions src/Responses/Models/RetrieveResponsePermission.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Testing/Responses/Fixtures/Models/RetrieveResponseFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,5 @@ final class RetrieveResponseFixture
'object' => 'model',
'created' => 1_642_018_370,
'owned_by' => 'openai',
'permission' => [
[
'id' => 'snapperm-7oP3WFr9x7qf5xb3eZrVABAH',
'object' => 'model_permission',
'created' => 1_642_018_480,
'allow_create_engine' => false,
'allow_sampling' => true,
'allow_logprobs' => true,
'allow_search_indices' => false,
'allow_view' => true,
'allow_fine_tuning' => false,
'organization' => '*',
'group' => null,
'is_blocking' => false,
],
],
'root' => 'text-babbage:001',
'parent' => null,
];
}
18 changes: 0 additions & 18 deletions tests/Fixtures/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ function model(): array
'object' => 'model',
'created' => 1642018370,
'owned_by' => 'openai',
'permission' => [
[
'id' => 'snapperm-7oP3WFr9x7qf5xb3eZrVABAH',
'object' => 'model_permission',
'created' => 1642018480,
'allow_create_engine' => false,
'allow_sampling' => true,
'allow_logprobs' => true,
'allow_search_indices' => false,
'allow_view' => true,
'allow_fine_tuning' => false,
'organization' => '*',
'group' => null,
'is_blocking' => false,
],
],
'root' => 'text-babbage:001',
'parent' => null,
];
}

Expand Down
21 changes: 1 addition & 20 deletions tests/Resources/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use OpenAI\Responses\Models\DeleteResponse;
use OpenAI\Responses\Models\ListResponse;
use OpenAI\Responses\Models\RetrieveResponse;
use OpenAI\Responses\Models\RetrieveResponsePermission;

test('list', function () {
$client = mockClient('GET', 'models', [], \OpenAI\ValueObjects\Transporter\Response::from(modelList(), metaHeaders()));
Expand Down Expand Up @@ -34,25 +33,7 @@
->id->toBe('text-babbage:001')
->object->toBe('model')
->created->toBe(1642018370)
->ownedBy->toBe('openai')
->permission->toBeArray()->toHaveCount(1)
->permission->each->toBeInstanceOf(RetrieveResponsePermission::class)
->root->toBe('text-babbage:001')
->parent->toBe(null);

expect($result->permission[0])
->id->toBe('snapperm-7oP3WFr9x7qf5xb3eZrVABAH')
->object->toBe('model_permission')
->created->toBe(1642018480)
->allowCreateEngine->toBe(false)
->allowSampling->toBe(true)
->allowLogprobs->toBe(true)
->allowSearchIndices->toBe(false)
->allowView->toBe(true)
->allowFineTuning->toBe(false)
->organization->toBe('*')
->group->toBe(null)
->isBlocking->toBe(false);
->ownedBy->toBe('openai');

expect($result->meta())
->toBeInstanceOf(MetaInformation::class);
Expand Down
17 changes: 3 additions & 14 deletions tests/Responses/Models/RetrieveResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Models\RetrieveResponse;
use OpenAI\Responses\Models\RetrieveResponsePermission;

test('from', function () {
$result = RetrieveResponse::from(model(), meta());
Expand All @@ -13,10 +12,6 @@
->object->toBe('model')
->created->toBe(1642018370)
->ownedBy->toBe('openai')
->permission->toBeArray()->toHaveCount(1)
->permission->each->toBeInstanceOf(RetrieveResponsePermission::class)
->root->toBe('text-babbage:001')
->parent->toBe(null)
->meta()->toBeInstanceOf(MetaInformation::class);
});

Expand All @@ -38,22 +33,16 @@

expect($response)
->id->toBe('text-babbage:001')
->and($response->permission[0])
->allowCreateEngine->toBeFalse();
->ownedBy->toBe('openai');
});

test('fake with override', function () {
$response = RetrieveResponse::fake([
'id' => 'text-1234',
'permission' => [
[
'allow_create_engine' => true,
],
],
'owned_by' => 'xyz-dev',
]);

expect($response)
->id->toBe('text-1234')
->and($response->permission[0])
->allowCreateEngine->toBeTrue();
->ownedBy->toBe('xyz-dev');
});
28 changes: 0 additions & 28 deletions tests/Responses/Models/RetrieveResponsePermission.php

This file was deleted.

0 comments on commit b693eca

Please sign in to comment.