Skip to content

Add missing api endpoints #3

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

Merged
merged 5 commits into from
Sep 29, 2022
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
74 changes: 61 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ $result = $client->completions()->create([
echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.
```

## TODO



- [x] Models
- [x] Completions
- [x] Edits
- [x] Embeddings
- [x] Files
- [ ] FineTunes
- [ ] Moderations
- [ ] Classifications

## Usage

### `Models` Resource
Expand All @@ -69,6 +56,14 @@ Retrieves a model instance, providing basic information about the model such as
$client->models()->retrieve($model); // ['id' => 'text-davinci-002', ...]
```

#### `delete`

Delete a fine-tuned model.

```php
$client->models()->delete($model); // ['id' => 'curie:ft-acmeco-2021-03-03-21-44-20', ...]
```

### `Completions` Resource

#### `create`
Expand Down Expand Up @@ -144,6 +139,59 @@ Returns the contents of the specified file.
$client->files()->download($file); // '{"prompt": "<prompt text>", ...'
```

### `FineTunes` Resource

#### `create`

Creates a job that fine-tunes a specified model from a given dataset.

```php
$client->fineTunes()->create($parameters); // ['id' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F', ...]
```

#### `list`

List your organization's fine-tuning jobs.

```php
$client->fineTunes()->list(); // ['data' => [...], ...]
```

#### `retrieve`

Gets info about the fine-tune job.

```php
$client->fineTunes()->retrieve($fineTuneId); // ['id' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F', ...]
```

#### `cancel`

Immediately cancel a fine-tune job.

```php
$client->fineTunes()->cancel($fineTuneId); // ['id' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F', ...]
```

#### `list events`

Get fine-grained status updates for a fine-tune job.

```php
$client->fineTunes()->listEvents($fineTuneId); // ['data' => [...], ...]
```

### `Moderations` Resource

#### `create`

Classifies if text violates OpenAI's Content Policy.

```php
$client->moderations()->create($parameters); // ['id' => 'modr-5MWoLO', ...]
```


---

OpenAI PHP is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.
22 changes: 22 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use OpenAI\Resources\Edits;
use OpenAI\Resources\Embeddings;
use OpenAI\Resources\Files;
use OpenAI\Resources\FineTunes;
use OpenAI\Resources\Models;
use OpenAI\Resources\Moderations;

final class Client
{
Expand Down Expand Up @@ -71,4 +73,24 @@ public function models(): Models
{
return new Models($this->transporter);
}

/**
* Manage fine-tuning jobs to tailor a model to your specific training data.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes
*/
public function fineTunes(): FineTunes
{
return new FineTunes($this->transporter);
}

/**
* Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
*
* @see https://beta.openai.com/docs/api-reference/moderations
*/
public function moderations(): Moderations
{
return new Moderations($this->transporter);
}
}
94 changes: 94 additions & 0 deletions src/Resources/FineTunes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace OpenAI\Resources;

use OpenAI\ValueObjects\Transporter\Payload;

final class FineTunes
{
use Concerns\Transportable;

/**
* Creates a job that fine-tunes a specified model from a given dataset.
*
* Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes/create
*
* @param array<string, mixed> $parameters
* @return array<string, mixed>
*/
public function create(array $parameters): array
{
$payload = Payload::create('fine-tunes', $parameters);

/** @var array<string, mixed> $result */
$result = $this->transporter->requestObject($payload);

return $result;
}

/**
* List your organization's fine-tuning jobs.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes/list
*
* @return array<string, array<int, array<string, mixed>>>
*/
public function list(): array
{
$payload = Payload::list('fine-tunes');

/** @var array<string, array<int, array<string, mixed>>> $result */
$result = $this->transporter->requestObject($payload);

return $result;
}

/**
* Gets info about the fine-tune job.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes/list
*
* @return array<string, mixed>
*/
public function retrieve(string $fineTuneId): array
{
$payload = Payload::retrieve('fine-tunes', $fineTuneId);

return $this->transporter->requestObject($payload);
}

/**
* Immediately cancel a fine-tune job.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes/cancel
*
* @return array<string, mixed>
*/
public function cancel(string $fineTuneId): array
{
$payload = Payload::cancel('fine-tunes', $fineTuneId);

return $this->transporter->requestObject($payload);
}

/**
* Get fine-grained status updates for a fine-tune job.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes/events
*
* @return array<string, array<int, array<string, mixed>>>
*/
public function listEvents(string $fineTuneId): array
{
$payload = Payload::retrieve('fine-tunes', $fineTuneId, '/events');

/** @var array<string, array<int, array<string, mixed>>> $result */
$result = $this->transporter->requestObject($payload);

return $result;
}
}
14 changes: 14 additions & 0 deletions src/Resources/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ public function retrieve(string $model): array

return $this->transporter->requestObject($payload);
}

/**
* Delete a fine-tuned model. You must have the Owner role in your organization.
*
* @see https://beta.openai.com/docs/api-reference/fine-tunes/delete-model
*
* @return array<string, mixed>
*/
public function delete(string $model): array
{
$payload = Payload::delete('models', $model);

return $this->transporter->requestObject($payload);
}
}
30 changes: 30 additions & 0 deletions src/Resources/Moderations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace OpenAI\Resources;

use OpenAI\ValueObjects\Transporter\Payload;

final class Moderations
{
use Concerns\Transportable;

/**
* Classifies if text violates OpenAI's Content Policy.
*
* @see https://beta.openai.com/docs/api-reference/moderations/create
*
* @param array<string, mixed> $parameters
* @return array<string, mixed>
*/
public function create(array $parameters): array
{
$payload = Payload::create('moderations', $parameters);

/** @var array<string, mixed> $result */
$result = $this->transporter->requestObject($payload);

return $result;
}
}
12 changes: 10 additions & 2 deletions src/ValueObjects/ResourceUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public static function list(string $resource): self
/**
* Creates a new ResourceUri value object that retrieves the given resource.
*/
public static function retrieve(string $resource, string $id): self
public static function retrieve(string $resource, string $id, string $suffix): self
{
return new self("{$resource}/{$id}");
return new self("{$resource}/{$id}{$suffix}");
}

/**
Expand All @@ -59,6 +59,14 @@ public static function retrieveContent(string $resource, string $id): self
return new self("{$resource}/{$id}/content");
}

/**
* Creates a new ResourceUri value object that cancels the given resource.
*/
public static function cancel(string $resource, string $id): self
{
return new self("{$resource}/{$id}/cancel");
}

/**
* Creates a new ResourceUri value object that deletes the given resource.
*/
Expand Down
16 changes: 14 additions & 2 deletions src/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public static function list(string $resource): self
/**
* Creates a new Payload value object from the given parameters.
*/
public static function retrieve(string $resource, string $id): self
public static function retrieve(string $resource, string $id, string $suffix = ''): self
{
$contentType = ContentType::JSON;
$method = Method::GET;
$uri = ResourceUri::retrieve($resource, $id);
$uri = ResourceUri::retrieve($resource, $id, $suffix);

return new self($contentType, $method, $uri);
}
Expand Down Expand Up @@ -93,6 +93,18 @@ public static function upload(string $resource, array $parameters): self
return new self($contentType, $method, $uri, $parameters);
}

/**
* Creates a new Payload value object from the given parameters.
*/
public static function cancel(string $resource, string $id): self
{
$contentType = ContentType::JSON;
$method = Method::POST;
$uri = ResourceUri::cancel($resource, $id);

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

/**
* Creates a new Payload value object from the given parameters.
*/
Expand Down
50 changes: 50 additions & 0 deletions tests/Fixtures/FineTune.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @return array<string, mixed>
*/
function fineTuneResource(): array
{
return [
'id' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F',
'object' => 'fine-tune',
'model' => 'curie',
'created_at' => 1614807352,
'events' => [
fineTuneEventResource(),
fineTuneEventResource(),
],
'fine_tuned_model' => 'curie => ft-acmeco-2021-03-03-21-44-20',
'hyperparams' => [
'batch_size' => 4,
'learning_rate_multiplier' => 0.1,
'n_epochs' => 4,
'prompt_loss_weight' => 0.1,
],
'organization_id' => 'org-jwe45798ASN82s',
'result_files' => [
fileResource(),
fileResource(),
],
'status' => 'succeeded',
'validation_files' => [],
'training_files' => [
fileResource(),
fileResource(),
],
'updated_at' => 1614807865,
];
}

/**
* @return array<string, mixed>
*/
function fineTuneEventResource(): array
{
return [
'object' => 'fine-tune-event',
'created_at' => 1614807352,
'level' => 'info',
'message' => 'Job enqueued. Waiting for jobs ahead to complete. Queue number => 0.',
];
}
Loading