Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "has_more" to the fine tuning list responses, add pagination parameters to list jobs request #206

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,15 @@ foreach ($response->data as $result) {
$response->toArray(); // ['object' => 'list', 'data' => [...]]
```

You can pass additional parameters to the `listJobs` method to narrow down the results.

```php
$response = $client->fineTuning()->listJobs([
'limit' => 3, // Number of jobs to retrieve (Default: 20)
'after' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F', // Identifier for the last job from the previous pagination request.
]);
```

#### `retrieve job`

Get info about a fine-tuning job.
Expand Down
6 changes: 4 additions & 2 deletions src/Contracts/Resources/FineTuningContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public function createJob(array $parameters): RetrieveJobResponse;
/**
* List your organization's fine-tuning jobs.
*
* @see TODO: There is no official documentation yet
* @see https://platform.openai.com/docs/api-reference/fine-tuning/undefined
*
* @param array<string, mixed> $parameters
*/
public function listJobs(): ListJobsResponse;
public function listJobs(array $parameters = []): ListJobsResponse;

/**
* Get info about a fine-tuning job.
Expand Down
14 changes: 8 additions & 6 deletions src/Resources/FineTuning.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public function createJob(array $parameters): RetrieveJobResponse
/**
* List your organization's fine-tuning jobs.
*
* @see TODO: There is no official documentation yet
* @see https://platform.openai.com/docs/api-reference/fine-tuning/undefined
*
* @param array<string, mixed> $parameters
*/
public function listJobs(): ListJobsResponse
public function listJobs(array $parameters = []): ListJobsResponse
{
$payload = Payload::list('fine_tuning/jobs');
$payload = Payload::list('fine_tuning/jobs', $parameters);

/** @var Response<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>}> $response */
/** @var Response<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);

return ListJobsResponse::from($response->data(), $response->meta());
Expand All @@ -52,7 +54,7 @@ public function listJobs(): ListJobsResponse
/**
* Gets info about the fine-tune job.
*
* @see https://platform.openai.com/docs/api-reference/fine-tunes/list
* @see https://platform.openai.com/docs/api-reference/fine-tuning/retrieve
*/
public function retrieveJob(string $jobId): RetrieveJobResponse
{
Expand Down Expand Up @@ -90,7 +92,7 @@ public function listJobEvents(string $jobId, array $parameters = []): ListJobEve
{
$payload = Payload::retrieve('fine_tuning/jobs', $jobId, '/events', $parameters);

/** @var Response<array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>}> $response */
/** @var Response<array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>, has_more: bool}> $response */
$response = $this->transporter->requestObject($payload);

return ListJobEventsResponse::from($response->data(), $response->meta());
Expand Down
9 changes: 6 additions & 3 deletions src/Responses/FineTuning/ListJobEventsResponse.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{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>}>
* @implements ResponseContract<array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>, has_more: bool}>
*/
final class ListJobEventsResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>}>
* @use ArrayAccessible<array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>, has_more: bool}>
*/
use ArrayAccessible;

Expand All @@ -30,14 +30,15 @@ final class ListJobEventsResponse implements ResponseContract, ResponseHasMetaIn
private function __construct(
public readonly string $object,
public readonly array $data,
public readonly bool $hasMore,
private readonly MetaInformation $meta,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>} $attributes
* @param array{object: string, data: array<int, array{object: string, id: string, created_at: int, level: string, message: string, data: array{step: int, train_loss: float, train_mean_token_accuracy: float}|null, type: string}>, has_more: bool} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand All @@ -48,6 +49,7 @@ public static function from(array $attributes, MetaInformation $meta): self
return new self(
$attributes['object'],
$data,
$attributes['has_more'],
$meta,
);
}
Expand All @@ -63,6 +65,7 @@ public function toArray(): array
static fn (ListJobEventsResponseEvent $response): array => $response->toArray(),
$this->data,
),
'has_more' => $this->hasMore,
];
}
}
9 changes: 6 additions & 3 deletions src/Responses/FineTuning/ListJobsResponse.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, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>}>
* @implements ResponseContract<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}>
*/
final class ListJobsResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>}>
* @use ArrayAccessible<array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}>
*/
use ArrayAccessible;

Expand All @@ -30,14 +30,15 @@ final class ListJobsResponse implements ResponseContract, ResponseHasMetaInforma
private function __construct(
public readonly string $object,
public readonly array $data,
public readonly bool $hasMore,
private readonly MetaInformation $meta,
) {
}

/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>} $attributes
* @param array{object: string, data: array<int, array{id: string, object: string, model: string, created_at: int, finished_at: ?int, fine_tuned_model: ?string, hyperparameters: array{n_epochs: int}, organization_id: string, result_files: array<int, string>, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand All @@ -49,6 +50,7 @@ public static function from(array $attributes, MetaInformation $meta): self
return new self(
$attributes['object'],
$data,
$attributes['has_more'],
$meta,
);
}
Expand All @@ -64,6 +66,7 @@ public function toArray(): array
static fn (RetrieveJobResponse $response): array => $response->toArray(),
$this->data,
),
'has_more' => $this->hasMore,
];
}
}
2 changes: 1 addition & 1 deletion src/Testing/Resources/FineTuningTestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function createJob(array $parameters): RetrieveJobResponse
return $this->record(__FUNCTION__, $parameters);
}

public function listJobs(): ListJobsResponse
public function listJobs(array $parameters = []): ListJobsResponse
{
return $this->record(__FUNCTION__);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ final class ListJobEventsResponseFixture
'type' => 'message',
],
],
'has_more' => false,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ final class ListJobsResponseFixture
'data' => [
RetrieveJobResponseFixture::ATTRIBUTES,
],
'has_more' => false,
];
}
6 changes: 4 additions & 2 deletions src/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ private function __construct(

/**
* Creates a new Payload value object from the given parameters.
*
* @param array<string, mixed> $parameters
*/
public static function list(string $resource): self
public static function list(string $resource, array $parameters = []): self
{
$contentType = ContentType::JSON;
$method = Method::GET;
$uri = ResourceUri::list($resource);

return new self($contentType, $method, $uri);
return new self($contentType, $method, $uri, $parameters);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/FineTuning.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function fineTuningJobListResource(): array
fineTuningJobRetrieveResource(),
fineTuningJobRetrieveResource(),
],
'has_more' => false,
];
}

Expand Down Expand Up @@ -111,5 +112,6 @@ function fineTuningJobListEventsResource(): array
fineTuningJobMessageEventResource(),
fineTuningJobMetricsEventResource(),
],
'has_more' => true,
];
}
11 changes: 11 additions & 0 deletions tests/Resources/FineTuning.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@
->toBeInstanceOf(MetaInformation::class);
});

test('list jobs with params', function () {
$client = mockClient('GET', 'fine_tuning/jobs', [], \OpenAI\ValueObjects\Transporter\Response::from(fineTuningJobListResource(), metaHeaders()));

$result = $client->fineTuning()->listJobs(['limit' => 3]);

expect($result)
->toBeInstanceOf(ListJobsResponse::class)
->data->toBeArray()->toHaveCount(2)
->data->each->toBeInstanceOf(RetrieveJobResponse::class);
});

test('retrieve job', function () {
$client = mockClient('GET', 'fine_tuning/jobs/ft-AF1WoRqd3aJAHsqc9NY7iL8F', [], \OpenAI\ValueObjects\Transporter\Response::from(fineTuningJobRetrieveResource(), metaHeaders()));

Expand Down
1 change: 1 addition & 0 deletions tests/Responses/FineTuning/ListJobEventsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
->object->toBe('list')
->data->toBeArray()->toHaveCount(2)
->data->each->toBeInstanceOf(ListJobEventsResponseEvent::class)
->hasMore->toBeTrue()
->meta()->toBeInstanceOf(MetaInformation::class);
});

Expand Down
1 change: 1 addition & 0 deletions tests/Responses/FineTuning/ListJobsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
->object->toBe('list')
->data->toBeArray()->toHaveCount(2)
->data->each->toBeInstanceOf(RetrieveJobResponse::class)
->hasMore->toBeFalse()
->meta()->toBeInstanceOf(MetaInformation::class);
});

Expand Down
Loading