Skip to content
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
11 changes: 11 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OpenAI\Resources\Batches;
use OpenAI\Resources\Chat;
use OpenAI\Resources\Completions;
use OpenAI\Resources\Containers;
use OpenAI\Resources\Edits;
use OpenAI\Resources\Embeddings;
use OpenAI\Resources\Files;
Expand Down Expand Up @@ -68,6 +69,16 @@ public function chat(): Chat
return new Chat($this->transporter);
}

/**
* Create and manage containers for use with the Code Interpreter tool.
*
* @see https://platform.openai.com/docs/api-reference/containers
*/
public function containers(): Containers
{
return new Containers($this->transporter);
}

/**
* Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
*
Expand Down
45 changes: 45 additions & 0 deletions src/Contracts/Resources/ContainersContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace OpenAI\Contracts\Resources;

use OpenAI\Responses\Containers\CreateContainer;
use OpenAI\Responses\Containers\DeleteContainer;
use OpenAI\Responses\Containers\ListContainers;
use OpenAI\Responses\Containers\RetrieveContainer;

interface ContainersContract
{
/**
* Creates a container for use with the Code Interpreter tool.
*
* @see https://platform.openai.com/docs/api-reference/containers/createContainers
*
* @param array<string, mixed> $parameters
*/
public function create(array $parameters): CreateContainer;

/**
* Retrieves a container with the given ID.
*
* @see https://platform.openai.com/docs/api-reference/containers/retrieveContainer
*/
public function retrieve(string $id): RetrieveContainer;

/**
* Delete a container with the given ID.
*
* @see https://platform.openai.com/docs/api-reference/containers/deleteContainer
*/
public function delete(string $id): DeleteContainer;

/**
* List containers
*
* @see https://platform.openai.com/docs/api-reference/containers/listContainers
*
* @param array<string, mixed> $parameters
*/
public function list(array $parameters = []): ListContainers;
}
88 changes: 88 additions & 0 deletions src/Resources/Containers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace OpenAI\Resources;

use OpenAI\Contracts\Resources\ContainersContract;
use OpenAI\Responses\Containers\CreateContainer;
use OpenAI\Responses\Containers\DeleteContainer;
use OpenAI\Responses\Containers\ListContainers;
use OpenAI\Responses\Containers\RetrieveContainer;
use OpenAI\ValueObjects\Transporter\Payload;
use OpenAI\ValueObjects\Transporter\Response;

/**
* @phpstan-import-type CreateContainerType from CreateContainer
* @phpstan-import-type RetrieveContainerType from RetrieveContainer
* @phpstan-import-type DeleteContainerType from DeleteContainer
* @phpstan-import-type ListContainersType from ListContainers
*/
final class Containers implements ContainersContract
{
use Concerns\Transportable;

/**
* Creates a container for use with the Code Interpreter tool.
*
* @see https://platform.openai.com/docs/api-reference/containers/createContainers
*
* @param array<string, mixed> $parameters
*/
public function create(array $parameters): CreateContainer
{
$payload = Payload::create('containers', $parameters);

/** @var Response<CreateContainerType> $response */
$response = $this->transporter->requestObject($payload);

return CreateContainer::from($response->data(), $response->meta());
}

/**
* Retrieves a container with the given ID.
*
* @see https://platform.openai.com/docs/api-reference/containers/retrieveContainer
*/
public function retrieve(string $id): RetrieveContainer
{
$payload = Payload::retrieve('containers', $id);

/** @var Response<RetrieveContainerType> $response */
$response = $this->transporter->requestObject($payload);

return RetrieveContainer::from($response->data(), $response->meta());
}

/**
* Delete a container with the given ID.
*
* @see https://platform.openai.com/docs/api-reference/containers/deleteContainer
*/
public function delete(string $id): DeleteContainer
{
$payload = Payload::delete('containers', $id);

/** @var Response<DeleteContainerType> $response */
$response = $this->transporter->requestObject($payload);

return DeleteContainer::from($response->data(), $response->meta());
}

/**
* List containers
*
* @see https://platform.openai.com/docs/api-reference/containers/listContainers
*
* @param array<string, mixed> $parameters
*/
public function list(array $parameters = []): ListContainers
{
$payload = Payload::list('containers', $parameters);

/** @var Response<ListContainersType> $response */
$response = $this->transporter->requestObject($payload);

return ListContainers::from($response->data(), $response->meta());
}
}
78 changes: 78 additions & 0 deletions src/Responses/Containers/CreateContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Containers;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Contracts\ResponseHasMetaInformationContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Concerns\HasMetaInformation;
use OpenAI\Responses\Containers\Objects\ExpiresAfter;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-import-type ExpiresAfterType from ExpiresAfter
*
* @phpstan-type CreateContainerType array{id: string, object: 'container', created_at: int, status: string, expires_after: ExpiresAfterType, last_active_at: int, name: string}
*
* @implements ResponseContract<CreateContainerType>
*/
final class CreateContainer implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<CreateContainerType>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

/**
* @param 'container' $object
*/
private function __construct(
public readonly string $id,
public readonly string $object,
public readonly int $createdAt,
public readonly string $status,
public readonly ExpiresAfter $expiresAfter,
public readonly int $lastActiveAt,
public readonly string $name,
private readonly MetaInformation $meta,
) {}

/**
* @param CreateContainerType $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
return new self(
id: $attributes['id'],
object: $attributes['object'],
createdAt: $attributes['created_at'],
status: $attributes['status'],
expiresAfter: ExpiresAfter::from($attributes['expires_after']),
lastActiveAt: $attributes['last_active_at'],
name: $attributes['name'],
meta: $meta,
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'id' => $this->id,
'object' => $this->object,
'created_at' => $this->createdAt,
'status' => $this->status,
'expires_after' => $this->expiresAfter->toArray(),
'last_active_at' => $this->lastActiveAt,
'name' => $this->name,
];
}
}
60 changes: 60 additions & 0 deletions src/Responses/Containers/DeleteContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Containers;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Contracts\ResponseHasMetaInformationContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Concerns\HasMetaInformation;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-type DeleteContainerType array{id: string, object: string, deleted: bool}
*
* @implements ResponseContract<DeleteContainerType>
*/
final class DeleteContainer implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<DeleteContainerType>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

private function __construct(
public readonly string $id,
public readonly string $object,
public readonly bool $deleted,
private readonly MetaInformation $meta,
) {}

/**
* @param DeleteContainerType $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
return new self(
id: $attributes['id'],
object: $attributes['object'],
deleted: $attributes['deleted'],
meta: $meta,
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'id' => $this->id,
'object' => $this->object,
'deleted' => $this->deleted,
];
}
}
78 changes: 78 additions & 0 deletions src/Responses/Containers/ListContainers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Containers;

use OpenAI\Contracts\ResponseContract;
use OpenAI\Contracts\ResponseHasMetaInformationContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Concerns\HasMetaInformation;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Testing\Responses\Concerns\Fakeable;

/**
* @phpstan-import-type RetrieveContainerType from RetrieveContainer
*
* @phpstan-type ListContainersType array{object: 'list', data: RetrieveContainerType[], first_id: string|null, last_id: string|null, has_more: bool}
*
* @implements ResponseContract<ListContainersType>
*/
final class ListContainers implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<ListContainersType>
*/
use ArrayAccessible;

use Fakeable;
use HasMetaInformation;

/**
* @param 'list' $object
* @param RetrieveContainer[] $data
*/
private function __construct(
public readonly string $object,
public readonly array $data,
public readonly ?string $firstId,
public readonly ?string $lastId,
public readonly bool $hasMore,
private readonly MetaInformation $meta,
) {}

/**
* @param ListContainersType $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
return new self(
object: $attributes['object'],
data: array_map(
fn (array $container): RetrieveContainer => RetrieveContainer::from($container, $meta),
$attributes['data']
),
firstId: $attributes['first_id'] ?? null,
lastId: $attributes['last_id'] ?? null,
hasMore: $attributes['has_more'],
meta: $meta,
);
}

/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'object' => $this->object,
'data' => array_map(
fn (RetrieveContainer $container): array => $container->toArray(),
$this->data
),
'first_id' => $this->firstId,
'last_id' => $this->lastId,
'has_more' => $this->hasMore,
];
}
}
Loading