From 69bea8e310bade81beec931cfde0be23a6373d18 Mon Sep 17 00:00:00 2001 From: Gianluca Fabrizi Date: Thu, 19 Oct 2023 12:29:23 +0200 Subject: [PATCH 1/4] Downgraded to PHP72 --- rector.php | 20 +-- src/Client.php | 30 ++-- src/Contracts/MetaInformationContract.php | 20 ++- src/Contracts/ResponseContract.php | 20 ++- src/Enums/FineTuning/FineTuningEventLevel.php | 8 +- src/Enums/Moderations/Category.php | 24 +-- src/Enums/Transporter/ContentType.php | 11 +- src/Enums/Transporter/Method.php | 13 +- src/Exceptions/ErrorException.php | 11 +- src/Factory.php | 33 ++-- src/Resources/Concerns/Transportable.php | 2 +- src/Responses/Audio/TranscriptionResponse.php | 72 +++++--- .../Audio/TranscriptionResponseSegment.php | 95 +++++++--- src/Responses/Audio/TranslationResponse.php | 72 +++++--- .../Audio/TranslationResponseSegment.php | 95 +++++++--- src/Responses/Chat/CreateResponse.php | 78 +++++--- src/Responses/Chat/CreateResponseChoice.php | 32 ++-- .../Chat/CreateResponseFunctionCall.php | 24 ++- src/Responses/Chat/CreateResponseMessage.php | 32 ++-- src/Responses/Chat/CreateResponseUsage.php | 32 ++-- src/Responses/Chat/CreateStreamedResponse.php | 62 ++++--- .../Chat/CreateStreamedResponseChoice.php | 32 ++-- .../Chat/CreateStreamedResponseDelta.php | 36 ++-- .../CreateStreamedResponseFunctionCall.php | 24 ++- src/Responses/Completions/CreateResponse.php | 78 +++++--- .../Completions/CreateResponseChoice.php | 40 +++-- .../CreateResponseChoiceLogprobs.php | 40 +++-- .../Completions/CreateResponseUsage.php | 32 ++-- .../Completions/CreateStreamedResponse.php | 62 ++++--- src/Responses/Concerns/ArrayAccessible.php | 16 +- src/Responses/Edits/CreateResponse.php | 62 ++++--- src/Responses/Edits/CreateResponseChoice.php | 24 ++- src/Responses/Edits/CreateResponseUsage.php | 32 ++-- src/Responses/Embeddings/CreateResponse.php | 54 ++++-- .../Embeddings/CreateResponseEmbedding.php | 32 ++-- .../Embeddings/CreateResponseUsage.php | 24 ++- src/Responses/Files/CreateResponse.php | 79 ++++++--- src/Responses/Files/DeleteResponse.php | 39 ++-- src/Responses/Files/ListResponse.php | 45 +++-- src/Responses/Files/RetrieveResponse.php | 79 ++++++--- .../FineTunes/ListEventsResponse.php | 46 +++-- src/Responses/FineTunes/ListResponse.php | 45 +++-- src/Responses/FineTunes/RetrieveResponse.php | 167 +++++++++++++----- .../FineTunes/RetrieveResponseEvent.php | 39 ++-- .../FineTunes/RetrieveResponseFile.php | 71 +++++--- .../FineTunes/RetrieveResponseHyperparams.php | 39 ++-- .../RetrieveStreamedResponseEvent.php | 39 ++-- .../FineTuning/ListJobEventsResponse.php | 54 ++++-- .../FineTuning/ListJobEventsResponseEvent.php | 65 +++++-- .../ListJobEventsResponseEventData.php | 31 ++-- src/Responses/FineTuning/ListJobsResponse.php | 53 +++--- .../FineTuning/RetrieveJobResponse.php | 119 +++++++++---- .../RetrieveJobResponseHyperparameters.php | 18 +- src/Responses/Images/CreateResponse.php | 46 +++-- src/Responses/Images/CreateResponseData.php | 23 ++- src/Responses/Images/EditResponse.php | 46 +++-- src/Responses/Images/EditResponseData.php | 23 ++- src/Responses/Images/VariationResponse.php | 46 +++-- .../Images/VariationResponseData.php | 23 ++- src/Responses/Meta/MetaInformation.php | 42 +++-- src/Responses/Meta/MetaInformationOpenAI.php | 40 +++-- .../Meta/MetaInformationRateLimit.php | 32 ++-- src/Responses/Models/DeleteResponse.php | 39 ++-- src/Responses/Models/ListResponse.php | 45 +++-- src/Responses/Models/RetrieveResponse.php | 86 ++++++--- .../Models/RetrieveResponsePermission.php | 104 ++++++++--- src/Responses/Moderations/CreateResponse.php | 54 ++++-- .../Moderations/CreateResponseCategory.php | 32 ++-- .../Moderations/CreateResponseResult.php | 19 +- src/Responses/StreamResponse.php | 26 ++- src/Testing/ClientFake.php | 61 ++++--- src/Testing/Requests/TestRequest.php | 19 +- src/Testing/Resources/Concerns/Testable.php | 13 +- src/Testing/Responses/Concerns/Fakeable.php | 8 +- .../Fixtures/Chat/CreateResponseFixture.php | 2 +- .../Completions/CreateResponseFixture.php | 2 +- .../Fixtures/Edits/CreateResponseFixture.php | 2 +- .../Fixtures/Files/CreateResponseFixture.php | 2 +- .../Fixtures/Files/ListResponseFixture.php | 2 +- .../Files/RetrieveResponseFixture.php | 2 +- .../FineTunes/ListEventsResponseFixture.php | 2 +- .../FineTunes/RetrieveResponseFixture.php | 6 +- .../ListJobEventsResponseFixture.php | 2 +- .../FineTuning/RetrieveJobResponseFixture.php | 4 +- .../Fixtures/Images/CreateResponseFixture.php | 2 +- .../Fixtures/Images/EditResponseFixture.php | 2 +- .../Images/VariationResponseFixture.php | 2 +- .../Models/RetrieveResponseFixture.php | 4 +- src/Transporters/HttpTransporter.php | 65 +++++-- src/ValueObjects/ApiKey.php | 8 +- src/ValueObjects/ResourceUri.php | 8 +- src/ValueObjects/Transporter/BaseUri.php | 10 +- src/ValueObjects/Transporter/Headers.php | 26 ++- src/ValueObjects/Transporter/Payload.php | 37 +++- src/ValueObjects/Transporter/QueryParams.php | 16 +- src/ValueObjects/Transporter/Response.php | 24 ++- 96 files changed, 2320 insertions(+), 1139 deletions(-) diff --git a/rector.php b/rector.php index da8a14ea..626a8c82 100644 --- a/rector.php +++ b/rector.php @@ -5,6 +5,7 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector; +use Rector\Set\ValueObject\DowngradeLevelSetList; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; @@ -13,20 +14,11 @@ __DIR__.'/src', ]); - $rectorConfig->skip([ - __DIR__.'/src/Testing/ClientFake.php' => FinalizeClassesWithoutChildrenRector::class, - ]); - - $rectorConfig->rules([ - InlineConstructorDefaultToPropertyRector::class, - ]); - + // here we can define, what sets of rules will be applied $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_81, - SetList::CODE_QUALITY, - SetList::DEAD_CODE, - SetList::EARLY_RETURN, - SetList::TYPE_DECLARATION, - SetList::PRIVATIZATION, + DowngradeLevelSetList::DOWN_TO_PHP_72 ]); + + // disable parallel execution to avoid errors + $rectorConfig->disableParallel(); }; diff --git a/src/Client.php b/src/Client.php index 9c5df261..dc708afa 100644 --- a/src/Client.php +++ b/src/Client.php @@ -20,11 +20,17 @@ final class Client implements ClientContract { + /** + * @readonly + * @var \OpenAI\Contracts\TransporterContract + */ + private $transporter; /** * Creates a Client instance with the given API token. */ - public function __construct(private readonly TransporterContract $transporter) + public function __construct(TransporterContract $transporter) { + $this->transporter = $transporter; // .. } @@ -34,7 +40,7 @@ public function __construct(private readonly TransporterContract $transporter) * * @see https://platform.openai.com/docs/api-reference/completions */ - public function completions(): Completions + public function completions(): \OpenAI\Contracts\Resources\CompletionsContract { return new Completions($this->transporter); } @@ -44,7 +50,7 @@ public function completions(): Completions * * @see https://platform.openai.com/docs/api-reference/chat */ - public function chat(): Chat + public function chat(): \OpenAI\Contracts\Resources\ChatContract { return new Chat($this->transporter); } @@ -54,7 +60,7 @@ public function chat(): Chat * * @see https://platform.openai.com/docs/api-reference/embeddings */ - public function embeddings(): Embeddings + public function embeddings(): \OpenAI\Contracts\Resources\EmbeddingsContract { return new Embeddings($this->transporter); } @@ -64,7 +70,7 @@ public function embeddings(): Embeddings * * @see https://platform.openai.com/docs/api-reference/audio */ - public function audio(): Audio + public function audio(): \OpenAI\Contracts\Resources\AudioContract { return new Audio($this->transporter); } @@ -74,7 +80,7 @@ public function audio(): Audio * * @see https://platform.openai.com/docs/api-reference/edits */ - public function edits(): Edits + public function edits(): \OpenAI\Contracts\Resources\EditsContract { return new Edits($this->transporter); } @@ -84,7 +90,7 @@ public function edits(): Edits * * @see https://platform.openai.com/docs/api-reference/files */ - public function files(): Files + public function files(): \OpenAI\Contracts\Resources\FilesContract { return new Files($this->transporter); } @@ -94,7 +100,7 @@ public function files(): Files * * @see https://platform.openai.com/docs/api-reference/models */ - public function models(): Models + public function models(): \OpenAI\Contracts\Resources\ModelsContract { return new Models($this->transporter); } @@ -104,7 +110,7 @@ public function models(): Models * * @see https://platform.openai.com/docs/api-reference/fine-tuning */ - public function fineTuning(): FineTuning + public function fineTuning(): \OpenAI\Contracts\Resources\FineTuningContract { return new FineTuning($this->transporter); } @@ -116,7 +122,7 @@ public function fineTuning(): FineTuning * @deprecated OpenAI has deprecated this endpoint and will stop working by January 4, 2024. * https://openai.com/blog/gpt-3-5-turbo-fine-tuning-and-api-updates#updated-gpt-3-models */ - public function fineTunes(): FineTunes + public function fineTunes(): \OpenAI\Contracts\Resources\FineTunesContract { return new FineTunes($this->transporter); } @@ -126,7 +132,7 @@ public function fineTunes(): FineTunes * * @see https://platform.openai.com/docs/api-reference/moderations */ - public function moderations(): Moderations + public function moderations(): \OpenAI\Contracts\Resources\ModerationsContract { return new Moderations($this->transporter); } @@ -136,7 +142,7 @@ public function moderations(): Moderations * * @see https://platform.openai.com/docs/api-reference/images */ - public function images(): Images + public function images(): \OpenAI\Contracts\Resources\ImagesContract { return new Images($this->transporter); } diff --git a/src/Contracts/MetaInformationContract.php b/src/Contracts/MetaInformationContract.php index b60a4d6b..05da9528 100644 --- a/src/Contracts/MetaInformationContract.php +++ b/src/Contracts/MetaInformationContract.php @@ -23,30 +23,32 @@ interface MetaInformationContract extends ArrayAccess public function toArray(): array; /** - * @param key-of $offset + * @param mixed $offset */ - public function offsetExists(mixed $offset): bool; + public function offsetExists($offset): bool; /** * @template TOffsetKey of key-of * - * @param TOffsetKey $offset + * @param mixed $offset * @return TArray[TOffsetKey] */ - public function offsetGet(mixed $offset): mixed; + public function offsetGet($offset); /** * @template TOffsetKey of key-of * - * @param TOffsetKey $offset - * @param TArray[TOffsetKey] $value + * @param mixed $offset + * @param mixed $value + * @return never */ - public function offsetSet(mixed $offset, mixed $value): never; + public function offsetSet($offset, $value); /** * @template TOffsetKey of key-of * - * @param TOffsetKey $offset + * @param mixed $offset + * @return never */ - public function offsetUnset(mixed $offset): never; + public function offsetUnset($offset); } diff --git a/src/Contracts/ResponseContract.php b/src/Contracts/ResponseContract.php index 0dfcffea..a12ce31a 100644 --- a/src/Contracts/ResponseContract.php +++ b/src/Contracts/ResponseContract.php @@ -23,30 +23,32 @@ interface ResponseContract extends ArrayAccess public function toArray(): array; /** - * @param key-of $offset + * @param mixed $offset */ - public function offsetExists(mixed $offset): bool; + public function offsetExists($offset): bool; /** * @template TOffsetKey of key-of * - * @param TOffsetKey $offset + * @param mixed $offset * @return TArray[TOffsetKey] */ - public function offsetGet(mixed $offset): mixed; + public function offsetGet($offset); /** * @template TOffsetKey of key-of * - * @param TOffsetKey $offset - * @param TArray[TOffsetKey] $value + * @param mixed $offset + * @param mixed $value + * @return never */ - public function offsetSet(mixed $offset, mixed $value): never; + public function offsetSet($offset, $value); /** * @template TOffsetKey of key-of * - * @param TOffsetKey $offset + * @param mixed $offset + * @return never */ - public function offsetUnset(mixed $offset): never; + public function offsetUnset($offset); } diff --git a/src/Enums/FineTuning/FineTuningEventLevel.php b/src/Enums/FineTuning/FineTuningEventLevel.php index 548887dd..5c00e4e3 100644 --- a/src/Enums/FineTuning/FineTuningEventLevel.php +++ b/src/Enums/FineTuning/FineTuningEventLevel.php @@ -2,9 +2,9 @@ namespace OpenAI\Enums\FineTuning; -enum FineTuningEventLevel: string +class FineTuningEventLevel { - case Info = 'info'; - case Warning = 'warn'; - case Error = 'error'; + public const Info = 'info'; + public const Warning = 'warn'; + public const Error = 'error'; } diff --git a/src/Enums/Moderations/Category.php b/src/Enums/Moderations/Category.php index 34bdbaf6..ceeb8aa3 100644 --- a/src/Enums/Moderations/Category.php +++ b/src/Enums/Moderations/Category.php @@ -4,17 +4,17 @@ namespace OpenAI\Enums\Moderations; -enum Category: string +class Category { - case Hate = 'hate'; - case HateThreatening = 'hate/threatening'; - case Harassment = 'harassment'; - case HarassmentThreatening = 'harassment/threatening'; - case SelfHarm = 'self-harm'; - case SelfHarmIntent = 'self-harm/intent'; - case SelfHarmInstructions = 'self-harm/instructions'; - case Sexual = 'sexual'; - case SexualMinors = 'sexual/minors'; - case Violence = 'violence'; - case ViolenceGraphic = 'violence/graphic'; + public const Hate = 'hate'; + public const HateThreatening = 'hate/threatening'; + public const Harassment = 'harassment'; + public const HarassmentThreatening = 'harassment/threatening'; + public const SelfHarm = 'self-harm'; + public const SelfHarmIntent = 'self-harm/intent'; + public const SelfHarmInstructions = 'self-harm/instructions'; + public const Sexual = 'sexual'; + public const SexualMinors = 'sexual/minors'; + public const Violence = 'violence'; + public const ViolenceGraphic = 'violence/graphic'; } diff --git a/src/Enums/Transporter/ContentType.php b/src/Enums/Transporter/ContentType.php index 6ec7fba9..578ae95e 100644 --- a/src/Enums/Transporter/ContentType.php +++ b/src/Enums/Transporter/ContentType.php @@ -4,12 +4,9 @@ namespace OpenAI\Enums\Transporter; -/** - * @internal - */ -enum ContentType: string +class ContentType { - case JSON = 'application/json'; - case MULTIPART = 'multipart/form-data'; - case TEXT_PLAIN = 'text/plain'; + public const JSON = 'application/json'; + public const MULTIPART = 'multipart/form-data'; + public const TEXT_PLAIN = 'text/plain'; } diff --git a/src/Enums/Transporter/Method.php b/src/Enums/Transporter/Method.php index 50612d18..9acc462e 100644 --- a/src/Enums/Transporter/Method.php +++ b/src/Enums/Transporter/Method.php @@ -4,13 +4,10 @@ namespace OpenAI\Enums\Transporter; -/** - * @internal - */ -enum Method: string +class Method { - case GET = 'GET'; - case POST = 'POST'; - case PUT = 'PUT'; - case DELETE = 'DELETE'; + public const GET = 'GET'; + public const POST = 'POST'; + public const PUT = 'PUT'; + public const DELETE = 'DELETE'; } diff --git a/src/Exceptions/ErrorException.php b/src/Exceptions/ErrorException.php index f82bc36f..2af59f0d 100644 --- a/src/Exceptions/ErrorException.php +++ b/src/Exceptions/ErrorException.php @@ -8,13 +8,19 @@ final class ErrorException extends Exception { + /** + * @var array{message: (string | array), type: ?string, code: (string | int | null)} + * @readonly + */ + private $contents; /** * Creates a new Exception instance. * * @param array{message: string|array, type: ?string, code: string|int|null} $contents */ - public function __construct(private readonly array $contents) + public function __construct(array $contents) { + $this->contents = $contents; $message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error'; if (is_array($message)) { @@ -42,8 +48,9 @@ public function getErrorType(): ?string /** * Returns the error code. + * @return string|int|null */ - public function getErrorCode(): string|int|null + public function getErrorCode() { return $this->contents['code']; } diff --git a/src/Factory.php b/src/Factory.php index d0415205..c842041d 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -20,39 +20,46 @@ final class Factory { /** * The API key for the requests. + * @var string|null */ - private ?string $apiKey = null; + private $apiKey; /** * The organization for the requests. + * @var string|null */ - private ?string $organization = null; + private $organization; /** * The HTTP client for the requests. + * @var \Psr\Http\Client\ClientInterface|null */ - private ?ClientInterface $httpClient = null; + private $httpClient; /** * The base URI for the requests. + * @var string|null */ - private ?string $baseUri = null; + private $baseUri; /** * The HTTP headers for the requests. * * @var array */ - private array $headers = []; + private $headers = []; /** * The query parameters for the requests. * * @var array */ - private array $queryParams = []; + private $queryParams = []; - private ?Closure $streamHandler = null; + /** + * @var \Closure|null + */ + private $streamHandler; /** * Sets the API key for the requests. @@ -152,7 +159,7 @@ public function make(): Client $queryParams = $queryParams->withParam($name, $value); } - $client = $this->httpClient ??= Psr18ClientDiscovery::find(); + $client = $this->httpClient = $this->httpClient ?? Psr18ClientDiscovery::find(); $sendAsync = $this->makeStreamHandler($client); @@ -171,14 +178,18 @@ private function makeStreamHandler(ClientInterface $client): Closure } if ($client instanceof GuzzleClient) { - return fn (RequestInterface $request): ResponseInterface => $client->send($request, ['stream' => true]); + return function (RequestInterface $request) use ($client) : ResponseInterface { + return $client->send($request, ['stream' => true]); + }; } if ($client instanceof Psr18Client) { // @phpstan-ignore-line - return fn (RequestInterface $request): ResponseInterface => $client->sendRequest($request); // @phpstan-ignore-line + return function (RequestInterface $request) use ($client) : ResponseInterface { + return $client->sendRequest($request); + }; // @phpstan-ignore-line } - return function (RequestInterface $_): never { + return function (RequestInterface $_) { throw new Exception('To use stream requests you must provide an stream handler closure via the OpenAI factory.'); }; } diff --git a/src/Resources/Concerns/Transportable.php b/src/Resources/Concerns/Transportable.php index 091511f9..f5cf79d4 100644 --- a/src/Resources/Concerns/Transportable.php +++ b/src/Resources/Concerns/Transportable.php @@ -11,7 +11,7 @@ trait Transportable /** * Creates a Client instance with the given API token. */ - public function __construct(private readonly TransporterContract $transporter) + public function __construct(private TransporterContract $transporter) { // .. } diff --git a/src/Responses/Audio/TranscriptionResponse.php b/src/Responses/Audio/TranscriptionResponse.php index f126e8c8..6190ebcd 100644 --- a/src/Responses/Audio/TranscriptionResponse.php +++ b/src/Responses/Audio/TranscriptionResponse.php @@ -16,6 +16,36 @@ */ final class TranscriptionResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string|null + */ + public $task; + /** + * @readonly + * @var string|null + */ + public $language; + /** + * @readonly + * @var float|null + */ + public $duration; + /** + * @var array + * @readonly + */ + public $segments; + /** + * @readonly + * @var string + */ + public $text; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}> */ @@ -27,14 +57,14 @@ final class TranscriptionResponse implements ResponseContract, ResponseHasMetaIn /** * @param array $segments */ - private function __construct( - public readonly ?string $task, - public readonly ?string $language, - public readonly ?float $duration, - public readonly array $segments, - public readonly string $text, - private readonly MetaInformation $meta, - ) { + private function __construct(?string $task, ?string $language, ?float $duration, array $segments, string $text, MetaInformation $meta) + { + $this->task = $task; + $this->language = $language; + $this->duration = $duration; + $this->segments = $segments; + $this->text = $text; + $this->meta = $meta; } /** @@ -42,24 +72,19 @@ private function __construct( * * @param array{task: ?string, language: ?string, duration: ?float, segments: array, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}|string $attributes */ - public static function from(array|string $attributes, MetaInformation $meta): self + public static function from($attributes, MetaInformation $meta): self { if (is_string($attributes)) { $attributes = ['text' => $attributes]; } - $segments = isset($attributes['segments']) ? array_map(fn (array $result): TranscriptionResponseSegment => TranscriptionResponseSegment::from( - $result - ), $attributes['segments']) : []; + $segments = isset($attributes['segments']) ? array_map(function (array $result) : TranscriptionResponseSegment { + return TranscriptionResponseSegment::from( + $result + ); + }, $attributes['segments']) : []; - return new self( - $attributes['task'] ?? null, - $attributes['language'] ?? null, - $attributes['duration'] ?? null, - $segments, - $attributes['text'], - $meta, - ); + return new self($attributes['task'] ?? null, $attributes['language'] ?? null, $attributes['duration'] ?? null, $segments, $attributes['text'], $meta); } /** @@ -71,10 +96,9 @@ public function toArray(): array 'task' => $this->task, 'language' => $this->language, 'duration' => $this->duration, - 'segments' => array_map( - static fn (TranscriptionResponseSegment $result): array => $result->toArray(), - $this->segments, - ), + 'segments' => array_map(static function (TranscriptionResponseSegment $result) : array { + return $result->toArray(); + }, $this->segments), 'text' => $this->text, ]; } diff --git a/src/Responses/Audio/TranscriptionResponseSegment.php b/src/Responses/Audio/TranscriptionResponseSegment.php index 0b15acc4..6003dbb0 100644 --- a/src/Responses/Audio/TranscriptionResponseSegment.php +++ b/src/Responses/Audio/TranscriptionResponseSegment.php @@ -12,6 +12,61 @@ */ final class TranscriptionResponseSegment implements ResponseContract { + /** + * @readonly + * @var int + */ + public $id; + /** + * @readonly + * @var int + */ + public $seek; + /** + * @readonly + * @var float + */ + public $start; + /** + * @readonly + * @var float + */ + public $end; + /** + * @readonly + * @var string + */ + public $text; + /** + * @var array + * @readonly + */ + public $tokens; + /** + * @readonly + * @var float + */ + public $temperature; + /** + * @readonly + * @var float + */ + public $avgLogprob; + /** + * @readonly + * @var float + */ + public $compressionRatio; + /** + * @readonly + * @var float + */ + public $noSpeechProb; + /** + * @readonly + * @var bool + */ + public $transient; /** * @use ArrayAccessible, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}> */ @@ -20,19 +75,19 @@ final class TranscriptionResponseSegment implements ResponseContract /** * @param array $tokens */ - private function __construct( - public readonly int $id, - public readonly int $seek, - public readonly float $start, - public readonly float $end, - public readonly string $text, - public readonly array $tokens, - public readonly float $temperature, - public readonly float $avgLogprob, - public readonly float $compressionRatio, - public readonly float $noSpeechProb, - public readonly bool $transient, - ) { + private function __construct(int $id, int $seek, float $start, float $end, string $text, array $tokens, float $temperature, float $avgLogprob, float $compressionRatio, float $noSpeechProb, bool $transient) + { + $this->id = $id; + $this->seek = $seek; + $this->start = $start; + $this->end = $end; + $this->text = $text; + $this->tokens = $tokens; + $this->temperature = $temperature; + $this->avgLogprob = $avgLogprob; + $this->compressionRatio = $compressionRatio; + $this->noSpeechProb = $noSpeechProb; + $this->transient = $transient; } /** @@ -42,19 +97,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['id'], - $attributes['seek'], - $attributes['start'], - $attributes['end'], - $attributes['text'], - $attributes['tokens'], - $attributes['temperature'], - $attributes['avg_logprob'], - $attributes['compression_ratio'], - $attributes['no_speech_prob'], - $attributes['transient'] ?? false, - ); + return new self($attributes['id'], $attributes['seek'], $attributes['start'], $attributes['end'], $attributes['text'], $attributes['tokens'], $attributes['temperature'], $attributes['avg_logprob'], $attributes['compression_ratio'], $attributes['no_speech_prob'], $attributes['transient'] ?? false); } /** diff --git a/src/Responses/Audio/TranslationResponse.php b/src/Responses/Audio/TranslationResponse.php index 32a9ac1f..077e1d8b 100644 --- a/src/Responses/Audio/TranslationResponse.php +++ b/src/Responses/Audio/TranslationResponse.php @@ -16,6 +16,36 @@ */ final class TranslationResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string|null + */ + public $task; + /** + * @readonly + * @var string|null + */ + public $language; + /** + * @readonly + * @var float|null + */ + public $duration; + /** + * @var array + * @readonly + */ + public $segments; + /** + * @readonly + * @var string + */ + public $text; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}> */ @@ -27,14 +57,14 @@ final class TranslationResponse implements ResponseContract, ResponseHasMetaInfo /** * @param array $segments */ - private function __construct( - public readonly ?string $task, - public readonly ?string $language, - public readonly ?float $duration, - public readonly array $segments, - public readonly string $text, - private readonly MetaInformation $meta, - ) { + private function __construct(?string $task, ?string $language, ?float $duration, array $segments, string $text, MetaInformation $meta) + { + $this->task = $task; + $this->language = $language; + $this->duration = $duration; + $this->segments = $segments; + $this->text = $text; + $this->meta = $meta; } /** @@ -42,24 +72,19 @@ private function __construct( * * @param array{task: ?string, language: ?string, duration: ?float, segments: array, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string} $attributes */ - public static function from(array|string $attributes, MetaInformation $meta): self + public static function from($attributes, MetaInformation $meta): self { if (is_string($attributes)) { $attributes = ['text' => $attributes]; } - $segments = isset($attributes['segments']) ? array_map(fn (array $result): TranslationResponseSegment => TranslationResponseSegment::from( - $result - ), $attributes['segments']) : []; + $segments = isset($attributes['segments']) ? array_map(function (array $result) : TranslationResponseSegment { + return TranslationResponseSegment::from( + $result + ); + }, $attributes['segments']) : []; - return new self( - $attributes['task'] ?? null, - $attributes['language'] ?? null, - $attributes['duration'] ?? null, - $segments, - $attributes['text'], - $meta, - ); + return new self($attributes['task'] ?? null, $attributes['language'] ?? null, $attributes['duration'] ?? null, $segments, $attributes['text'], $meta); } /** @@ -71,10 +96,9 @@ public function toArray(): array 'task' => $this->task, 'language' => $this->language, 'duration' => $this->duration, - 'segments' => array_map( - static fn (TranslationResponseSegment $result): array => $result->toArray(), - $this->segments, - ), + 'segments' => array_map(static function (TranslationResponseSegment $result) : array { + return $result->toArray(); + }, $this->segments), 'text' => $this->text, ]; } diff --git a/src/Responses/Audio/TranslationResponseSegment.php b/src/Responses/Audio/TranslationResponseSegment.php index eb22a8b1..8d1e0b38 100644 --- a/src/Responses/Audio/TranslationResponseSegment.php +++ b/src/Responses/Audio/TranslationResponseSegment.php @@ -12,6 +12,61 @@ */ final class TranslationResponseSegment implements ResponseContract { + /** + * @readonly + * @var int + */ + public $id; + /** + * @readonly + * @var int + */ + public $seek; + /** + * @readonly + * @var float + */ + public $start; + /** + * @readonly + * @var float + */ + public $end; + /** + * @readonly + * @var string + */ + public $text; + /** + * @var array + * @readonly + */ + public $tokens; + /** + * @readonly + * @var float + */ + public $temperature; + /** + * @readonly + * @var float + */ + public $avgLogprob; + /** + * @readonly + * @var float + */ + public $compressionRatio; + /** + * @readonly + * @var float + */ + public $noSpeechProb; + /** + * @readonly + * @var bool + */ + public $transient; /** * @use ArrayAccessible, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}> */ @@ -20,19 +75,19 @@ final class TranslationResponseSegment implements ResponseContract /** * @param array $tokens */ - private function __construct( - public readonly int $id, - public readonly int $seek, - public readonly float $start, - public readonly float $end, - public readonly string $text, - public readonly array $tokens, - public readonly float $temperature, - public readonly float $avgLogprob, - public readonly float $compressionRatio, - public readonly float $noSpeechProb, - public readonly bool $transient, - ) { + private function __construct(int $id, int $seek, float $start, float $end, string $text, array $tokens, float $temperature, float $avgLogprob, float $compressionRatio, float $noSpeechProb, bool $transient) + { + $this->id = $id; + $this->seek = $seek; + $this->start = $start; + $this->end = $end; + $this->text = $text; + $this->tokens = $tokens; + $this->temperature = $temperature; + $this->avgLogprob = $avgLogprob; + $this->compressionRatio = $compressionRatio; + $this->noSpeechProb = $noSpeechProb; + $this->transient = $transient; } /** @@ -42,19 +97,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['id'], - $attributes['seek'], - $attributes['start'], - $attributes['end'], - $attributes['text'], - $attributes['tokens'], - $attributes['temperature'], - $attributes['avg_logprob'], - $attributes['compression_ratio'], - $attributes['no_speech_prob'], - $attributes['transient'] ?? false, - ); + return new self($attributes['id'], $attributes['seek'], $attributes['start'], $attributes['end'], $attributes['text'], $attributes['tokens'], $attributes['temperature'], $attributes['avg_logprob'], $attributes['compression_ratio'], $attributes['no_speech_prob'], $attributes['transient'] ?? false); } /** diff --git a/src/Responses/Chat/CreateResponse.php b/src/Responses/Chat/CreateResponse.php index 96da30ba..2175e6f5 100644 --- a/src/Responses/Chat/CreateResponse.php +++ b/src/Responses/Chat/CreateResponse.php @@ -16,6 +16,41 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @readonly + * @var string + */ + public $model; + /** + * @var array + * @readonly + */ + public $choices; + /** + * @readonly + * @var \OpenAI\Responses\Chat\CreateResponseUsage + */ + public $usage; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> */ @@ -27,15 +62,15 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array $choices */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $created, - public readonly string $model, - public readonly array $choices, - public readonly CreateResponseUsage $usage, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, int $created, string $model, array $choices, CreateResponseUsage $usage, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->created = $created; + $this->model = $model; + $this->choices = $choices; + $this->usage = $usage; + $this->meta = $meta; } /** @@ -45,19 +80,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $choices = array_map(fn (array $result): CreateResponseChoice => CreateResponseChoice::from( - $result - ), $attributes['choices']); + $choices = array_map(function (array $result) : CreateResponseChoice { + return CreateResponseChoice::from( + $result + ); + }, $attributes['choices']); - return new self( - $attributes['id'], - $attributes['object'], - $attributes['created'], - $attributes['model'], - $choices, - CreateResponseUsage::from($attributes['usage']), - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['created'], $attributes['model'], $choices, CreateResponseUsage::from($attributes['usage']), $meta); } /** @@ -70,10 +99,9 @@ public function toArray(): array 'object' => $this->object, 'created' => $this->created, 'model' => $this->model, - 'choices' => array_map( - static fn (CreateResponseChoice $result): array => $result->toArray(), - $this->choices, - ), + 'choices' => array_map(static function (CreateResponseChoice $result) : array { + return $result->toArray(); + }, $this->choices), 'usage' => $this->usage->toArray(), ]; } diff --git a/src/Responses/Chat/CreateResponseChoice.php b/src/Responses/Chat/CreateResponseChoice.php index 1204538b..d60e32c5 100644 --- a/src/Responses/Chat/CreateResponseChoice.php +++ b/src/Responses/Chat/CreateResponseChoice.php @@ -6,23 +6,33 @@ final class CreateResponseChoice { - private function __construct( - public readonly int $index, - public readonly CreateResponseMessage $message, - public readonly ?string $finishReason, - ) { + /** + * @readonly + * @var int + */ + public $index; + /** + * @readonly + * @var \OpenAI\Responses\Chat\CreateResponseMessage + */ + public $message; + /** + * @readonly + * @var string|null + */ + public $finishReason; + private function __construct(int $index, CreateResponseMessage $message, ?string $finishReason) + { + $this->index = $index; + $this->message = $message; + $this->finishReason = $finishReason; } - /** * @param array{index: int, message: array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}}, finish_reason: string|null} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['index'], - CreateResponseMessage::from($attributes['message']), - $attributes['finish_reason'], - ); + return new self($attributes['index'], CreateResponseMessage::from($attributes['message']), $attributes['finish_reason']); } /** diff --git a/src/Responses/Chat/CreateResponseFunctionCall.php b/src/Responses/Chat/CreateResponseFunctionCall.php index bd7f1cbc..4f83e354 100644 --- a/src/Responses/Chat/CreateResponseFunctionCall.php +++ b/src/Responses/Chat/CreateResponseFunctionCall.php @@ -6,21 +6,27 @@ final class CreateResponseFunctionCall { - private function __construct( - public readonly string $name, - public readonly string $arguments, - ) { + /** + * @readonly + * @var string + */ + public $name; + /** + * @readonly + * @var string + */ + public $arguments; + private function __construct(string $name, string $arguments) + { + $this->name = $name; + $this->arguments = $arguments; } - /** * @param array{name: string, arguments: string} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['name'], - $attributes['arguments'], - ); + return new self($attributes['name'], $attributes['arguments']); } /** diff --git a/src/Responses/Chat/CreateResponseMessage.php b/src/Responses/Chat/CreateResponseMessage.php index 20889e74..6d14140f 100644 --- a/src/Responses/Chat/CreateResponseMessage.php +++ b/src/Responses/Chat/CreateResponseMessage.php @@ -6,23 +6,33 @@ final class CreateResponseMessage { - private function __construct( - public readonly string $role, - public readonly ?string $content, - public readonly ?CreateResponseFunctionCall $functionCall, - ) { + /** + * @readonly + * @var string + */ + public $role; + /** + * @readonly + * @var string|null + */ + public $content; + /** + * @readonly + * @var \OpenAI\Responses\Chat\CreateResponseFunctionCall|null + */ + public $functionCall; + private function __construct(string $role, ?string $content, ?CreateResponseFunctionCall $functionCall) + { + $this->role = $role; + $this->content = $content; + $this->functionCall = $functionCall; } - /** * @param array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['role'], - $attributes['content'] ?? null, - isset($attributes['function_call']) ? CreateResponseFunctionCall::from($attributes['function_call']) : null, - ); + return new self($attributes['role'], $attributes['content'] ?? null, isset($attributes['function_call']) ? CreateResponseFunctionCall::from($attributes['function_call']) : null); } /** diff --git a/src/Responses/Chat/CreateResponseUsage.php b/src/Responses/Chat/CreateResponseUsage.php index c006423a..2baf031a 100644 --- a/src/Responses/Chat/CreateResponseUsage.php +++ b/src/Responses/Chat/CreateResponseUsage.php @@ -6,23 +6,33 @@ final class CreateResponseUsage { - private function __construct( - public readonly int $promptTokens, - public readonly ?int $completionTokens, - public readonly int $totalTokens, - ) { + /** + * @readonly + * @var int + */ + public $promptTokens; + /** + * @readonly + * @var int|null + */ + public $completionTokens; + /** + * @readonly + * @var int + */ + public $totalTokens; + private function __construct(int $promptTokens, ?int $completionTokens, int $totalTokens) + { + $this->promptTokens = $promptTokens; + $this->completionTokens = $completionTokens; + $this->totalTokens = $totalTokens; } - /** * @param array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['prompt_tokens'], - $attributes['completion_tokens'] ?? null, - $attributes['total_tokens'], - ); + return new self($attributes['prompt_tokens'], $attributes['completion_tokens'] ?? null, $attributes['total_tokens']); } /** diff --git a/src/Responses/Chat/CreateStreamedResponse.php b/src/Responses/Chat/CreateStreamedResponse.php index 5b0ee289..ea765eaf 100644 --- a/src/Responses/Chat/CreateStreamedResponse.php +++ b/src/Responses/Chat/CreateStreamedResponse.php @@ -13,6 +13,31 @@ */ final class CreateStreamedResponse implements ResponseContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @readonly + * @var string + */ + public $model; + /** + * @var array + * @readonly + */ + public $choices; /** * @use ArrayAccessible}> */ @@ -23,13 +48,13 @@ final class CreateStreamedResponse implements ResponseContract /** * @param array $choices */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $created, - public readonly string $model, - public readonly array $choices, - ) { + private function __construct(string $id, string $object, int $created, string $model, array $choices) + { + $this->id = $id; + $this->object = $object; + $this->created = $created; + $this->model = $model; + $this->choices = $choices; } /** @@ -39,17 +64,13 @@ private function __construct( */ public static function from(array $attributes): self { - $choices = array_map(fn (array $result): CreateStreamedResponseChoice => CreateStreamedResponseChoice::from( - $result - ), $attributes['choices']); + $choices = array_map(function (array $result) : CreateStreamedResponseChoice { + return CreateStreamedResponseChoice::from( + $result + ); + }, $attributes['choices']); - return new self( - $attributes['id'], - $attributes['object'], - $attributes['created'], - $attributes['model'], - $choices, - ); + return new self($attributes['id'], $attributes['object'], $attributes['created'], $attributes['model'], $choices); } /** @@ -62,10 +83,9 @@ public function toArray(): array 'object' => $this->object, 'created' => $this->created, 'model' => $this->model, - 'choices' => array_map( - static fn (CreateStreamedResponseChoice $result): array => $result->toArray(), - $this->choices, - ), + 'choices' => array_map(static function (CreateStreamedResponseChoice $result) : array { + return $result->toArray(); + }, $this->choices), ]; } } diff --git a/src/Responses/Chat/CreateStreamedResponseChoice.php b/src/Responses/Chat/CreateStreamedResponseChoice.php index 48ecd14c..82514909 100644 --- a/src/Responses/Chat/CreateStreamedResponseChoice.php +++ b/src/Responses/Chat/CreateStreamedResponseChoice.php @@ -6,23 +6,33 @@ final class CreateStreamedResponseChoice { - private function __construct( - public readonly int $index, - public readonly CreateStreamedResponseDelta $delta, - public readonly ?string $finishReason, - ) { + /** + * @readonly + * @var int + */ + public $index; + /** + * @readonly + * @var \OpenAI\Responses\Chat\CreateStreamedResponseDelta + */ + public $delta; + /** + * @readonly + * @var string|null + */ + public $finishReason; + private function __construct(int $index, CreateStreamedResponseDelta $delta, ?string $finishReason) + { + $this->index = $index; + $this->delta = $delta; + $this->finishReason = $finishReason; } - /** * @param array{index: int, delta: array{role?: string, content?: string}, finish_reason: string|null} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['index'], - CreateStreamedResponseDelta::from($attributes['delta']), - $attributes['finish_reason'], - ); + return new self($attributes['index'], CreateStreamedResponseDelta::from($attributes['delta']), $attributes['finish_reason']); } /** diff --git a/src/Responses/Chat/CreateStreamedResponseDelta.php b/src/Responses/Chat/CreateStreamedResponseDelta.php index 5d4511f9..d2930482 100644 --- a/src/Responses/Chat/CreateStreamedResponseDelta.php +++ b/src/Responses/Chat/CreateStreamedResponseDelta.php @@ -6,23 +6,33 @@ final class CreateStreamedResponseDelta { - private function __construct( - public readonly ?string $role, - public readonly ?string $content, - public readonly ?CreateStreamedResponseFunctionCall $functionCall, - ) { + /** + * @readonly + * @var string|null + */ + public $role; + /** + * @readonly + * @var string|null + */ + public $content; + /** + * @readonly + * @var \OpenAI\Responses\Chat\CreateStreamedResponseFunctionCall|null + */ + public $functionCall; + private function __construct(?string $role, ?string $content, ?CreateStreamedResponseFunctionCall $functionCall) + { + $this->role = $role; + $this->content = $content; + $this->functionCall = $functionCall; } - /** * @param array{role?: string, content?: string, function_call?: array{name?: ?string, arguments?: ?string}} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['role'] ?? null, - $attributes['content'] ?? null, - isset($attributes['function_call']) ? CreateStreamedResponseFunctionCall::from($attributes['function_call']) : null, - ); + return new self($attributes['role'] ?? null, $attributes['content'] ?? null, isset($attributes['function_call']) ? CreateStreamedResponseFunctionCall::from($attributes['function_call']) : null); } /** @@ -33,7 +43,9 @@ public function toArray(): array $data = array_filter([ 'role' => $this->role, 'content' => $this->content, - ], fn (?string $value): bool => ! is_null($value)); + ], function (?string $value) : bool { + return ! is_null($value); + }); if ($this->functionCall instanceof CreateStreamedResponseFunctionCall) { $data['content'] = null; diff --git a/src/Responses/Chat/CreateStreamedResponseFunctionCall.php b/src/Responses/Chat/CreateStreamedResponseFunctionCall.php index b05c4eb1..373dbd1f 100644 --- a/src/Responses/Chat/CreateStreamedResponseFunctionCall.php +++ b/src/Responses/Chat/CreateStreamedResponseFunctionCall.php @@ -6,21 +6,27 @@ final class CreateStreamedResponseFunctionCall { - private function __construct( - public readonly ?string $name, - public readonly ?string $arguments, - ) { + /** + * @readonly + * @var string|null + */ + public $name; + /** + * @readonly + * @var string|null + */ + public $arguments; + private function __construct(?string $name, ?string $arguments) + { + $this->name = $name; + $this->arguments = $arguments; } - /** * @param array{name?: ?string, arguments?: ?string} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['name'] ?? null, - $attributes['arguments'] ?? null, - ); + return new self($attributes['name'] ?? null, $attributes['arguments'] ?? null); } /** diff --git a/src/Responses/Completions/CreateResponse.php b/src/Responses/Completions/CreateResponse.php index 75622c27..476d287a 100644 --- a/src/Responses/Completions/CreateResponse.php +++ b/src/Responses/Completions/CreateResponse.php @@ -16,6 +16,41 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @readonly + * @var string + */ + public $model; + /** + * @var array + * @readonly + */ + public $choices; + /** + * @readonly + * @var \OpenAI\Responses\Completions\CreateResponseUsage + */ + public $usage; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, token_logprobs: array, top_logprobs: array|null, text_offset: array}|null, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> */ @@ -27,15 +62,15 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array $choices */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $created, - public readonly string $model, - public readonly array $choices, - public readonly CreateResponseUsage $usage, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, int $created, string $model, array $choices, CreateResponseUsage $usage, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->created = $created; + $this->model = $model; + $this->choices = $choices; + $this->usage = $usage; + $this->meta = $meta; } /** @@ -45,19 +80,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $choices = array_map(fn (array $result): CreateResponseChoice => CreateResponseChoice::from( - $result - ), $attributes['choices']); + $choices = array_map(function (array $result) : CreateResponseChoice { + return CreateResponseChoice::from( + $result + ); + }, $attributes['choices']); - return new self( - $attributes['id'], - $attributes['object'], - $attributes['created'], - $attributes['model'], - $choices, - CreateResponseUsage::from($attributes['usage']), - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['created'], $attributes['model'], $choices, CreateResponseUsage::from($attributes['usage']), $meta); } /** @@ -70,10 +99,9 @@ public function toArray(): array 'object' => $this->object, 'created' => $this->created, 'model' => $this->model, - 'choices' => array_map( - static fn (CreateResponseChoice $result): array => $result->toArray(), - $this->choices, - ), + 'choices' => array_map(static function (CreateResponseChoice $result) : array { + return $result->toArray(); + }, $this->choices), 'usage' => $this->usage->toArray(), ]; } diff --git a/src/Responses/Completions/CreateResponseChoice.php b/src/Responses/Completions/CreateResponseChoice.php index 300c238b..66ebb093 100644 --- a/src/Responses/Completions/CreateResponseChoice.php +++ b/src/Responses/Completions/CreateResponseChoice.php @@ -6,25 +6,39 @@ final class CreateResponseChoice { - private function __construct( - public readonly string $text, - public readonly int $index, - public readonly ?CreateResponseChoiceLogprobs $logprobs, - public readonly ?string $finishReason, - ) { + /** + * @readonly + * @var string + */ + public $text; + /** + * @readonly + * @var int + */ + public $index; + /** + * @readonly + * @var \OpenAI\Responses\Completions\CreateResponseChoiceLogprobs|null + */ + public $logprobs; + /** + * @readonly + * @var string|null + */ + public $finishReason; + private function __construct(string $text, int $index, ?CreateResponseChoiceLogprobs $logprobs, ?string $finishReason) + { + $this->text = $text; + $this->index = $index; + $this->logprobs = $logprobs; + $this->finishReason = $finishReason; } - /** * @param array{text: string, index: int, logprobs: array{tokens: array, token_logprobs: array, top_logprobs: array|null, text_offset: array}|null, finish_reason: string|null} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['text'], - $attributes['index'], - $attributes['logprobs'] ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null, - $attributes['finish_reason'], - ); + return new self($attributes['text'], $attributes['index'], $attributes['logprobs'] ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null, $attributes['finish_reason']); } /** diff --git a/src/Responses/Completions/CreateResponseChoiceLogprobs.php b/src/Responses/Completions/CreateResponseChoiceLogprobs.php index 00353b54..08254fe4 100644 --- a/src/Responses/Completions/CreateResponseChoiceLogprobs.php +++ b/src/Responses/Completions/CreateResponseChoiceLogprobs.php @@ -6,31 +6,45 @@ final class CreateResponseChoiceLogprobs { + /** + * @var array + * @readonly + */ + public $tokens; + /** + * @var array + * @readonly + */ + public $tokenLogprobs; + /** + * @var array|null + * @readonly + */ + public $topLogprobs; + /** + * @var array + * @readonly + */ + public $textOffset; /** * @param array $tokens * @param array $tokenLogprobs * @param array|null $topLogprobs * @param array $textOffset */ - private function __construct( - public readonly array $tokens, - public readonly array $tokenLogprobs, - public readonly ?array $topLogprobs, - public readonly array $textOffset, - ) { + private function __construct(array $tokens, array $tokenLogprobs, ?array $topLogprobs, array $textOffset) + { + $this->tokens = $tokens; + $this->tokenLogprobs = $tokenLogprobs; + $this->topLogprobs = $topLogprobs; + $this->textOffset = $textOffset; } - /** * @param array{tokens: array, token_logprobs: array, top_logprobs: array|null, text_offset: array} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['tokens'], - $attributes['token_logprobs'], - $attributes['top_logprobs'], - $attributes['text_offset'], - ); + return new self($attributes['tokens'], $attributes['token_logprobs'], $attributes['top_logprobs'], $attributes['text_offset']); } /** diff --git a/src/Responses/Completions/CreateResponseUsage.php b/src/Responses/Completions/CreateResponseUsage.php index 2d066fd5..62b94793 100644 --- a/src/Responses/Completions/CreateResponseUsage.php +++ b/src/Responses/Completions/CreateResponseUsage.php @@ -6,23 +6,33 @@ final class CreateResponseUsage { - private function __construct( - public readonly int $promptTokens, - public readonly ?int $completionTokens, - public readonly int $totalTokens, - ) { + /** + * @readonly + * @var int + */ + public $promptTokens; + /** + * @readonly + * @var int|null + */ + public $completionTokens; + /** + * @readonly + * @var int + */ + public $totalTokens; + private function __construct(int $promptTokens, ?int $completionTokens, int $totalTokens) + { + $this->promptTokens = $promptTokens; + $this->completionTokens = $completionTokens; + $this->totalTokens = $totalTokens; } - /** * @param array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['prompt_tokens'], - $attributes['completion_tokens'] ?? null, - $attributes['total_tokens'], - ); + return new self($attributes['prompt_tokens'], $attributes['completion_tokens'] ?? null, $attributes['total_tokens']); } /** diff --git a/src/Responses/Completions/CreateStreamedResponse.php b/src/Responses/Completions/CreateStreamedResponse.php index 029c68e9..9e5df80e 100644 --- a/src/Responses/Completions/CreateStreamedResponse.php +++ b/src/Responses/Completions/CreateStreamedResponse.php @@ -13,6 +13,31 @@ */ final class CreateStreamedResponse implements ResponseContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @readonly + * @var string + */ + public $model; + /** + * @var array + * @readonly + */ + public $choices; /** * @use ArrayAccessible, token_logprobs: array, top_logprobs: array|null, text_offset: array}|null, finish_reason: string|null}>}> */ @@ -23,13 +48,13 @@ final class CreateStreamedResponse implements ResponseContract /** * @param array $choices */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $created, - public readonly string $model, - public readonly array $choices, - ) { + private function __construct(string $id, string $object, int $created, string $model, array $choices) + { + $this->id = $id; + $this->object = $object; + $this->created = $created; + $this->model = $model; + $this->choices = $choices; } /** @@ -39,17 +64,13 @@ private function __construct( */ public static function from(array $attributes): self { - $choices = array_map(fn (array $result): CreateResponseChoice => CreateResponseChoice::from( - $result - ), $attributes['choices']); + $choices = array_map(function (array $result) : CreateResponseChoice { + return CreateResponseChoice::from( + $result + ); + }, $attributes['choices']); - return new self( - $attributes['id'], - $attributes['object'], - $attributes['created'], - $attributes['model'], - $choices, - ); + return new self($attributes['id'], $attributes['object'], $attributes['created'], $attributes['model'], $choices); } /** @@ -62,10 +83,9 @@ public function toArray(): array 'object' => $this->object, 'created' => $this->created, 'model' => $this->model, - 'choices' => array_map( - static fn (CreateResponseChoice $result): array => $result->toArray(), - $this->choices, - ), + 'choices' => array_map(static function (CreateResponseChoice $result) : array { + return $result->toArray(); + }, $this->choices), ]; } } diff --git a/src/Responses/Concerns/ArrayAccessible.php b/src/Responses/Concerns/ArrayAccessible.php index d4149864..c4ecb965 100644 --- a/src/Responses/Concerns/ArrayAccessible.php +++ b/src/Responses/Concerns/ArrayAccessible.php @@ -15,32 +15,40 @@ trait ArrayAccessible { /** * {@inheritDoc} + * @param mixed $offset */ - public function offsetExists(mixed $offset): bool + public function offsetExists($offset): bool { return array_key_exists($offset, $this->toArray()); } /** * {@inheritDoc} + * @param mixed $offset + * @return mixed */ - public function offsetGet(mixed $offset): mixed + public function offsetGet($offset) { return $this->toArray()[$offset]; } /** * {@inheritDoc} + * @return never + * @param mixed $offset + * @param mixed $value */ - public function offsetSet(mixed $offset, mixed $value): never + public function offsetSet($offset, $value) { throw new BadMethodCallException('Cannot set response attributes.'); } /** * {@inheritDoc} + * @return never + * @param mixed $offset */ - public function offsetUnset(mixed $offset): never + public function offsetUnset($offset) { throw new BadMethodCallException('Cannot unset response attributes.'); } diff --git a/src/Responses/Edits/CreateResponse.php b/src/Responses/Edits/CreateResponse.php index e8d2658e..268bfb72 100644 --- a/src/Responses/Edits/CreateResponse.php +++ b/src/Responses/Edits/CreateResponse.php @@ -16,6 +16,31 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @var array + * @readonly + */ + public $choices; + /** + * @readonly + * @var \OpenAI\Responses\Edits\CreateResponseUsage + */ + public $usage; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, usage: array{prompt_tokens: int, completion_tokens: int, total_tokens: int}}> */ @@ -27,13 +52,13 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array $choices */ - private function __construct( - public readonly string $object, - public readonly int $created, - public readonly array $choices, - public readonly CreateResponseUsage $usage, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, int $created, array $choices, CreateResponseUsage $usage, MetaInformation $meta) + { + $this->object = $object; + $this->created = $created; + $this->choices = $choices; + $this->usage = $usage; + $this->meta = $meta; } /** @@ -43,17 +68,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $choices = array_map(fn (array $result): CreateResponseChoice => CreateResponseChoice::from( - $result - ), $attributes['choices']); + $choices = array_map(function (array $result) : CreateResponseChoice { + return CreateResponseChoice::from( + $result + ); + }, $attributes['choices']); - return new self( - $attributes['object'], - $attributes['created'], - $choices, - CreateResponseUsage::from($attributes['usage']), - $meta, - ); + return new self($attributes['object'], $attributes['created'], $choices, CreateResponseUsage::from($attributes['usage']), $meta); } /** @@ -64,10 +85,9 @@ public function toArray(): array return [ 'object' => $this->object, 'created' => $this->created, - 'choices' => array_map( - static fn (CreateResponseChoice $result): array => $result->toArray(), - $this->choices, - ), + 'choices' => array_map(static function (CreateResponseChoice $result) : array { + return $result->toArray(); + }, $this->choices), 'usage' => $this->usage->toArray(), ]; } diff --git a/src/Responses/Edits/CreateResponseChoice.php b/src/Responses/Edits/CreateResponseChoice.php index 4fd67c2a..800564d6 100644 --- a/src/Responses/Edits/CreateResponseChoice.php +++ b/src/Responses/Edits/CreateResponseChoice.php @@ -6,21 +6,27 @@ final class CreateResponseChoice { - private function __construct( - public readonly string $text, - public readonly int $index, - ) { + /** + * @readonly + * @var string + */ + public $text; + /** + * @readonly + * @var int + */ + public $index; + private function __construct(string $text, int $index) + { + $this->text = $text; + $this->index = $index; } - /** * @param array{text: string, index: int} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['text'], - $attributes['index'], - ); + return new self($attributes['text'], $attributes['index']); } /** diff --git a/src/Responses/Edits/CreateResponseUsage.php b/src/Responses/Edits/CreateResponseUsage.php index 53c58810..93e28555 100644 --- a/src/Responses/Edits/CreateResponseUsage.php +++ b/src/Responses/Edits/CreateResponseUsage.php @@ -6,23 +6,33 @@ final class CreateResponseUsage { - private function __construct( - public readonly int $promptTokens, - public readonly int $completionTokens, - public readonly int $totalTokens, - ) { + /** + * @readonly + * @var int + */ + public $promptTokens; + /** + * @readonly + * @var int + */ + public $completionTokens; + /** + * @readonly + * @var int + */ + public $totalTokens; + private function __construct(int $promptTokens, int $completionTokens, int $totalTokens) + { + $this->promptTokens = $promptTokens; + $this->completionTokens = $completionTokens; + $this->totalTokens = $totalTokens; } - /** * @param array{prompt_tokens: int, completion_tokens: int, total_tokens: int} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['prompt_tokens'], - $attributes['completion_tokens'], - $attributes['total_tokens'], - ); + return new self($attributes['prompt_tokens'], $attributes['completion_tokens'], $attributes['total_tokens']); } /** diff --git a/src/Responses/Embeddings/CreateResponse.php b/src/Responses/Embeddings/CreateResponse.php index 876853a1..c005a3b5 100644 --- a/src/Responses/Embeddings/CreateResponse.php +++ b/src/Responses/Embeddings/CreateResponse.php @@ -16,6 +16,26 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $embeddings; + /** + * @readonly + * @var \OpenAI\Responses\Embeddings\CreateResponseUsage + */ + public $usage; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, index: int}>, usage: array{prompt_tokens: int, total_tokens: int}}> */ @@ -27,12 +47,12 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array $embeddings */ - private function __construct( - public readonly string $object, - public readonly array $embeddings, - public readonly CreateResponseUsage $usage, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $embeddings, CreateResponseUsage $usage, MetaInformation $meta) + { + $this->object = $object; + $this->embeddings = $embeddings; + $this->usage = $usage; + $this->meta = $meta; } /** @@ -42,16 +62,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $embeddings = array_map(fn (array $result): CreateResponseEmbedding => CreateResponseEmbedding::from( - $result - ), $attributes['data']); + $embeddings = array_map(function (array $result) : CreateResponseEmbedding { + return CreateResponseEmbedding::from( + $result + ); + }, $attributes['data']); - return new self( - $attributes['object'], - $embeddings, - CreateResponseUsage::from($attributes['usage']), - $meta, - ); + return new self($attributes['object'], $embeddings, CreateResponseUsage::from($attributes['usage']), $meta); } /** @@ -61,10 +78,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (CreateResponseEmbedding $result): array => $result->toArray(), - $this->embeddings, - ), + 'data' => array_map(static function (CreateResponseEmbedding $result) : array { + return $result->toArray(); + }, $this->embeddings), 'usage' => $this->usage->toArray(), ]; } diff --git a/src/Responses/Embeddings/CreateResponseEmbedding.php b/src/Responses/Embeddings/CreateResponseEmbedding.php index a71ecb69..5c67d9d8 100644 --- a/src/Responses/Embeddings/CreateResponseEmbedding.php +++ b/src/Responses/Embeddings/CreateResponseEmbedding.php @@ -6,26 +6,36 @@ final class CreateResponseEmbedding { + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $index; + /** + * @var array + * @readonly + */ + public $embedding; /** * @param array $embedding */ - private function __construct( - public readonly string $object, - public readonly int $index, - public readonly array $embedding, - ) { + private function __construct(string $object, int $index, array $embedding) + { + $this->object = $object; + $this->index = $index; + $this->embedding = $embedding; } - /** * @param array{object: string, index: int, embedding: array} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['object'], - $attributes['index'], - $attributes['embedding'], - ); + return new self($attributes['object'], $attributes['index'], $attributes['embedding']); } /** diff --git a/src/Responses/Embeddings/CreateResponseUsage.php b/src/Responses/Embeddings/CreateResponseUsage.php index 8faea591..367a8698 100644 --- a/src/Responses/Embeddings/CreateResponseUsage.php +++ b/src/Responses/Embeddings/CreateResponseUsage.php @@ -6,21 +6,27 @@ final class CreateResponseUsage { - private function __construct( - public readonly int $promptTokens, - public readonly int $totalTokens, - ) { + /** + * @readonly + * @var int + */ + public $promptTokens; + /** + * @readonly + * @var int + */ + public $totalTokens; + private function __construct(int $promptTokens, int $totalTokens) + { + $this->promptTokens = $promptTokens; + $this->totalTokens = $totalTokens; } - /** * @param array{prompt_tokens: int, total_tokens: int} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['prompt_tokens'], - $attributes['total_tokens'], - ); + return new self($attributes['prompt_tokens'], $attributes['total_tokens']); } /** diff --git a/src/Responses/Files/CreateResponse.php b/src/Responses/Files/CreateResponse.php index c53dcd32..4a1e791d 100644 --- a/src/Responses/Files/CreateResponse.php +++ b/src/Responses/Files/CreateResponse.php @@ -16,6 +16,51 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $bytes; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var string + */ + public $filename; + /** + * @readonly + * @var string + */ + public $purpose; + /** + * @readonly + * @var string + */ + public $status; + /** + * @var array|null + * @readonly + */ + public $statusDetails; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible|string|null}> */ @@ -27,17 +72,17 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array|null $statusDetails */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $bytes, - public readonly int $createdAt, - public readonly string $filename, - public readonly string $purpose, - public readonly string $status, - public readonly array|string|null $statusDetails, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, int $bytes, int $createdAt, string $filename, string $purpose, string $status, $statusDetails, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->bytes = $bytes; + $this->createdAt = $createdAt; + $this->filename = $filename; + $this->purpose = $purpose; + $this->status = $status; + $this->statusDetails = $statusDetails; + $this->meta = $meta; } /** @@ -47,17 +92,7 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['bytes'], - $attributes['created_at'], - $attributes['filename'], - $attributes['purpose'], - $attributes['status'], - $attributes['status_details'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['bytes'], $attributes['created_at'], $attributes['filename'], $attributes['purpose'], $attributes['status'], $attributes['status_details'], $meta); } /** diff --git a/src/Responses/Files/DeleteResponse.php b/src/Responses/Files/DeleteResponse.php index 22c47250..54648618 100644 --- a/src/Responses/Files/DeleteResponse.php +++ b/src/Responses/Files/DeleteResponse.php @@ -16,6 +16,26 @@ */ final class DeleteResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var bool + */ + public $deleted; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible */ @@ -24,12 +44,12 @@ final class DeleteResponse implements ResponseContract, ResponseHasMetaInformati use Fakeable; use HasMetaInformation; - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly bool $deleted, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, bool $deleted, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->deleted = $deleted; + $this->meta = $meta; } /** @@ -39,12 +59,7 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['deleted'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['deleted'], $meta); } /** diff --git a/src/Responses/Files/ListResponse.php b/src/Responses/Files/ListResponse.php index e23a735c..46784897 100644 --- a/src/Responses/Files/ListResponse.php +++ b/src/Responses/Files/ListResponse.php @@ -16,6 +16,21 @@ */ final class ListResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible|string|null}>}> */ @@ -27,11 +42,11 @@ final class ListResponse implements ResponseContract, ResponseHasMetaInformation /** * @param array $data */ - private function __construct( - public readonly string $object, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $data, MetaInformation $meta) + { + $this->object = $object; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,16 +56,11 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $data = array_map(fn (array $result): RetrieveResponse => RetrieveResponse::from( - $result, - $meta, - ), $attributes['data']); + $data = array_map(function (array $result) use ($meta) : RetrieveResponse { + return RetrieveResponse::from($result, $meta); + }, $attributes['data']); - return new self( - $attributes['object'], - $data, - $meta, - ); + return new self($attributes['object'], $data, $meta); } /** @@ -60,10 +70,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (RetrieveResponse $response): array => $response->toArray(), - $this->data, - ), + 'data' => array_map(static function (RetrieveResponse $response) : array { + return $response->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/Files/RetrieveResponse.php b/src/Responses/Files/RetrieveResponse.php index a7677019..c7b21458 100644 --- a/src/Responses/Files/RetrieveResponse.php +++ b/src/Responses/Files/RetrieveResponse.php @@ -16,6 +16,51 @@ */ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $bytes; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var string + */ + public $filename; + /** + * @readonly + * @var string + */ + public $purpose; + /** + * @readonly + * @var string + */ + public $status; + /** + * @var array|null + * @readonly + */ + public $statusDetails; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible|string|null}> */ @@ -27,17 +72,17 @@ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInforma /** * @param array|null $statusDetails */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $bytes, - public readonly int $createdAt, - public readonly string $filename, - public readonly string $purpose, - public readonly string $status, - public readonly array|string|null $statusDetails, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, int $bytes, int $createdAt, string $filename, string $purpose, string $status, $statusDetails, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->bytes = $bytes; + $this->createdAt = $createdAt; + $this->filename = $filename; + $this->purpose = $purpose; + $this->status = $status; + $this->statusDetails = $statusDetails; + $this->meta = $meta; } /** @@ -47,17 +92,7 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['bytes'], - $attributes['created_at'], - $attributes['filename'], - $attributes['purpose'], - $attributes['status'], - $attributes['status_details'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['bytes'], $attributes['created_at'], $attributes['filename'], $attributes['purpose'], $attributes['status'], $attributes['status_details'], $meta); } /** diff --git a/src/Responses/FineTunes/ListEventsResponse.php b/src/Responses/FineTunes/ListEventsResponse.php index 77b95b41..25584a5d 100644 --- a/src/Responses/FineTunes/ListEventsResponse.php +++ b/src/Responses/FineTunes/ListEventsResponse.php @@ -16,6 +16,21 @@ */ final class ListEventsResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible}> */ @@ -27,11 +42,11 @@ final class ListEventsResponse implements ResponseContract, ResponseHasMetaInfor /** * @param array $data */ - private function __construct( - public readonly string $object, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $data, MetaInformation $meta) + { + $this->object = $object; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,15 +56,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $data = array_map(fn (array $result): RetrieveResponseEvent => RetrieveResponseEvent::from( - $result - ), $attributes['data']); + $data = array_map(function (array $result) : RetrieveResponseEvent { + return RetrieveResponseEvent::from( + $result + ); + }, $attributes['data']); - return new self( - $attributes['object'], - $data, - $meta, - ); + return new self($attributes['object'], $data, $meta); } /** @@ -59,10 +72,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (RetrieveResponseEvent $response): array => $response->toArray(), - $this->data, - ), + 'data' => array_map(static function (RetrieveResponseEvent $response) : array { + return $response->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/FineTunes/ListResponse.php b/src/Responses/FineTunes/ListResponse.php index fc7d0079..fc7eb5e7 100644 --- a/src/Responses/FineTunes/ListResponse.php +++ b/src/Responses/FineTunes/ListResponse.php @@ -16,6 +16,21 @@ */ final class ListResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, fine_tuned_model: ?string, hyperparams: array{batch_size: ?int, learning_rate_multiplier: ?float, n_epochs: int, prompt_loss_weight: float}, organization_id: string, result_files: array|string|null}>, status: string, validation_files: array|string|null}>, training_files: array|string|null}>, updated_at: int}>}> */ @@ -27,11 +42,11 @@ final class ListResponse implements ResponseContract, ResponseHasMetaInformation /** * @param array $data */ - private function __construct( - public readonly string $object, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $data, MetaInformation $meta) + { + $this->object = $object; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,16 +56,11 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $data = array_map(fn (array $result): RetrieveResponse => RetrieveResponse::from( - $result, - $meta, - ), $attributes['data']); + $data = array_map(function (array $result) use ($meta) : RetrieveResponse { + return RetrieveResponse::from($result, $meta); + }, $attributes['data']); - return new self( - $attributes['object'], - $data, - $meta, - ); + return new self($attributes['object'], $data, $meta); } /** @@ -60,10 +70,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (RetrieveResponse $response): array => $response->toArray(), - $this->data, - ), + 'data' => array_map(static function (RetrieveResponse $response) : array { + return $response->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/FineTunes/RetrieveResponse.php b/src/Responses/FineTunes/RetrieveResponse.php index c4a6abf3..630e4e14 100644 --- a/src/Responses/FineTunes/RetrieveResponse.php +++ b/src/Responses/FineTunes/RetrieveResponse.php @@ -16,6 +16,76 @@ */ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var string + */ + public $model; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @var array + * @readonly + */ + public $events; + /** + * @readonly + * @var string|null + */ + public $fineTunedModel; + /** + * @readonly + * @var \OpenAI\Responses\FineTunes\RetrieveResponseHyperparams + */ + public $hyperparams; + /** + * @readonly + * @var string + */ + public $organizationId; + /** + * @var array + * @readonly + */ + public $resultFiles; + /** + * @readonly + * @var string + */ + public $status; + /** + * @var array + * @readonly + */ + public $validationFiles; + /** + * @var array + * @readonly + */ + public $trainingFiles; + /** + * @readonly + * @var int + */ + public $updatedAt; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, fine_tuned_model: ?string, hyperparams: array{batch_size: ?int, learning_rate_multiplier: ?float, n_epochs: int, prompt_loss_weight: float}, organization_id: string, result_files: array|string|null}>, status: string, validation_files: array|string|null}>, training_files: array|string|null}>, updated_at: int}> */ @@ -30,22 +100,22 @@ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInforma * @param array $validationFiles * @param array $trainingFiles */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly string $model, - public readonly int $createdAt, - public readonly array $events, - public readonly ?string $fineTunedModel, - public readonly RetrieveResponseHyperparams $hyperparams, - public readonly string $organizationId, - public readonly array $resultFiles, - public readonly string $status, - public readonly array $validationFiles, - public readonly array $trainingFiles, - public readonly int $updatedAt, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, string $model, int $createdAt, array $events, ?string $fineTunedModel, RetrieveResponseHyperparams $hyperparams, string $organizationId, array $resultFiles, string $status, array $validationFiles, array $trainingFiles, int $updatedAt, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->model = $model; + $this->createdAt = $createdAt; + $this->events = $events; + $this->fineTunedModel = $fineTunedModel; + $this->hyperparams = $hyperparams; + $this->organizationId = $organizationId; + $this->resultFiles = $resultFiles; + $this->status = $status; + $this->validationFiles = $validationFiles; + $this->trainingFiles = $trainingFiles; + $this->updatedAt = $updatedAt; + $this->meta = $meta; } /** @@ -55,38 +125,31 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $events = array_map(fn (array $result): RetrieveResponseEvent => RetrieveResponseEvent::from( - $result - ), $attributes['events'] ?? []); + $events = array_map(function (array $result) : RetrieveResponseEvent { + return RetrieveResponseEvent::from( + $result + ); + }, $attributes['events'] ?? []); - $resultFiles = array_map(fn (array $result): RetrieveResponseFile => RetrieveResponseFile::from( - $result - ), $attributes['result_files']); + $resultFiles = array_map(function (array $result) : RetrieveResponseFile { + return RetrieveResponseFile::from( + $result + ); + }, $attributes['result_files']); - $validationFiles = array_map(fn (array $result): RetrieveResponseFile => RetrieveResponseFile::from( - $result - ), $attributes['validation_files']); + $validationFiles = array_map(function (array $result) : RetrieveResponseFile { + return RetrieveResponseFile::from( + $result + ); + }, $attributes['validation_files']); - $trainingFiles = array_map(fn (array $result): RetrieveResponseFile => RetrieveResponseFile::from( - $result - ), $attributes['training_files']); + $trainingFiles = array_map(function (array $result) : RetrieveResponseFile { + return RetrieveResponseFile::from( + $result + ); + }, $attributes['training_files']); - return new self( - $attributes['id'], - $attributes['object'], - $attributes['model'], - $attributes['created_at'], - $events, - $attributes['fine_tuned_model'], - RetrieveResponseHyperparams::from($attributes['hyperparams']), - $attributes['organization_id'], - $resultFiles, - $attributes['status'], - $validationFiles, - $trainingFiles, - $attributes['updated_at'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['model'], $attributes['created_at'], $events, $attributes['fine_tuned_model'], RetrieveResponseHyperparams::from($attributes['hyperparams']), $attributes['organization_id'], $resultFiles, $attributes['status'], $validationFiles, $trainingFiles, $attributes['updated_at'], $meta); } /** @@ -100,23 +163,31 @@ public function toArray(): array 'model' => $this->model, 'created_at' => $this->createdAt, 'events' => array_map( - static fn (RetrieveResponseEvent $result): array => $result->toArray(), + static function (RetrieveResponseEvent $result) : array { + return $result->toArray(); + }, $this->events ), 'fine_tuned_model' => $this->fineTunedModel, 'hyperparams' => $this->hyperparams->toArray(), 'organization_id' => $this->organizationId, 'result_files' => array_map( - static fn (RetrieveResponseFile $result): array => $result->toArray(), + static function (RetrieveResponseFile $result) : array { + return $result->toArray(); + }, $this->resultFiles ), 'status' => $this->status, 'validation_files' => array_map( - static fn (RetrieveResponseFile $result): array => $result->toArray(), + static function (RetrieveResponseFile $result) : array { + return $result->toArray(); + }, $this->validationFiles ), 'training_files' => array_map( - static fn (RetrieveResponseFile $result): array => $result->toArray(), + static function (RetrieveResponseFile $result) : array { + return $result->toArray(); + }, $this->trainingFiles ), 'updated_at' => $this->updatedAt, diff --git a/src/Responses/FineTunes/RetrieveResponseEvent.php b/src/Responses/FineTunes/RetrieveResponseEvent.php index b0c55904..560f7df3 100644 --- a/src/Responses/FineTunes/RetrieveResponseEvent.php +++ b/src/Responses/FineTunes/RetrieveResponseEvent.php @@ -12,17 +12,37 @@ */ final class RetrieveResponseEvent implements ResponseContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var string + */ + public $level; + /** + * @readonly + * @var string + */ + public $message; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly string $object, - public readonly int $createdAt, - public readonly string $level, - public readonly string $message, - ) { + private function __construct(string $object, int $createdAt, string $level, string $message) + { + $this->object = $object; + $this->createdAt = $createdAt; + $this->level = $level; + $this->message = $message; } /** @@ -32,12 +52,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['object'], - $attributes['created_at'], - $attributes['level'], - $attributes['message'], - ); + return new self($attributes['object'], $attributes['created_at'], $attributes['level'], $attributes['message']); } /** diff --git a/src/Responses/FineTunes/RetrieveResponseFile.php b/src/Responses/FineTunes/RetrieveResponseFile.php index f2297202..c60abe17 100644 --- a/src/Responses/FineTunes/RetrieveResponseFile.php +++ b/src/Responses/FineTunes/RetrieveResponseFile.php @@ -12,6 +12,46 @@ */ final class RetrieveResponseFile implements ResponseContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $bytes; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var string + */ + public $filename; + /** + * @readonly + * @var string + */ + public $purpose; + /** + * @readonly + * @var string + */ + public $status; + /** + * @var array|null + * @readonly + */ + public $statusDetails; /** * @use ArrayAccessible|string|null}> */ @@ -20,16 +60,16 @@ final class RetrieveResponseFile implements ResponseContract /** * @param array|null $statusDetails */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $bytes, - public readonly int $createdAt, - public readonly string $filename, - public readonly string $purpose, - public readonly string $status, - public readonly array|string|null $statusDetails, - ) { + private function __construct(string $id, string $object, int $bytes, int $createdAt, string $filename, string $purpose, string $status, $statusDetails) + { + $this->id = $id; + $this->object = $object; + $this->bytes = $bytes; + $this->createdAt = $createdAt; + $this->filename = $filename; + $this->purpose = $purpose; + $this->status = $status; + $this->statusDetails = $statusDetails; } /** @@ -39,16 +79,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['bytes'], - $attributes['created_at'], - $attributes['filename'], - $attributes['purpose'], - $attributes['status'], - $attributes['status_details'], - ); + return new self($attributes['id'], $attributes['object'], $attributes['bytes'], $attributes['created_at'], $attributes['filename'], $attributes['purpose'], $attributes['status'], $attributes['status_details']); } /** diff --git a/src/Responses/FineTunes/RetrieveResponseHyperparams.php b/src/Responses/FineTunes/RetrieveResponseHyperparams.php index c6b13ec7..7559a6f3 100644 --- a/src/Responses/FineTunes/RetrieveResponseHyperparams.php +++ b/src/Responses/FineTunes/RetrieveResponseHyperparams.php @@ -12,17 +12,37 @@ */ final class RetrieveResponseHyperparams implements ResponseContract { + /** + * @readonly + * @var int|null + */ + public $batchSize; + /** + * @readonly + * @var float|null + */ + public $learningRateMultiplier; + /** + * @readonly + * @var int + */ + public $nEpochs; + /** + * @readonly + * @var float + */ + public $promptLossWeight; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly ?int $batchSize, - public readonly ?float $learningRateMultiplier, - public readonly int $nEpochs, - public readonly float $promptLossWeight, - ) { + private function __construct(?int $batchSize, ?float $learningRateMultiplier, int $nEpochs, float $promptLossWeight) + { + $this->batchSize = $batchSize; + $this->learningRateMultiplier = $learningRateMultiplier; + $this->nEpochs = $nEpochs; + $this->promptLossWeight = $promptLossWeight; } /** @@ -32,12 +52,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['batch_size'], - $attributes['learning_rate_multiplier'], - $attributes['n_epochs'], - $attributes['prompt_loss_weight'], - ); + return new self($attributes['batch_size'], $attributes['learning_rate_multiplier'], $attributes['n_epochs'], $attributes['prompt_loss_weight']); } /** diff --git a/src/Responses/FineTunes/RetrieveStreamedResponseEvent.php b/src/Responses/FineTunes/RetrieveStreamedResponseEvent.php index b5cb3573..ff692d86 100644 --- a/src/Responses/FineTunes/RetrieveStreamedResponseEvent.php +++ b/src/Responses/FineTunes/RetrieveStreamedResponseEvent.php @@ -13,6 +13,26 @@ */ final class RetrieveStreamedResponseEvent implements ResponseContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var string + */ + public $level; + /** + * @readonly + * @var string + */ + public $message; /** * @use ArrayAccessible */ @@ -20,12 +40,12 @@ final class RetrieveStreamedResponseEvent implements ResponseContract use FakeableForStreamedResponse; - private function __construct( - public readonly string $object, - public readonly int $createdAt, - public readonly string $level, - public readonly string $message, - ) { + private function __construct(string $object, int $createdAt, string $level, string $message) + { + $this->object = $object; + $this->createdAt = $createdAt; + $this->level = $level; + $this->message = $message; } /** @@ -35,12 +55,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['object'], - $attributes['created_at'], - $attributes['level'], - $attributes['message'], - ); + return new self($attributes['object'], $attributes['created_at'], $attributes['level'], $attributes['message']); } /** diff --git a/src/Responses/FineTuning/ListJobEventsResponse.php b/src/Responses/FineTuning/ListJobEventsResponse.php index bf47b75b..e4c6d825 100644 --- a/src/Responses/FineTuning/ListJobEventsResponse.php +++ b/src/Responses/FineTuning/ListJobEventsResponse.php @@ -16,6 +16,26 @@ */ final class ListJobEventsResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var bool + */ + public $hasMore; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, has_more: bool}> */ @@ -27,12 +47,12 @@ final class ListJobEventsResponse implements ResponseContract, ResponseHasMetaIn /** * @param array $data */ - private function __construct( - public readonly string $object, - public readonly array $data, - public readonly bool $hasMore, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $data, bool $hasMore, MetaInformation $meta) + { + $this->object = $object; + $this->data = $data; + $this->hasMore = $hasMore; + $this->meta = $meta; } /** @@ -42,16 +62,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $data = array_map(fn (array $result): ListJobEventsResponseEvent => ListJobEventsResponseEvent::from( - $result - ), $attributes['data']); + $data = array_map(function (array $result) : ListJobEventsResponseEvent { + return ListJobEventsResponseEvent::from( + $result + ); + }, $attributes['data']); - return new self( - $attributes['object'], - $data, - $attributes['has_more'], - $meta, - ); + return new self($attributes['object'], $data, $attributes['has_more'], $meta); } /** @@ -61,10 +78,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (ListJobEventsResponseEvent $response): array => $response->toArray(), - $this->data, - ), + 'data' => array_map(static function (ListJobEventsResponseEvent $response) : array { + return $response->toArray(); + }, $this->data), 'has_more' => $this->hasMore, ]; } diff --git a/src/Responses/FineTuning/ListJobEventsResponseEvent.php b/src/Responses/FineTuning/ListJobEventsResponseEvent.php index 6dc97091..c55d0fc4 100644 --- a/src/Responses/FineTuning/ListJobEventsResponseEvent.php +++ b/src/Responses/FineTuning/ListJobEventsResponseEvent.php @@ -13,20 +13,55 @@ */ final class ListJobEventsResponseEvent implements ResponseContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var \OpenAI\Enums\FineTuning\FineTuningEventLevel + */ + public $level; + /** + * @readonly + * @var string + */ + public $message; + /** + * @readonly + * @var \OpenAI\Responses\FineTuning\ListJobEventsResponseEventData|null + */ + public $data; + /** + * @readonly + * @var string + */ + public $type; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly string $object, - public readonly string $id, - public readonly int $createdAt, - public readonly FineTuningEventLevel $level, - public readonly string $message, - public readonly ?ListJobEventsResponseEventData $data, - public readonly string $type, - ) { + private function __construct(string $object, string $id, int $createdAt, FineTuningEventLevel $level, string $message, ?ListJobEventsResponseEventData $data, string $type) + { + $this->object = $object; + $this->id = $id; + $this->createdAt = $createdAt; + $this->level = $level; + $this->message = $message; + $this->data = $data; + $this->type = $type; } /** @@ -36,15 +71,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['object'], - $attributes['id'], - $attributes['created_at'], - FineTuningEventLevel::from($attributes['level']), - $attributes['message'], - $attributes['data'] ? ListJobEventsResponseEventData::from($attributes['data']) : null, - $attributes['type'], - ); + return new self($attributes['object'], $attributes['id'], $attributes['created_at'], FineTuningEventLevel::from($attributes['level']), $attributes['message'], $attributes['data'] ? ListJobEventsResponseEventData::from($attributes['data']) : null, $attributes['type']); } /** @@ -58,7 +85,7 @@ public function toArray(): array 'created_at' => $this->createdAt, 'level' => $this->level->value, 'message' => $this->message, - 'data' => $this->data?->toArray(), + 'data' => ($nullsafeVariable1 = $this->data) ? $nullsafeVariable1->toArray() : null, 'type' => $this->type, ]; } diff --git a/src/Responses/FineTuning/ListJobEventsResponseEventData.php b/src/Responses/FineTuning/ListJobEventsResponseEventData.php index 4f907c9b..6eb5e15f 100644 --- a/src/Responses/FineTuning/ListJobEventsResponseEventData.php +++ b/src/Responses/FineTuning/ListJobEventsResponseEventData.php @@ -12,16 +12,31 @@ */ final class ListJobEventsResponseEventData implements ResponseContract { + /** + * @readonly + * @var int + */ + public $step; + /** + * @readonly + * @var float + */ + public $trainLoss; + /** + * @readonly + * @var float + */ + public $trainMeanTokenAccuracy; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly int $step, - public readonly float $trainLoss, - public readonly float $trainMeanTokenAccuracy, - ) { + private function __construct(int $step, float $trainLoss, float $trainMeanTokenAccuracy) + { + $this->step = $step; + $this->trainLoss = $trainLoss; + $this->trainMeanTokenAccuracy = $trainMeanTokenAccuracy; } /** @@ -31,11 +46,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['step'], - $attributes['train_loss'], - $attributes['train_mean_token_accuracy'], - ); + return new self($attributes['step'], $attributes['train_loss'], $attributes['train_mean_token_accuracy']); } /** diff --git a/src/Responses/FineTuning/ListJobsResponse.php b/src/Responses/FineTuning/ListJobsResponse.php index 8b2ea959..7bc1c239 100644 --- a/src/Responses/FineTuning/ListJobsResponse.php +++ b/src/Responses/FineTuning/ListJobsResponse.php @@ -16,6 +16,26 @@ */ final class ListJobsResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var bool + */ + public $hasMore; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}>, has_more: bool}> */ @@ -27,12 +47,12 @@ final class ListJobsResponse implements ResponseContract, ResponseHasMetaInforma /** * @param array $data */ - private function __construct( - public readonly string $object, - public readonly array $data, - public readonly bool $hasMore, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $data, bool $hasMore, MetaInformation $meta) + { + $this->object = $object; + $this->data = $data; + $this->hasMore = $hasMore; + $this->meta = $meta; } /** @@ -42,17 +62,11 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $data = array_map(fn (array $result): RetrieveJobResponse => RetrieveJobResponse::from( - $result, - $meta, - ), $attributes['data']); + $data = array_map(function (array $result) use ($meta) : RetrieveJobResponse { + return RetrieveJobResponse::from($result, $meta); + }, $attributes['data']); - return new self( - $attributes['object'], - $data, - $attributes['has_more'], - $meta, - ); + return new self($attributes['object'], $data, $attributes['has_more'], $meta); } /** @@ -62,10 +76,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (RetrieveJobResponse $response): array => $response->toArray(), - $this->data, - ), + 'data' => array_map(static function (RetrieveJobResponse $response) : array { + return $response->toArray(); + }, $this->data), 'has_more' => $this->hasMore, ]; } diff --git a/src/Responses/FineTuning/RetrieveJobResponse.php b/src/Responses/FineTuning/RetrieveJobResponse.php index 0e5900f6..be097ca7 100644 --- a/src/Responses/FineTuning/RetrieveJobResponse.php +++ b/src/Responses/FineTuning/RetrieveJobResponse.php @@ -16,6 +16,76 @@ */ final class RetrieveJobResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var string + */ + public $model; + /** + * @readonly + * @var int + */ + public $createdAt; + /** + * @readonly + * @var int|null + */ + public $finishedAt; + /** + * @readonly + * @var string|null + */ + public $fineTunedModel; + /** + * @readonly + * @var \OpenAI\Responses\FineTuning\RetrieveJobResponseHyperparameters + */ + public $hyperparameters; + /** + * @readonly + * @var string + */ + public $organizationId; + /** + * @var array + * @readonly + */ + public $resultFiles; + /** + * @readonly + * @var string + */ + public $status; + /** + * @readonly + * @var string|null + */ + public $validationFile; + /** + * @readonly + * @var string + */ + public $trainingFile; + /** + * @readonly + * @var int|null + */ + public $trainedTokens; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, status: string, validation_file: ?string, training_file: string, trained_tokens: ?int}> */ @@ -27,22 +97,22 @@ final class RetrieveJobResponse implements ResponseContract, ResponseHasMetaInfo /** * @param array $resultFiles */ - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly string $model, - public readonly int $createdAt, - public readonly ?int $finishedAt, - public readonly ?string $fineTunedModel, - public readonly RetrieveJobResponseHyperparameters $hyperparameters, - public readonly string $organizationId, - public readonly array $resultFiles, - public readonly string $status, - public readonly ?string $validationFile, - public readonly string $trainingFile, - public readonly ?int $trainedTokens, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, string $model, int $createdAt, ?int $finishedAt, ?string $fineTunedModel, RetrieveJobResponseHyperparameters $hyperparameters, string $organizationId, array $resultFiles, string $status, ?string $validationFile, string $trainingFile, ?int $trainedTokens, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->model = $model; + $this->createdAt = $createdAt; + $this->finishedAt = $finishedAt; + $this->fineTunedModel = $fineTunedModel; + $this->hyperparameters = $hyperparameters; + $this->organizationId = $organizationId; + $this->resultFiles = $resultFiles; + $this->status = $status; + $this->validationFile = $validationFile; + $this->trainingFile = $trainingFile; + $this->trainedTokens = $trainedTokens; + $this->meta = $meta; } /** @@ -52,22 +122,7 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['model'], - $attributes['created_at'], - $attributes['finished_at'], - $attributes['fine_tuned_model'], - RetrieveJobResponseHyperparameters::from($attributes['hyperparameters']), - $attributes['organization_id'], - $attributes['result_files'], - $attributes['status'], - $attributes['validation_file'], - $attributes['training_file'], - $attributes['trained_tokens'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['model'], $attributes['created_at'], $attributes['finished_at'], $attributes['fine_tuned_model'], RetrieveJobResponseHyperparameters::from($attributes['hyperparameters']), $attributes['organization_id'], $attributes['result_files'], $attributes['status'], $attributes['validation_file'], $attributes['training_file'], $attributes['trained_tokens'], $meta); } /** diff --git a/src/Responses/FineTuning/RetrieveJobResponseHyperparameters.php b/src/Responses/FineTuning/RetrieveJobResponseHyperparameters.php index 188b5157..0de31a30 100644 --- a/src/Responses/FineTuning/RetrieveJobResponseHyperparameters.php +++ b/src/Responses/FineTuning/RetrieveJobResponseHyperparameters.php @@ -12,14 +12,22 @@ */ final class RetrieveJobResponseHyperparameters implements ResponseContract { + /** + * @readonly + * @var int|string + */ + public $nEpochs; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly int|string $nEpochs, - ) { + /** + * @param int|string $nEpochs + */ + private function __construct($nEpochs) + { + $this->nEpochs = $nEpochs; } /** @@ -29,9 +37,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['n_epochs'], - ); + return new self($attributes['n_epochs']); } /** diff --git a/src/Responses/Images/CreateResponse.php b/src/Responses/Images/CreateResponse.php index 0a9bbed5..d6ac1c4c 100644 --- a/src/Responses/Images/CreateResponse.php +++ b/src/Responses/Images/CreateResponse.php @@ -16,6 +16,21 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var int + */ + public $created; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible}> */ @@ -27,11 +42,11 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array $data */ - private function __construct( - public readonly int $created, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(int $created, array $data, MetaInformation $meta) + { + $this->created = $created; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,15 +56,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $results = array_map(fn (array $result): CreateResponseData => CreateResponseData::from( - $result - ), $attributes['data']); + $results = array_map(function (array $result) : CreateResponseData { + return CreateResponseData::from( + $result + ); + }, $attributes['data']); - return new self( - $attributes['created'], - $results, - $meta, - ); + return new self($attributes['created'], $results, $meta); } /** @@ -59,10 +72,9 @@ public function toArray(): array { return [ 'created' => $this->created, - 'data' => array_map( - static fn (CreateResponseData $result): array => $result->toArray(), - $this->data, - ), + 'data' => array_map(static function (CreateResponseData $result) : array { + return $result->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/Images/CreateResponseData.php b/src/Responses/Images/CreateResponseData.php index e884c1df..f5f18ba2 100644 --- a/src/Responses/Images/CreateResponseData.php +++ b/src/Responses/Images/CreateResponseData.php @@ -12,15 +12,25 @@ */ final class CreateResponseData implements ResponseContract { + /** + * @readonly + * @var string + */ + public $url = ''; + /** + * @readonly + * @var string + */ + public $b64_json = ''; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly string $url = '', - public readonly string $b64_json = '', - ) { + private function __construct(string $url = '', string $b64_json = '') + { + $this->url = $url; + $this->b64_json = $b64_json; } /** @@ -30,10 +40,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['url'] ?? '', - $attributes['b64_json'] ?? '', - ); + return new self($attributes['url'] ?? '', $attributes['b64_json'] ?? ''); } /** diff --git a/src/Responses/Images/EditResponse.php b/src/Responses/Images/EditResponse.php index b1e0b8fd..65f4d7bc 100644 --- a/src/Responses/Images/EditResponse.php +++ b/src/Responses/Images/EditResponse.php @@ -16,6 +16,21 @@ */ final class EditResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var int + */ + public $created; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible}> */ @@ -27,11 +42,11 @@ final class EditResponse implements ResponseContract, ResponseHasMetaInformation /** * @param array $data */ - private function __construct( - public readonly int $created, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(int $created, array $data, MetaInformation $meta) + { + $this->created = $created; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,15 +56,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $results = array_map(fn (array $result): EditResponseData => EditResponseData::from( - $result - ), $attributes['data']); + $results = array_map(function (array $result) : EditResponseData { + return EditResponseData::from( + $result + ); + }, $attributes['data']); - return new self( - $attributes['created'], - $results, - $meta, - ); + return new self($attributes['created'], $results, $meta); } /** @@ -59,10 +72,9 @@ public function toArray(): array { return [ 'created' => $this->created, - 'data' => array_map( - static fn (EditResponseData $result): array => $result->toArray(), - $this->data, - ), + 'data' => array_map(static function (EditResponseData $result) : array { + return $result->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/Images/EditResponseData.php b/src/Responses/Images/EditResponseData.php index e6528442..5db378d4 100644 --- a/src/Responses/Images/EditResponseData.php +++ b/src/Responses/Images/EditResponseData.php @@ -12,15 +12,25 @@ */ final class EditResponseData implements ResponseContract { + /** + * @readonly + * @var string + */ + public $url = ''; + /** + * @readonly + * @var string + */ + public $b64_json = ''; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly string $url = '', - public readonly string $b64_json = '', - ) { + private function __construct(string $url = '', string $b64_json = '') + { + $this->url = $url; + $this->b64_json = $b64_json; } /** @@ -30,10 +40,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['url'] ?? '', - $attributes['b64_json'] ?? '', - ); + return new self($attributes['url'] ?? '', $attributes['b64_json'] ?? ''); } /** diff --git a/src/Responses/Images/VariationResponse.php b/src/Responses/Images/VariationResponse.php index c4f79084..616297c6 100644 --- a/src/Responses/Images/VariationResponse.php +++ b/src/Responses/Images/VariationResponse.php @@ -16,6 +16,21 @@ */ final class VariationResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var int + */ + public $created; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible}> */ @@ -27,11 +42,11 @@ final class VariationResponse implements ResponseContract, ResponseHasMetaInform /** * @param array $data */ - private function __construct( - public readonly int $created, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(int $created, array $data, MetaInformation $meta) + { + $this->created = $created; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,15 +56,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $results = array_map(fn (array $result): VariationResponseData => VariationResponseData::from( - $result - ), $attributes['data']); + $results = array_map(function (array $result) : VariationResponseData { + return VariationResponseData::from( + $result + ); + }, $attributes['data']); - return new self( - $attributes['created'], - $results, - $meta, - ); + return new self($attributes['created'], $results, $meta); } /** @@ -59,10 +72,9 @@ public function toArray(): array { return [ 'created' => $this->created, - 'data' => array_map( - static fn (VariationResponseData $result): array => $result->toArray(), - $this->data, - ), + 'data' => array_map(static function (VariationResponseData $result) : array { + return $result->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/Images/VariationResponseData.php b/src/Responses/Images/VariationResponseData.php index 89615ea2..bee4862c 100644 --- a/src/Responses/Images/VariationResponseData.php +++ b/src/Responses/Images/VariationResponseData.php @@ -12,15 +12,25 @@ */ final class VariationResponseData implements ResponseContract { + /** + * @readonly + * @var string + */ + public $url = ''; + /** + * @readonly + * @var string + */ + public $b64_json = ''; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public readonly string $url = '', - public readonly string $b64_json = '', - ) { + private function __construct(string $url = '', string $b64_json = '') + { + $this->url = $url; + $this->b64_json = $b64_json; } /** @@ -30,10 +40,7 @@ private function __construct( */ public static function from(array $attributes): self { - return new self( - $attributes['url'] ?? '', - $attributes['b64_json'] ?? '', - ); + return new self($attributes['url'] ?? '', $attributes['b64_json'] ?? ''); } /** diff --git a/src/Responses/Meta/MetaInformation.php b/src/Responses/Meta/MetaInformation.php index 686b0382..ef53fe9c 100644 --- a/src/Responses/Meta/MetaInformation.php +++ b/src/Responses/Meta/MetaInformation.php @@ -10,17 +10,36 @@ */ final class MetaInformation implements MetaInformationContract { + /** + * @var string + */ + public $requestId; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformationOpenAI + */ + public $openai; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformationRateLimit|null + */ + public $requestLimit; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformationRateLimit|null + */ + public $tokenLimit; /** * @use ArrayAccessible */ use ArrayAccessible; - private function __construct( - public string $requestId, - public readonly MetaInformationOpenAI $openai, - public readonly ?MetaInformationRateLimit $requestLimit, - public readonly ?MetaInformationRateLimit $tokenLimit, - ) { + private function __construct(string $requestId, MetaInformationOpenAI $openai, ?MetaInformationRateLimit $requestLimit, ?MetaInformationRateLimit $tokenLimit) + { + $this->requestId = $requestId; + $this->openai = $openai; + $this->requestLimit = $requestLimit; + $this->tokenLimit = $tokenLimit; } /** @@ -57,12 +76,7 @@ public static function from(array $headers): self $tokenLimit = null; } - return new self( - $requestId, - $openai, - $requestLimit, - $tokenLimit, - ); + return new self($requestId, $openai, $requestLimit, $tokenLimit); } /** @@ -82,6 +96,8 @@ public function toArray(): array 'x-ratelimit-reset-requests' => $this->requestLimit->reset ?? null, 'x-ratelimit-reset-tokens' => $this->tokenLimit->reset ?? null, 'x-request-id' => $this->requestId, - ], fn (string|int|null $value): bool => ! is_null($value)); + ], function ($value) : bool { + return ! is_null($value); + }); } } diff --git a/src/Responses/Meta/MetaInformationOpenAI.php b/src/Responses/Meta/MetaInformationOpenAI.php index e9f72e83..864929df 100644 --- a/src/Responses/Meta/MetaInformationOpenAI.php +++ b/src/Responses/Meta/MetaInformationOpenAI.php @@ -4,24 +4,38 @@ final class MetaInformationOpenAI { - public function __construct( - public readonly ?string $model, - public readonly ?string $organization, - public readonly ?string $version, - public readonly int $processingMs, - ) { + /** + * @readonly + * @var string|null + */ + public $model; + /** + * @readonly + * @var string|null + */ + public $organization; + /** + * @readonly + * @var string|null + */ + public $version; + /** + * @readonly + * @var int + */ + public $processingMs; + public function __construct(?string $model, ?string $organization, ?string $version, int $processingMs) + { + $this->model = $model; + $this->organization = $organization; + $this->version = $version; + $this->processingMs = $processingMs; } - /** * @param array{model: ?string, organization: ?string, version: ?string, processingMs: int} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['model'], - $attributes['organization'], - $attributes['version'], - $attributes['processingMs'], - ); + return new self($attributes['model'], $attributes['organization'], $attributes['version'], $attributes['processingMs']); } } diff --git a/src/Responses/Meta/MetaInformationRateLimit.php b/src/Responses/Meta/MetaInformationRateLimit.php index 52848f7a..4715322a 100644 --- a/src/Responses/Meta/MetaInformationRateLimit.php +++ b/src/Responses/Meta/MetaInformationRateLimit.php @@ -4,22 +4,32 @@ final class MetaInformationRateLimit { - public function __construct( - public readonly int $limit, - public readonly int $remaining, - public readonly string $reset, - ) { + /** + * @readonly + * @var int + */ + public $limit; + /** + * @readonly + * @var int + */ + public $remaining; + /** + * @readonly + * @var string + */ + public $reset; + public function __construct(int $limit, int $remaining, string $reset) + { + $this->limit = $limit; + $this->remaining = $remaining; + $this->reset = $reset; } - /** * @param array{limit: int, remaining: int, reset: string} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['limit'], - $attributes['remaining'], - $attributes['reset'], - ); + return new self($attributes['limit'], $attributes['remaining'], $attributes['reset']); } } diff --git a/src/Responses/Models/DeleteResponse.php b/src/Responses/Models/DeleteResponse.php index dea13183..45305eb8 100644 --- a/src/Responses/Models/DeleteResponse.php +++ b/src/Responses/Models/DeleteResponse.php @@ -16,6 +16,26 @@ */ final class DeleteResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var bool + */ + public $deleted; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible */ @@ -24,12 +44,12 @@ final class DeleteResponse implements ResponseContract, ResponseHasMetaInformati use Fakeable; use HasMetaInformation; - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly bool $deleted, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $object, bool $deleted, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->deleted = $deleted; + $this->meta = $meta; } /** @@ -39,12 +59,7 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['deleted'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['deleted'], $meta); } /** diff --git a/src/Responses/Models/ListResponse.php b/src/Responses/Models/ListResponse.php index 2ccb8307..93bbc2ab 100644 --- a/src/Responses/Models/ListResponse.php +++ b/src/Responses/Models/ListResponse.php @@ -16,6 +16,21 @@ */ final class ListResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $object; + /** + * @var array + * @readonly + */ + public $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, root: string, parent: ?string}>}> */ @@ -27,11 +42,11 @@ final class ListResponse implements ResponseContract, ResponseHasMetaInformation /** * @param array $data */ - private function __construct( - public readonly string $object, - public readonly array $data, - private readonly MetaInformation $meta, - ) { + private function __construct(string $object, array $data, MetaInformation $meta) + { + $this->object = $object; + $this->data = $data; + $this->meta = $meta; } /** @@ -41,16 +56,11 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $data = array_map(fn (array $result): RetrieveResponse => RetrieveResponse::from( - $result, - $meta, - ), $attributes['data']); + $data = array_map(function (array $result) use ($meta) : RetrieveResponse { + return RetrieveResponse::from($result, $meta); + }, $attributes['data']); - return new self( - $attributes['object'], - $data, - $meta, - ); + return new self($attributes['object'], $data, $meta); } /** @@ -60,10 +70,9 @@ public function toArray(): array { return [ 'object' => $this->object, - 'data' => array_map( - static fn (RetrieveResponse $response): array => $response->toArray(), - $this->data, - ), + 'data' => array_map(static function (RetrieveResponse $response) : array { + return $response->toArray(); + }, $this->data), ]; } } diff --git a/src/Responses/Models/RetrieveResponse.php b/src/Responses/Models/RetrieveResponse.php index 43fb8c85..8ded644e 100644 --- a/src/Responses/Models/RetrieveResponse.php +++ b/src/Responses/Models/RetrieveResponse.php @@ -16,6 +16,46 @@ */ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @readonly + * @var string + */ + public $ownedBy; + /** + * @var array + * @readonly + */ + public $permission; + /** + * @readonly + * @var string + */ + public $root; + /** + * @readonly + * @var string|null + */ + public $parent; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, root: string, parent: ?string}> */ @@ -27,16 +67,16 @@ final class RetrieveResponse implements ResponseContract, ResponseHasMetaInforma /** * @param array $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, - ) { + private function __construct(string $id, string $object, int $created, string $ownedBy, array $permission, string $root, ?string $parent, MetaInformation $meta) + { + $this->id = $id; + $this->object = $object; + $this->created = $created; + $this->ownedBy = $ownedBy; + $this->permission = $permission; + $this->root = $root; + $this->parent = $parent; + $this->meta = $meta; } /** @@ -46,20 +86,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $permission = array_map(fn (array $result): RetrieveResponsePermission => RetrieveResponsePermission::from( - $result - ), $attributes['permission']); + $permission = array_map(function (array $result) : RetrieveResponsePermission { + return RetrieveResponsePermission::from( + $result + ); + }, $attributes['permission']); - return new self( - $attributes['id'], - $attributes['object'], - $attributes['created'], - $attributes['owned_by'], - $permission, - $attributes['root'], - $attributes['parent'], - $meta, - ); + return new self($attributes['id'], $attributes['object'], $attributes['created'], $attributes['owned_by'], $permission, $attributes['root'], $attributes['parent'], $meta); } /** @@ -72,10 +105,9 @@ 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, - ), + 'permission' => array_map(static function (RetrieveResponsePermission $result) : array { + return $result->toArray(); + }, $this->permission), 'root' => $this->root, 'parent' => $this->parent, ]; diff --git a/src/Responses/Models/RetrieveResponsePermission.php b/src/Responses/Models/RetrieveResponsePermission.php index 26136800..d254a78e 100644 --- a/src/Responses/Models/RetrieveResponsePermission.php +++ b/src/Responses/Models/RetrieveResponsePermission.php @@ -6,41 +6,87 @@ final class RetrieveResponsePermission { - private function __construct( - public readonly string $id, - public readonly string $object, - public readonly int $created, - public readonly bool $allowCreateEngine, - public readonly bool $allowSampling, - public readonly bool $allowLogprobs, - public readonly bool $allowSearchIndices, - public readonly bool $allowView, - public readonly bool $allowFineTuning, - public readonly string $organization, - public readonly ?string $group, - public readonly bool $isBlocking, - ) { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $object; + /** + * @readonly + * @var int + */ + public $created; + /** + * @readonly + * @var bool + */ + public $allowCreateEngine; + /** + * @readonly + * @var bool + */ + public $allowSampling; + /** + * @readonly + * @var bool + */ + public $allowLogprobs; + /** + * @readonly + * @var bool + */ + public $allowSearchIndices; + /** + * @readonly + * @var bool + */ + public $allowView; + /** + * @readonly + * @var bool + */ + public $allowFineTuning; + /** + * @readonly + * @var string + */ + public $organization; + /** + * @readonly + * @var string|null + */ + public $group; + /** + * @readonly + * @var bool + */ + public $isBlocking; + private function __construct(string $id, string $object, int $created, bool $allowCreateEngine, bool $allowSampling, bool $allowLogprobs, bool $allowSearchIndices, bool $allowView, bool $allowFineTuning, string $organization, ?string $group, bool $isBlocking) + { + $this->id = $id; + $this->object = $object; + $this->created = $created; + $this->allowCreateEngine = $allowCreateEngine; + $this->allowSampling = $allowSampling; + $this->allowLogprobs = $allowLogprobs; + $this->allowSearchIndices = $allowSearchIndices; + $this->allowView = $allowView; + $this->allowFineTuning = $allowFineTuning; + $this->organization = $organization; + $this->group = $group; + $this->isBlocking = $isBlocking; } - /** * @param 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} $attributes */ public static function from(array $attributes): self { - return new self( - $attributes['id'], - $attributes['object'], - $attributes['created'], - $attributes['allow_create_engine'], - $attributes['allow_sampling'], - $attributes['allow_logprobs'], - $attributes['allow_search_indices'], - $attributes['allow_view'], - $attributes['allow_fine_tuning'], - $attributes['organization'], - $attributes['group'], - $attributes['is_blocking'], - ); + return new self($attributes['id'], $attributes['object'], $attributes['created'], $attributes['allow_create_engine'], $attributes['allow_sampling'], $attributes['allow_logprobs'], $attributes['allow_search_indices'], $attributes['allow_view'], $attributes['allow_fine_tuning'], $attributes['organization'], $attributes['group'], $attributes['is_blocking']); } /** diff --git a/src/Responses/Moderations/CreateResponse.php b/src/Responses/Moderations/CreateResponse.php index acda84c4..04bd6bf6 100644 --- a/src/Responses/Moderations/CreateResponse.php +++ b/src/Responses/Moderations/CreateResponse.php @@ -16,6 +16,26 @@ */ final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract { + /** + * @readonly + * @var string + */ + public $id; + /** + * @readonly + * @var string + */ + public $model; + /** + * @var array + * @readonly + */ + public $results; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * @use ArrayAccessible, category_scores: array, flagged: bool}>}> */ @@ -27,12 +47,12 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati /** * @param array $results */ - private function __construct( - public readonly string $id, - public readonly string $model, - public readonly array $results, - private readonly MetaInformation $meta, - ) { + private function __construct(string $id, string $model, array $results, MetaInformation $meta) + { + $this->id = $id; + $this->model = $model; + $this->results = $results; + $this->meta = $meta; } /** @@ -42,16 +62,13 @@ private function __construct( */ public static function from(array $attributes, MetaInformation $meta): self { - $results = array_map(fn (array $result): CreateResponseResult => CreateResponseResult::from( - $result - ), $attributes['results']); + $results = array_map(function (array $result) : CreateResponseResult { + return CreateResponseResult::from( + $result + ); + }, $attributes['results']); - return new self( - $attributes['id'], - $attributes['model'], - $results, - $meta, - ); + return new self($attributes['id'], $attributes['model'], $results, $meta); } /** @@ -62,10 +79,9 @@ public function toArray(): array return [ 'id' => $this->id, 'model' => $this->model, - 'results' => array_map( - static fn (CreateResponseResult $result): array => $result->toArray(), - $this->results, - ), + 'results' => array_map(static function (CreateResponseResult $result) : array { + return $result->toArray(); + }, $this->results), ]; } } diff --git a/src/Responses/Moderations/CreateResponseCategory.php b/src/Responses/Moderations/CreateResponseCategory.php index b70489b5..b86aaa24 100644 --- a/src/Responses/Moderations/CreateResponseCategory.php +++ b/src/Responses/Moderations/CreateResponseCategory.php @@ -8,22 +8,32 @@ final class CreateResponseCategory { - private function __construct( - public readonly Category $category, - public readonly bool $violated, - public readonly float $score, - ) { + /** + * @readonly + * @var \OpenAI\Enums\Moderations\Category + */ + public $category; + /** + * @readonly + * @var bool + */ + public $violated; + /** + * @readonly + * @var float + */ + public $score; + private function __construct(Category $category, bool $violated, float $score) + { + $this->category = $category; + $this->violated = $violated; + $this->score = $score; } - /** * @param array{category: string, violated: bool, score: float} $attributes */ public static function from(array $attributes): self { - return new self( - Category::from($attributes['category']), - $attributes['violated'], - $attributes['score'], - ); + return new self(Category::from($attributes['category']), $attributes['violated'], $attributes['score']); } } diff --git a/src/Responses/Moderations/CreateResponseResult.php b/src/Responses/Moderations/CreateResponseResult.php index 6bd88ca1..5f084f1e 100644 --- a/src/Responses/Moderations/CreateResponseResult.php +++ b/src/Responses/Moderations/CreateResponseResult.php @@ -8,16 +8,25 @@ final class CreateResponseResult { + /** + * @var array + * @readonly + */ + public $categories; + /** + * @readonly + * @var bool + */ + public $flagged; /** * @param array $categories */ - private function __construct( - public readonly array $categories, - public readonly bool $flagged, - ) { + private function __construct(array $categories, bool $flagged) + { + $this->categories = $categories; + $this->flagged = $flagged; // .. } - /** * @param array{categories: array, category_scores: array, flagged: bool} $attributes */ diff --git a/src/Responses/StreamResponse.php b/src/Responses/StreamResponse.php index b54e621f..d2bab539 100644 --- a/src/Responses/StreamResponse.php +++ b/src/Responses/StreamResponse.php @@ -17,18 +17,27 @@ */ final class StreamResponse implements IteratorAggregate, ResponseHasMetaInformationContract { + /** + * @var class-string + * @readonly + */ + private $responseClass; + /** + * @readonly + * @var \Psr\Http\Message\ResponseInterface + */ + private $response; /** * Creates a new Stream Response instance. * * @param class-string $responseClass */ - public function __construct( - private readonly string $responseClass, - private readonly ResponseInterface $response, - ) { + public function __construct(string $responseClass, ResponseInterface $response) + { + $this->responseClass = $responseClass; + $this->response = $response; // } - /** * {@inheritDoc} */ @@ -37,7 +46,7 @@ public function getIterator(): Generator while (! $this->response->getBody()->eof()) { $line = $this->readLine($this->response->getBody()); - if (! str_starts_with($line, 'data:')) { + if (strncmp($line, 'data:', strlen('data:')) !== 0) { continue; } @@ -48,7 +57,10 @@ public function getIterator(): Generator } /** @var array{error?: array{message: string|array, type: string, code: string}} $response */ - $response = json_decode($data, true, 512, JSON_THROW_ON_ERROR); + $response = json_decode($data, true, 512, 0); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \Exception(json_last_error_msg()); + } if (isset($response['error'])) { throw new ErrorException($response['error']); diff --git a/src/Testing/ClientFake.php b/src/Testing/ClientFake.php index e96fd83d..29f35756 100644 --- a/src/Testing/ClientFake.php +++ b/src/Testing/ClientFake.php @@ -22,16 +22,21 @@ class ClientFake implements ClientContract { + /** + * @var array + */ + protected $responses = []; /** * @var array */ - private array $requests = []; + private $requests = []; /** * @param array $responses */ - public function __construct(protected array $responses = []) + public function __construct(array $responses = []) { + $this->responses = $responses; } /** @@ -39,10 +44,13 @@ public function __construct(protected array $responses = []) */ public function addResponses(array $responses): void { - $this->responses = [...$this->responses, ...$responses]; + $this->responses = array_merge($this->responses, $responses); } - public function assertSent(string $resource, callable|int $callback = null): void + /** + * @param callable|int $callback + */ + public function assertSent(string $resource, $callback = null): void { if (is_int($callback)) { $this->assertSentTimes($resource, $callback); @@ -75,9 +83,13 @@ private function sent(string $resource, callable $callback = null): array return []; } - $callback = $callback ?: fn (): bool => true; + $callback = $callback ?: function () : bool { + return true; + }; - return array_filter($this->resourcesOf($resource), fn (TestRequest $resource) => $callback($resource->method(), $resource->parameters())); + return array_filter($this->resourcesOf($resource), function (TestRequest $resource) use ($callback) { + return $callback($resource->method(), $resource->parameters()); + }); } private function hasSent(string $resource): bool @@ -96,8 +108,10 @@ public function assertNotSent(string $resource, callable $callback = null): void public function assertNothingSent(): void { $resourceNames = implode( - separator: ', ', - array: array_map(fn (TestRequest $request): string => $request->resource(), $this->requests) + ', ', + array_map(function (TestRequest $request) : string { + return $request->resource(); + }, $this->requests) ); PHPUnit::assertEmpty($this->requests, 'The following requests were sent unexpectedly: '.$resourceNames); @@ -108,10 +122,15 @@ public function assertNothingSent(): void */ private function resourcesOf(string $type): array { - return array_filter($this->requests, fn (TestRequest $request): bool => $request->resource() === $type); + return array_filter($this->requests, function (TestRequest $request) use ($type) : bool { + return $request->resource() === $type; + }); } - public function record(TestRequest $request): ResponseContract|StreamResponse|string + /** + * @return \OpenAI\Contracts\ResponseContract|\OpenAI\Responses\StreamResponse|string + */ + public function record(TestRequest $request) { $this->requests[] = $request; @@ -128,57 +147,57 @@ public function record(TestRequest $request): ResponseContract|StreamResponse|st return $response; } - public function completions(): CompletionsTestResource + public function completions(): \OpenAI\Contracts\Resources\CompletionsContract { return new CompletionsTestResource($this); } - public function chat(): ChatTestResource + public function chat(): \OpenAI\Contracts\Resources\ChatContract { return new ChatTestResource($this); } - public function embeddings(): EmbeddingsTestResource + public function embeddings(): \OpenAI\Contracts\Resources\EmbeddingsContract { return new EmbeddingsTestResource($this); } - public function audio(): AudioTestResource + public function audio(): \OpenAI\Contracts\Resources\AudioContract { return new AudioTestResource($this); } - public function edits(): EditsTestResource + public function edits(): \OpenAI\Contracts\Resources\EditsContract { return new EditsTestResource($this); } - public function files(): FilesTestResource + public function files(): \OpenAI\Contracts\Resources\FilesContract { return new FilesTestResource($this); } - public function models(): ModelsTestResource + public function models(): \OpenAI\Contracts\Resources\ModelsContract { return new ModelsTestResource($this); } - public function fineTunes(): FineTunesTestResource + public function fineTunes(): \OpenAI\Contracts\Resources\FineTunesContract { return new FineTunesTestResource($this); } - public function fineTuning(): FineTuningTestResource + public function fineTuning(): \OpenAI\Contracts\Resources\FineTuningContract { return new FineTuningTestResource($this); } - public function moderations(): ModerationsTestResource + public function moderations(): \OpenAI\Contracts\Resources\ModerationsContract { return new ModerationsTestResource($this); } - public function images(): ImagesTestResource + public function images(): \OpenAI\Contracts\Resources\ImagesContract { return new ImagesTestResource($this); } diff --git a/src/Testing/Requests/TestRequest.php b/src/Testing/Requests/TestRequest.php index eaac50c4..6b69bf5f 100644 --- a/src/Testing/Requests/TestRequest.php +++ b/src/Testing/Requests/TestRequest.php @@ -4,11 +4,26 @@ final class TestRequest { + /** + * @var string + */ + protected $resource; + /** + * @var string + */ + protected $method; + /** + * @var array|string|null + */ + protected $parameters; /** * @param array|string|null $parameters */ - public function __construct(protected string $resource, protected string $method, protected array|string|null $parameters) + public function __construct(string $resource, string $method, $parameters) { + $this->resource = $resource; + $this->method = $method; + $this->parameters = $parameters; } public function resource(): string @@ -24,7 +39,7 @@ public function method(): string /** * @return array|string|null */ - public function parameters(): array|string|null + public function parameters() { return $this->parameters; } diff --git a/src/Testing/Resources/Concerns/Testable.php b/src/Testing/Resources/Concerns/Testable.php index aa4a989d..083744a4 100644 --- a/src/Testing/Resources/Concerns/Testable.php +++ b/src/Testing/Resources/Concerns/Testable.php @@ -17,18 +17,25 @@ abstract protected function resource(): string; /** * @param array|string $parameters + * @return \OpenAI\Contracts\ResponseContract|\OpenAI\Responses\StreamResponse|string */ - protected function record(string $method, array|string $parameters = null): ResponseContract|StreamResponse|string + protected function record(string $method, $parameters = null) { return $this->fake->record(new TestRequest($this->resource(), $method, $parameters)); } - public function assertSent(callable|int $callback = null): void + /** + * @param callable|int $callback + */ + public function assertSent($callback = null): void { $this->fake->assertSent($this->resource(), $callback); } - public function assertNotSent(callable|int $callback = null): void + /** + * @param callable|int $callback + */ + public function assertNotSent($callback = null): void { $this->fake->assertNotSent($this->resource(), $callback); } diff --git a/src/Testing/Responses/Concerns/Fakeable.php b/src/Testing/Responses/Concerns/Fakeable.php index fe5935a8..4c6a91bb 100644 --- a/src/Testing/Responses/Concerns/Fakeable.php +++ b/src/Testing/Responses/Concerns/Fakeable.php @@ -10,15 +10,13 @@ trait Fakeable { /** * @param array $override + * @return static */ - public static function fake(array $override = [], MetaInformation $meta = null): static + public static function fake(array $override = [], MetaInformation $meta = null) { $class = str_replace('Responses\\', 'Testing\\Responses\\Fixtures\\', static::class).'Fixture'; - return static::from( - self::buildAttributes($class::ATTRIBUTES, $override), - $meta ?? self::fakeResponseMetaInformation(), - ); + return static::from(self::buildAttributes($class::ATTRIBUTES, $override), $meta ?? self::fakeResponseMetaInformation()); } /** diff --git a/src/Testing/Responses/Fixtures/Chat/CreateResponseFixture.php b/src/Testing/Responses/Fixtures/Chat/CreateResponseFixture.php index 78fbcd34..d971a988 100644 --- a/src/Testing/Responses/Fixtures/Chat/CreateResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Chat/CreateResponseFixture.php @@ -7,7 +7,7 @@ final class CreateResponseFixture public const ATTRIBUTES = [ 'id' => 'chatcmpl-123', 'object' => 'chat.completion', - 'created' => 1_677_652_288, + 'created' => 1677652288, 'model' => 'gpt-3.5-turbo', 'choices' => [ [ diff --git a/src/Testing/Responses/Fixtures/Completions/CreateResponseFixture.php b/src/Testing/Responses/Fixtures/Completions/CreateResponseFixture.php index 444006f8..b8a7cb45 100644 --- a/src/Testing/Responses/Fixtures/Completions/CreateResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Completions/CreateResponseFixture.php @@ -7,7 +7,7 @@ final class CreateResponseFixture public const ATTRIBUTES = [ 'id' => 'cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7', 'object' => 'text_completion', - 'created' => 1_589_478_378, + 'created' => 1589478378, 'model' => 'gpt-3.5-turbo-instruct', 'choices' => [ [ diff --git a/src/Testing/Responses/Fixtures/Edits/CreateResponseFixture.php b/src/Testing/Responses/Fixtures/Edits/CreateResponseFixture.php index 54b89d32..5154ebf1 100644 --- a/src/Testing/Responses/Fixtures/Edits/CreateResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Edits/CreateResponseFixture.php @@ -6,7 +6,7 @@ final class CreateResponseFixture { public const ATTRIBUTES = [ 'object' => 'edit', - 'created' => 1_664_135_921, + 'created' => 1664135921, 'choices' => [[ 'text' => "This is a fake edit response.\n", 'index' => 0, diff --git a/src/Testing/Responses/Fixtures/Files/CreateResponseFixture.php b/src/Testing/Responses/Fixtures/Files/CreateResponseFixture.php index 4bbc0178..e0942214 100644 --- a/src/Testing/Responses/Fixtures/Files/CreateResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Files/CreateResponseFixture.php @@ -8,7 +8,7 @@ final class CreateResponseFixture 'id' => 'file-XjGxS3KTG0uNmNOK362iJua3', 'object' => 'file', 'bytes' => 140, - 'created_at' => 1_613_779_121, + 'created_at' => 1613779121, 'filename' => 'fake-file.jsonl', 'purpose' => 'fine-tune', 'status' => 'succeeded', diff --git a/src/Testing/Responses/Fixtures/Files/ListResponseFixture.php b/src/Testing/Responses/Fixtures/Files/ListResponseFixture.php index c699da6d..84312efe 100644 --- a/src/Testing/Responses/Fixtures/Files/ListResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Files/ListResponseFixture.php @@ -11,7 +11,7 @@ final class ListResponseFixture 'id' => 'file-XjGxS3KTG0uNmNOK362iJua3', 'object' => 'file', 'bytes' => 140, - 'created_at' => 1_613_779_121, + 'created_at' => 1613779121, 'filename' => 'fake-file.jsonl', 'purpose' => 'fine-tune', 'status' => 'succeeded', diff --git a/src/Testing/Responses/Fixtures/Files/RetrieveResponseFixture.php b/src/Testing/Responses/Fixtures/Files/RetrieveResponseFixture.php index 50c840e3..cfdb54ed 100644 --- a/src/Testing/Responses/Fixtures/Files/RetrieveResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Files/RetrieveResponseFixture.php @@ -8,7 +8,7 @@ final class RetrieveResponseFixture 'id' => 'file-XjGxS3KTG0uNmNOK362iJua3', 'object' => 'file', 'bytes' => 140, - 'created_at' => 1_613_779_121, + 'created_at' => 1613779121, 'filename' => 'fake-file.jsonl', 'purpose' => 'fine-tune', 'status' => 'succeeded', diff --git a/src/Testing/Responses/Fixtures/FineTunes/ListEventsResponseFixture.php b/src/Testing/Responses/Fixtures/FineTunes/ListEventsResponseFixture.php index 63e10bf9..345f51be 100644 --- a/src/Testing/Responses/Fixtures/FineTunes/ListEventsResponseFixture.php +++ b/src/Testing/Responses/Fixtures/FineTunes/ListEventsResponseFixture.php @@ -9,7 +9,7 @@ final class ListEventsResponseFixture 'data' => [ [ 'object' => 'fine-tune-event', - 'created_at' => 1_614_807_352, + 'created_at' => 1614807352, 'level' => 'info', 'message' => 'Job enqueued. Waiting for jobs ahead to complete. Queue number => 0.', ], diff --git a/src/Testing/Responses/Fixtures/FineTunes/RetrieveResponseFixture.php b/src/Testing/Responses/Fixtures/FineTunes/RetrieveResponseFixture.php index 18e3c0f7..28a96a6d 100644 --- a/src/Testing/Responses/Fixtures/FineTunes/RetrieveResponseFixture.php +++ b/src/Testing/Responses/Fixtures/FineTunes/RetrieveResponseFixture.php @@ -10,11 +10,11 @@ final class RetrieveResponseFixture 'id' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F', 'object' => 'fine-tune', 'model' => 'curie', - 'created_at' => 1_614_807_352, + 'created_at' => 1614807352, 'events' => [ [ 'object' => 'fine-tune-event', - 'created_at' => 1_614_807_352, + 'created_at' => 1614807352, 'level' => 'info', 'message' => 'Job enqueued. Waiting for jobs ahead to complete. Queue number => 0.', ], @@ -37,6 +37,6 @@ final class RetrieveResponseFixture 'training_files' => [ CreateResponseFixture::ATTRIBUTES, ], - 'updated_at' => 1_614_807_865, + 'updated_at' => 1614807865, ]; } diff --git a/src/Testing/Responses/Fixtures/FineTuning/ListJobEventsResponseFixture.php b/src/Testing/Responses/Fixtures/FineTuning/ListJobEventsResponseFixture.php index 583a377e..b525573d 100644 --- a/src/Testing/Responses/Fixtures/FineTuning/ListJobEventsResponseFixture.php +++ b/src/Testing/Responses/Fixtures/FineTuning/ListJobEventsResponseFixture.php @@ -10,7 +10,7 @@ final class ListJobEventsResponseFixture [ 'object' => 'fine_tuning.job.event', 'id' => 'ft-event-ddTJfwuMVpfLXseO0Am0Gqjm', - 'created_at' => 1_692_407_401, + 'created_at' => 1692407401, 'level' => 'info', 'message' => 'Fine tuning job successfully completed', 'data' => null, diff --git a/src/Testing/Responses/Fixtures/FineTuning/RetrieveJobResponseFixture.php b/src/Testing/Responses/Fixtures/FineTuning/RetrieveJobResponseFixture.php index 5047ba89..37f8ecc8 100644 --- a/src/Testing/Responses/Fixtures/FineTuning/RetrieveJobResponseFixture.php +++ b/src/Testing/Responses/Fixtures/FineTuning/RetrieveJobResponseFixture.php @@ -8,8 +8,8 @@ final class RetrieveJobResponseFixture 'id' => 'ft-AF1WoRqd3aJAHsqc9NY7iL8F', 'object' => 'fine_tuning.job', 'model' => 'gpt-3.5-turbo-0613', - 'created_at' => 1_614_807_352, - 'finished_at' => 1_692_819_450, + 'created_at' => 1614807352, + 'finished_at' => 1692819450, 'fine_tuned_model' => 'ft:gpt-3.5-turbo-0613:gehri-dev::7qnxQ0sQ', 'hyperparameters' => [ 'n_epochs' => 9, diff --git a/src/Testing/Responses/Fixtures/Images/CreateResponseFixture.php b/src/Testing/Responses/Fixtures/Images/CreateResponseFixture.php index f5c4e055..0415dc98 100644 --- a/src/Testing/Responses/Fixtures/Images/CreateResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Images/CreateResponseFixture.php @@ -5,7 +5,7 @@ final class CreateResponseFixture { public const ATTRIBUTES = [ - 'created' => 1_664_136_088, + 'created' => 1664136088, 'data' => [ [ 'url' => 'https://openai.com/fake-image.png', diff --git a/src/Testing/Responses/Fixtures/Images/EditResponseFixture.php b/src/Testing/Responses/Fixtures/Images/EditResponseFixture.php index 57b62153..538fa68b 100644 --- a/src/Testing/Responses/Fixtures/Images/EditResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Images/EditResponseFixture.php @@ -5,7 +5,7 @@ final class EditResponseFixture { public const ATTRIBUTES = [ - 'created' => 1_664_136_088, + 'created' => 1664136088, 'data' => [ [ 'url' => 'https://openai.com/fake-image.png', diff --git a/src/Testing/Responses/Fixtures/Images/VariationResponseFixture.php b/src/Testing/Responses/Fixtures/Images/VariationResponseFixture.php index 2327129e..0e0f6abd 100644 --- a/src/Testing/Responses/Fixtures/Images/VariationResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Images/VariationResponseFixture.php @@ -5,7 +5,7 @@ final class VariationResponseFixture { public const ATTRIBUTES = [ - 'created' => 1_664_136_088, + 'created' => 1664136088, 'data' => [ [ 'url' => 'https://openai.com/fake-image.png', diff --git a/src/Testing/Responses/Fixtures/Models/RetrieveResponseFixture.php b/src/Testing/Responses/Fixtures/Models/RetrieveResponseFixture.php index 5a093948..2abf04bb 100644 --- a/src/Testing/Responses/Fixtures/Models/RetrieveResponseFixture.php +++ b/src/Testing/Responses/Fixtures/Models/RetrieveResponseFixture.php @@ -7,13 +7,13 @@ final class RetrieveResponseFixture public const ATTRIBUTES = [ 'id' => 'text-babbage:001', 'object' => 'model', - 'created' => 1_642_018_370, + 'created' => 1642018370, 'owned_by' => 'openai', 'permission' => [ [ 'id' => 'snapperm-7oP3WFr9x7qf5xb3eZrVABAH', 'object' => 'model_permission', - 'created' => 1_642_018_480, + 'created' => 1642018480, 'allow_create_engine' => false, 'allow_sampling' => true, 'allow_logprobs' => true, diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index 6f9eb50a..ee6c25a7 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -26,19 +26,43 @@ */ final class HttpTransporter implements TransporterContract { + /** + * @readonly + * @var \Psr\Http\Client\ClientInterface + */ + private $client; + /** + * @readonly + * @var \OpenAI\ValueObjects\Transporter\BaseUri + */ + private $baseUri; + /** + * @readonly + * @var \OpenAI\ValueObjects\Transporter\Headers + */ + private $headers; + /** + * @readonly + * @var \OpenAI\ValueObjects\Transporter\QueryParams + */ + private $queryParams; + /** + * @readonly + * @var \Closure + */ + private $streamHandler; /** * Creates a new Http Transporter instance. */ - public function __construct( - private readonly ClientInterface $client, - private readonly BaseUri $baseUri, - private readonly Headers $headers, - private readonly QueryParams $queryParams, - private readonly Closure $streamHandler, - ) { + public function __construct(ClientInterface $client, BaseUri $baseUri, Headers $headers, QueryParams $queryParams, Closure $streamHandler) + { + $this->client = $client; + $this->baseUri = $baseUri; + $this->headers = $headers; + $this->queryParams = $queryParams; + $this->streamHandler = $streamHandler; // .. } - /** * {@inheritDoc} */ @@ -46,11 +70,13 @@ public function requestObject(Payload $payload): Response { $request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams); - $response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request)); + $response = $this->sendRequest(function () use ($request) : \Psr\Http\Message\ResponseInterface { + return $this->client->sendRequest($request); + }); $contents = $response->getBody()->getContents(); - if (str_contains($response->getHeaderLine('Content-Type'), ContentType::TEXT_PLAIN->value)) { + if (strpos($response->getHeaderLine('Content-Type'), ContentType::TEXT_PLAIN->value) !== false) { return Response::from($contents, $response->getHeaders()); } @@ -58,7 +84,7 @@ public function requestObject(Payload $payload): Response try { /** @var array{error?: array{message: string, type: string, code: string}} $data */ - $data = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); + $data = json_decode($contents, true, 512, 0); } catch (JsonException $jsonException) { throw new UnserializableResponse($jsonException); } @@ -73,7 +99,9 @@ public function requestContent(Payload $payload): string { $request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams); - $response = $this->sendRequest(fn (): \Psr\Http\Message\ResponseInterface => $this->client->sendRequest($request)); + $response = $this->sendRequest(function () use ($request) : \Psr\Http\Message\ResponseInterface { + return $this->client->sendRequest($request); + }); $contents = $response->getBody()->getContents(); @@ -89,7 +117,9 @@ public function requestStream(Payload $payload): ResponseInterface { $request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams); - $response = $this->sendRequest(fn () => ($this->streamHandler)($request)); + $response = $this->sendRequest(function () use ($request) { + return ($this->streamHandler)($request); + }); $this->throwIfJsonError($response, $response); @@ -109,13 +139,16 @@ private function sendRequest(Closure $callable): ResponseInterface } } - private function throwIfJsonError(ResponseInterface $response, string|ResponseInterface $contents): void + /** + * @param string|\Psr\Http\Message\ResponseInterface $contents + */ + private function throwIfJsonError(ResponseInterface $response, $contents): void { if ($response->getStatusCode() < 400) { return; } - if (! str_contains($response->getHeaderLine('Content-Type'), ContentType::JSON->value)) { + if (strpos($response->getHeaderLine('Content-Type'), ContentType::JSON->value) === false) { return; } @@ -125,7 +158,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn try { /** @var array{error?: array{message: string|array, type: string, code: string}} $response */ - $response = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); + $response = json_decode($contents, true, 512, 0); if (isset($response['error'])) { throw new ErrorException($response['error']); diff --git a/src/ValueObjects/ApiKey.php b/src/ValueObjects/ApiKey.php index f27c646b..93e33890 100644 --- a/src/ValueObjects/ApiKey.php +++ b/src/ValueObjects/ApiKey.php @@ -11,11 +11,17 @@ */ final class ApiKey implements StringableContract { + /** + * @readonly + * @var string + */ + public $apiKey; /** * Creates a new API token value object. */ - private function __construct(public readonly string $apiKey) + private function __construct(string $apiKey) { + $this->apiKey = $apiKey; // .. } diff --git a/src/ValueObjects/ResourceUri.php b/src/ValueObjects/ResourceUri.php index aaf84d64..129d192b 100644 --- a/src/ValueObjects/ResourceUri.php +++ b/src/ValueObjects/ResourceUri.php @@ -11,11 +11,17 @@ */ final class ResourceUri implements StringableContract { + /** + * @readonly + * @var string + */ + private $uri; /** * Creates a new ResourceUri value object. */ - private function __construct(private readonly string $uri) + private function __construct(string $uri) { + $this->uri = $uri; // .. } diff --git a/src/ValueObjects/Transporter/BaseUri.php b/src/ValueObjects/Transporter/BaseUri.php index b034b938..952c6576 100644 --- a/src/ValueObjects/Transporter/BaseUri.php +++ b/src/ValueObjects/Transporter/BaseUri.php @@ -11,11 +11,17 @@ */ final class BaseUri implements StringableContract { + /** + * @readonly + * @var string + */ + private $baseUri; /** * Creates a new Base URI value object. */ - private function __construct(private readonly string $baseUri) + private function __construct(string $baseUri) { + $this->baseUri = $baseUri; // .. } @@ -33,7 +39,7 @@ public static function from(string $baseUri): self public function toString(): string { foreach (['http://', 'https://'] as $protocol) { - if (str_starts_with($this->baseUri, $protocol)) { + if (strncmp($this->baseUri, $protocol, strlen($protocol)) === 0) { return "{$this->baseUri}/"; } } diff --git a/src/ValueObjects/Transporter/Headers.php b/src/ValueObjects/Transporter/Headers.php index 40d4e314..400147e6 100644 --- a/src/ValueObjects/Transporter/Headers.php +++ b/src/ValueObjects/Transporter/Headers.php @@ -12,13 +12,19 @@ */ final class Headers { + /** + * @var array + * @readonly + */ + private $headers; /** * Creates a new Headers value object. * * @param array $headers */ - private function __construct(private readonly array $headers) + private function __construct(array $headers) { + $this->headers = $headers; // .. } @@ -42,13 +48,11 @@ public static function withAuthorization(ApiKey $apiKey): self /** * Creates a new Headers value object, with the given content type, and the existing headers. + * @param \OpenAI\Enums\Transporter\ContentType::* $contentType */ - public function withContentType(ContentType $contentType, string $suffix = ''): self + public function withContentType($contentType, string $suffix = ''): self { - return new self([ - ...$this->headers, - 'Content-Type' => $contentType->value.$suffix, - ]); + return new self(array_merge($this->headers, ['Content-Type' => $contentType->value.$suffix])); } /** @@ -56,10 +60,7 @@ public function withContentType(ContentType $contentType, string $suffix = ''): */ public function withOrganization(string $organization): self { - return new self([ - ...$this->headers, - 'OpenAI-Organization' => $organization, - ]); + return new self(array_merge($this->headers, ['OpenAI-Organization' => $organization])); } /** @@ -67,10 +68,7 @@ public function withOrganization(string $organization): self */ public function withCustomHeader(string $name, string $value): self { - return new self([ - ...$this->headers, - $name => $value, - ]); + return new self(array_merge($this->headers, [$name => $value])); } /** diff --git a/src/ValueObjects/Transporter/Payload.php b/src/ValueObjects/Transporter/Payload.php index 3533e96e..4a4fc01b 100644 --- a/src/ValueObjects/Transporter/Payload.php +++ b/src/ValueObjects/Transporter/Payload.php @@ -18,20 +18,39 @@ */ final class Payload { + /** + * @readonly + * @var \OpenAI\Enums\Transporter\ContentType + */ + private $contentType; + /** + * @readonly + * @var \OpenAI\Enums\Transporter\Method + */ + private $method; + /** + * @readonly + * @var \OpenAI\ValueObjects\ResourceUri + */ + private $uri; + /** + * @var array + * @readonly + */ + private $parameters = []; /** * Creates a new Request value object. * * @param array $parameters */ - private function __construct( - private readonly ContentType $contentType, - private readonly Method $method, - private readonly ResourceUri $uri, - private readonly array $parameters = [], - ) { + private function __construct(ContentType $contentType, Method $method, ResourceUri $uri, array $parameters = []) + { + $this->contentType = $contentType; + $this->method = $method; + $this->uri = $uri; + $this->parameters = $parameters; // .. } - /** * Creates a new Payload value object from the given parameters. * @@ -137,7 +156,7 @@ public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $query $queryParams = $queryParams->toArray(); if ($this->method === Method::GET) { - $queryParams = [...$queryParams, ...$this->parameters]; + $queryParams = array_merge($queryParams, $this->parameters); } if ($queryParams !== []) { @@ -165,7 +184,7 @@ public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $query $headers = $headers->withContentType($this->contentType, '; boundary='.$streamBuilder->getBoundary()); } else { - $body = $psr17Factory->createStream(json_encode($this->parameters, JSON_THROW_ON_ERROR)); + $body = $psr17Factory->createStream(json_encode($this->parameters, 0)); } } diff --git a/src/ValueObjects/Transporter/QueryParams.php b/src/ValueObjects/Transporter/QueryParams.php index 1eaf8dfe..299dbd68 100644 --- a/src/ValueObjects/Transporter/QueryParams.php +++ b/src/ValueObjects/Transporter/QueryParams.php @@ -9,13 +9,19 @@ */ final class QueryParams { + /** + * @var array + * @readonly + */ + private $params; /** * Creates a new Query Params value object. * * @param array $params */ - private function __construct(private readonly array $params) + private function __construct(array $params) { + $this->params = $params; // .. } @@ -29,13 +35,11 @@ public static function create(): self /** * Creates a new Query Params value object, with the newly added param, and the existing params. + * @param string|int $value */ - public function withParam(string $name, string|int $value): self + public function withParam(string $name, $value): self { - return new self([ - ...$this->params, - $name => $value, - ]); + return new self(array_merge($this->params, [$name => $value])); } /** diff --git a/src/ValueObjects/Transporter/Response.php b/src/ValueObjects/Transporter/Response.php index 1645ab44..78f4a4c0 100644 --- a/src/ValueObjects/Transporter/Response.php +++ b/src/ValueObjects/Transporter/Response.php @@ -13,26 +13,38 @@ */ final class Response { + /** + * @var TData + * @readonly + */ + private $data; + /** + * @readonly + * @var \OpenAI\Responses\Meta\MetaInformation + */ + private $meta; /** * Creates a new Response value object. * - * @param TData $data + * @param mixed[]|string $data */ private function __construct( - private readonly array|string $data, - private readonly MetaInformation $meta + $data, + MetaInformation $meta ) { + $this->data = $data; + $this->meta = $meta; // .. } /** * Creates a new Response value object from the given data and meta information. * - * @param TData $data + * @param mixed[]|string $data * @param array> $headers * @return Response */ - public static function from(array|string $data, array $headers): self + public static function from($data, array $headers): self { // @phpstan-ignore-next-line $meta = MetaInformation::from($headers); @@ -45,7 +57,7 @@ public static function from(array|string $data, array $headers): self * * @return TData */ - public function data(): array|string + public function data() { return $this->data; } From 1b8065b59fe5a3693076e5b70b118b1cb5c08ee2 Mon Sep 17 00:00:00 2001 From: Gianluca Fabrizi Date: Thu, 19 Oct 2023 12:36:15 +0200 Subject: [PATCH 2/4] Adjusted composer.json PHP version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e3a3786e..d646ffc5 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": "^8.1.0", + "php": "^7.2.0", "php-http/discovery": "^1.19.1", "php-http/multipart-stream-builder": "^1.3.0", "psr/http-client": "^1.0.3", From e94e509c59fc776a6096a2c4ede54de99bfa0db6 Mon Sep 17 00:00:00 2001 From: Gianluca Fabrizi Date: Thu, 19 Oct 2023 13:05:25 +0200 Subject: [PATCH 3/4] Adjusted composer.json name and removed dev dependencies --- composer.json | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/composer.json b/composer.json index d646ffc5..d5e9d5d0 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "openai-php/client", + "name": "skuola/openai-php-client", "description": "OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API", "keywords": ["php", "openai", "sdk", "codex", "GPT-3", "DALL-E", "api", "client", "natural", "language", "processing"], "license": "MIT", @@ -22,17 +22,6 @@ "psr/http-message": "^1.1.0|^2.0.0" }, "require-dev": { - "guzzlehttp/guzzle": "^7.8.0", - "guzzlehttp/psr7": "^2.6.1", - "laravel/pint": "^1.13.2", - "nunomaduro/collision": "^7.9.0", - "pestphp/pest": "^2.19.2", - "pestphp/pest-plugin-arch": "^2.3.3", - "pestphp/pest-plugin-mock": "^2.0.0", - "pestphp/pest-plugin-type-coverage": "^2.2.0", - "phpstan/phpstan": "^1.10.35", - "rector/rector": "^0.16.0", - "symfony/var-dumper": "^6.3.4" }, "autoload": { "psr-4": { From 1993e9594f1e291d5c0d7b006b4f804b00ff19c8 Mon Sep 17 00:00:00 2001 From: Gianluca Fabrizi Date: Thu, 19 Oct 2023 15:00:46 +0200 Subject: [PATCH 4/4] Downgraded psr/http-message --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d5e9d5d0..fd057a5e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "psr/http-client": "^1.0.3", "psr/http-client-implementation": "^1.0.1", "psr/http-factory-implementation": "*", - "psr/http-message": "^1.1.0|^2.0.0" + "psr/http-message": "^1.0.1|^2.0.0" }, "require-dev": { },