Skip to content
Merged
213 changes: 174 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ If you or your business relies on this package, it's important to support the de
- [Usage](#usage)
- [Models Resource](#models-resource)
- [Responses Resource](#responses-resource)
- [Containers Resource](#containers-resource)
- [Containers Files Resource](#container-files-resource)
- [Chat Resource](#chat-resource)
- [Audio Resource](#audio-resource)
- [Embeddings Resource](#embeddings-resource)
Expand Down Expand Up @@ -76,14 +78,12 @@ Then, interact with OpenAI's API:
$yourApiKey = getenv('YOUR_API_KEY');
$client = OpenAI::client($yourApiKey);

$result = $client->chat()->create([
$response = $client->responses()->create([
'model' => 'gpt-4o',
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
'input' => 'Hello!',
]);

echo $result->choices[0]->message->content; // Hello! How can I assist you today?
echo $response->outputText; // Hello! How can I assist you today?
```

If necessary, it is possible to configure and create a separate client.
Expand Down Expand Up @@ -187,6 +187,7 @@ $response->object; // 'response'
$response->createdAt; // 1741476542
$response->status; // 'completed'
$response->model; // 'gpt-4o-mini'
$response->outputText; // 'The combined response text of any `output_text` content.'

foreach ($response->output as $output) {
$output->type; // 'message'
Expand Down Expand Up @@ -308,58 +309,192 @@ $response->hasMore; // false
$response->toArray(); // ['object' => 'list', 'data' => [...], ...]
```

### `Completions` Resource
### `Containers` Resource

#### `create`

Creates a completion for the provided prompt and parameters.
Creates a container for use with the Code Interpreter tool.

```php
$response = $client->completions()->create([
'model' => 'gpt-3.5-turbo-instruct',
'prompt' => 'Say this is a test',
'max_tokens' => 6,
'temperature' => 0
$response = $client->containers()->create([
'name' => 'My Container',
'expires_after' => [
'anchor' => 'last_active_at',
'minutes' => 60,
],
]);

$response->id; // 'cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7'
$response->object; // 'text_completion'
$response->created; // 1589478378
$response->model; // 'gpt-3.5-turbo-instruct'
$response->id; // 'container_abc123'
$response->object; // 'container'
$response->createdAt; // 1690000000
$response->status; // 'active'
$response->expiresAfter->anchor; // 'last_active_at'
$response->expiresAfter->minutes; // 60
$response->lastActiveAt; // 1690001000
$response->name; // 'My Container'

foreach ($response->choices as $choice) {
$choice->text; // '\n\nThis is a test'
$choice->index; // 0
$choice->logprobs; // null
$choice->finishReason; // 'length' or null
$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', ...]
```

#### `list`

Returns a list of containers.

```php
$response = $client->containers()->list([
'limit' => 10,
'order' => 'desc',
]);

$response->object; // 'list'

foreach ($response->data as $container) {
$container->id; // 'container_abc123'
$container->object; // 'container'
$container->createdAt; // 1690000000
$container->status; // 'active'
$container->expiresAfter->anchor; // 'last_active_at'
$container->expiresAfter->minutes; // 60
$container->lastActiveAt; // 1690001000
$container->name; // 'Test Container'
}

$response->usage->promptTokens; // 5,
$response->usage->completionTokens; // 6,
$response->usage->totalTokens; // 11
$response->firstId; // 'container_abc123'
$response->lastId; // 'container_def456'
$response->hasMore; // false

$response->toArray(); // ['id' => 'cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7', ...]
$response->toArray(); // ['object' => 'list', 'data' => [...], ...]
```

#### `create streamed`
#### `retrieve`

Creates a streamed completion for the provided prompt and parameters.
Retrieves a container with the given ID.

```php
$stream = $client->completions()->createStreamed([
'model' => 'gpt-3.5-turbo-instruct',
'prompt' => 'Hi',
'max_tokens' => 10,
]);
$response = $client->containers()->retrieve('container_abc123');

foreach($stream as $response){
$response->choices[0]->text;
$response->id; // 'container_abc123'
$response->object; // 'container'
$response->createdAt; // 1690000000
$response->status; // 'active'
$response->expiresAfter->anchor; // 'last_active_at'
$response->expiresAfter->minutes; // 60
$response->lastActiveAt; // 1690001000
$response->name; // 'Test Container'

$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', ...]
```

#### `delete`

Delete a container with the given ID.

```php
$response = $client->containers()->delete('container_abc123');

$response->id; // 'container_abc123'
$response->object; // 'container'
$response->deleted; // true

$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', 'deleted' => true]
```

### `Containers Files` Resource

#### `create`

Create or upload a file into a container.

```php
$response = $client->containers()->files()->create('container_abc123', [
'file' => fopen('path/to/local-file.txt', 'r'),
]);
$response = $client->containers()->files()->create('container_abc123', [
'file_id' => 'file_XjGxS3KTG0uNmNOK362iJua3',
]);

$response->id; // 'cfile_682e0e8a43c88191a7978f477a09bdf5'
$response->object; // 'container.file'
$response->createdAt; // 1747848842
$response->bytes; // 880
$response->containerId; // 'container_abc123'
$response->path; // '/mnt/data/local-file.txt'
$response->source; // 'user'

$response->toArray(); // ['id' => 'cfile_...', 'object' => 'container.file', ...]
```

> [!NOTE]
> You must provide either `file` or `file_id`, but not both.

#### `list`

Returns a list of files in the container.

```php
$response = $client->containers()->files()->list('container_abc123', [
'limit' => 10,
'order' => 'desc',
]);

$response->object; // 'list'

foreach ($response->data as $file) {
$file->id; // 'cfile_682e0e8a43c88191a7978f477a09bdf5'
$file->object; // 'container.file'
$file->createdAt; // 1747848842
$file->bytes; // 880
$file->containerId; // 'container_abc123'
$file->path; // '/mnt/data/...'
$file->source; // 'user'
}
// 1. iteration => 'I'
// 2. iteration => ' am'
// 3. iteration => ' very'
// 4. iteration => ' excited'
// ...

$response->firstId; // 'cfile_...'
$response->lastId; // 'cfile_...'
$response->hasMore; // false

$response->toArray(); // ['object' => 'list', 'data' => [...], ...]
```

#### `retrieve`

Retrieve information about a container file.

```php
$response = $client->containers()->files()->retrieve('container_abc123', 'cfile_682e0e8a43c88191a7978f477a09bdf5');

$response->id; // 'cfile_682e0e8a43c88191a7978f477a09bdf5'
$response->object; // 'container.file'
$response->createdAt; // 1747848842
$response->bytes; // 880
$response->containerId; // 'container_abc123'
$response->path; // '/mnt/data/...'
$response->source; // 'user'

$response->toArray(); // ['id' => 'cfile_...', 'object' => 'container.file', ...]
```

#### `retrieve content`

Returns the raw content of the specified container file.

```php
$content = $client->containers()->files()->content('container_abc123', 'cfile_682e0e8a43c88191a7978f477a09bdf5');
// $content => string
```

#### `delete`

Delete a container file.

```php
$response = $client->containers()->files()->delete('container_abc123', 'cfile_682e0e8a43c88191a7978f477a09bdf5');

$response->id; // 'cfile_682e0e8a43c88191a7978f477a09bdf5'
$response->object; // 'container.file.deleted'
$response->deleted; // true

$response->toArray(); // ['id' => 'cfile_...', 'object' => 'container.file.deleted', 'deleted' => true]
```

### `Chat` Resource
Expand Down
49 changes: 49 additions & 0 deletions src/Contracts/Resources/ContainerFileContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace OpenAI\Contracts\Resources;

use OpenAI\Responses\Containers\Files\ContainerFileDeleteResponse;
use OpenAI\Responses\Containers\Files\ContainerFileListResponse;
use OpenAI\Responses\Containers\Files\ContainerFileResponse;

interface ContainerFileContract
{
/**
* Create a container file
*
* @see https://platform.openai.com/docs/api-reference/container_files/createContainerFile
*
* @param array<string, mixed> $parameters
*/
public function create(string $containerId, array $parameters = []): ContainerFileResponse;

/**
* List container files
*
* @see https://platform.openai.com/docs/api-reference/container_files/listContainerFiles
*
* @param array<string, mixed> $parameters
*/
public function list(string $containerId, array $parameters = []): ContainerFileListResponse;

/**
* Retrieve a container file
*
* @see https://platform.openai.com/docs/api-reference/container_files/retrieveContainerFile
*/
public function retrieve(string $containerId, string $fileId): ContainerFileResponse;

/**
* Retrieve container file content
*
* @see https://platform.openai.com/docs/api-reference/container_files/retrieveContainerFileContent
*/
public function content(string $containerId, string $fileId): string;

/**
* Delete a container file
*
* @see https://platform.openai.com/docs/api-reference/container_files/deleteContainerFile
*/
public function delete(string $containerId, string $fileId): ContainerFileDeleteResponse;
}
7 changes: 7 additions & 0 deletions src/Contracts/Resources/ContainersContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ public function delete(string $id): DeleteContainer;
* @param array<string, mixed> $parameters
*/
public function list(array $parameters = []): ListContainers;

/**
* Manage the files related to the container
*
* @see https://platform.openai.com/docs/api-reference/container-files
*/
public function files(): ContainerFileContract;
}
Loading