Skip to content

Commit

Permalink
WIP: refactor and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Aug 22, 2023
1 parent 90e9e2d commit 18bb369
Show file tree
Hide file tree
Showing 56 changed files with 321 additions and 175 deletions.
3 changes: 0 additions & 3 deletions src/Contracts/ResponseContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OpenAI\Contracts;

use ArrayAccess;
use OpenAI\Responses\ResponseMetaInformation;

/**
* @template TArray of array
Expand Down Expand Up @@ -50,6 +49,4 @@ public function offsetSet(mixed $offset, mixed $value): never;
* @param TOffsetKey $offset
*/
public function offsetUnset(mixed $offset): never;

// public function meta(): ResponseMetaInformation;
}
12 changes: 12 additions & 0 deletions src/Contracts/ResponseHasMetaInformationContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace OpenAI\Contracts;

use OpenAI\Responses\ResponseMetaInformation;

interface ResponseHasMetaInformationContract
{
public function meta(): ResponseMetaInformation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @internal
*/
interface ResponseMetaContract extends ArrayAccess
interface ResponseMetaInformationContract extends ArrayAccess
{
/**
* Returns the array representation of the meta information.
Expand Down
10 changes: 4 additions & 6 deletions src/Responses/Audio/TranscriptionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Audio;

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

/**
* @implements ResponseContract<array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}>
*/
final class TranscriptionResponse implements ResponseContract
final class TranscriptionResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -75,9 +78,4 @@ public function toArray(): array
'text' => $this->text,
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
10 changes: 4 additions & 6 deletions src/Responses/Audio/TranslationResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Audio;

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

/**
* @implements ResponseContract<array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}>
*/
final class TranslationResponse implements ResponseContract
final class TranslationResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{task: ?string, language: ?string, duration: ?float, segments: array<int, array{id: int, seek: int, start: float, end: float, text: string, tokens: array<int, int>, temperature: float, avg_logprob: float, compression_ratio: float, no_speech_prob: float, transient: bool}>, text: string}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -75,9 +78,4 @@ public function toArray(): array
'text' => $this->text,
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
10 changes: 4 additions & 6 deletions src/Responses/Chat/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Chat;

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

/**
* @implements ResponseContract<array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, message: array{role: string, content: string|null, function_call?: array{name: string, arguments: string}}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
*/
final class CreateResponse implements ResponseContract
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, choices: array<int, array{index: int, message: array{role: string, content: string|null, function_call?: array{name: string, arguments: string}}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -74,9 +77,4 @@ public function toArray(): array
'usage' => $this->usage->toArray(),
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
10 changes: 4 additions & 6 deletions src/Responses/Completions/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Completions;

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

/**
* @implements ResponseContract<array{id: string, object: string, created: int, model: string, choices: array<int, array{text: string, index: int, logprobs: array{tokens: array<int, string>, token_logprobs: array<int, float>, top_logprobs: array<int, string>|null, text_offset: array<int, int>}|null, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
*/
final class CreateResponse implements ResponseContract
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, choices: array<int, array{text: string, index: int, logprobs: array{tokens: array<int, string>, token_logprobs: array<int, float>, top_logprobs: array<int, string>|null, text_offset: array<int, int>}|null, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -74,9 +77,4 @@ public function toArray(): array
'usage' => $this->usage->toArray(),
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
15 changes: 15 additions & 0 deletions src/Responses/Concerns/HasMetaInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Concerns;

use OpenAI\Responses\ResponseMetaInformation;

trait HasMetaInformation
{
public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
10 changes: 4 additions & 6 deletions src/Responses/Edits/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Edits;

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

/**
* @implements ResponseContract<array{object: string, created: int, choices: array<int, array{text: string, index: int}>, usage: array{prompt_tokens: int, completion_tokens: int, total_tokens: int}}>
*/
final class CreateResponse implements ResponseContract
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, choices: array<int, array{text: string, index: int, logprobs: int|null, finish_reason: string}>, usage: array{prompt_tokens: int, completion_tokens: int, total_tokens: int}}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -68,9 +71,4 @@ public function toArray(): array
'usage' => $this->usage->toArray(),
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
10 changes: 4 additions & 6 deletions src/Responses/Embeddings/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Embeddings;

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

/**
* @implements ResponseContract<array{object: string, data: array<int, array{object: string, embedding: array<int, float>, index: int}>, usage: array{prompt_tokens: int, total_tokens: int}}>
*/
final class CreateResponse implements ResponseContract
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{object: string, data: array<int, array{object: string, embedding: array<int, float>, index: int}>, usage: array{prompt_tokens: int, total_tokens: int}}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -65,9 +68,4 @@ public function toArray(): array
'usage' => $this->usage->toArray(),
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
10 changes: 4 additions & 6 deletions src/Responses/Files/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Files;

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

/**
* @implements ResponseContract<array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}>
*/
final class CreateResponse implements ResponseContract
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand Down Expand Up @@ -73,9 +76,4 @@ public function toArray(): array
'status_details' => $this->statusDetails,
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
12 changes: 5 additions & 7 deletions src/Responses/Files/DeleteResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
namespace OpenAI\Responses\Files;

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

/**
* @implements ResponseContract<array{id: string, object: string, deleted: bool}>
*/
final class DeleteResponse implements ResponseContract
final class DeleteResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{id: string, object: string, deleted: bool}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

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

Expand Down Expand Up @@ -55,9 +58,4 @@ public function toArray(): array
'deleted' => $this->deleted,
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
15 changes: 7 additions & 8 deletions src/Responses/Files/ListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
namespace OpenAI\Responses\Files;

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

/**
* @implements ResponseContract<array{object: string, data: array<int, array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}>}>
*/
final class ListResponse implements ResponseContract
final class ListResponse implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<array{object: string, data: array<int, array{id: string, object: string, created_at: int, bytes: int, filename: string, purpose: string, status: string, status_details: array<array-key, mixed>|string|null}>}>
*/
use ArrayAccessible;

use HasMetaInformation;
use Fakeable;

/**
Expand All @@ -27,7 +30,7 @@ final class ListResponse implements ResponseContract
private function __construct(
public readonly string $object,
public readonly array $data,
public readonly ResponseMetaInformation $meta,
private readonly ResponseMetaInformation $meta,
) {
}

Expand All @@ -39,7 +42,8 @@ private function __construct(
public static function from(array $attributes, ResponseMetaInformation $meta): self
{
$data = array_map(fn (array $result): RetrieveResponse => RetrieveResponse::from(
$result
$result,
$meta,
), $attributes['data']);

return new self(
Expand All @@ -62,9 +66,4 @@ public function toArray(): array
),
];
}

public function meta(): ResponseMetaInformation
{
return $this->meta;
}
}
Loading

0 comments on commit 18bb369

Please sign in to comment.