Skip to content

Commit

Permalink
Fix: processingMs ond MetaInformationOpenAI may be null - fixes #218
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Oct 21, 2023
1 parent 6e0d203 commit 2e4b267
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Responses/Meta/MetaInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use OpenAI\Responses\Concerns\ArrayAccessible;

/**
* @implements MetaInformationContract<array{x-request-id: string, openai-model?: string, openai-organization?: string, openai-processing-ms: int, openai-version?: string, x-ratelimit-limit-requests?: int, x-ratelimit-limit-tokens?: int, x-ratelimit-remaining-requests?: int, x-ratelimit-remaining-tokens?: int, x-ratelimit-reset-requests?: string, x-ratelimit-reset-tokens?: string, x-request-id: string}>
* @implements MetaInformationContract<array{x-request-id: string, openai-model?: string, openai-organization?: string, openai-processing-ms?: int, openai-version?: string, x-ratelimit-limit-requests?: int, x-ratelimit-limit-tokens?: int, x-ratelimit-remaining-requests?: int, x-ratelimit-remaining-tokens?: int, x-ratelimit-reset-requests?: string, x-ratelimit-reset-tokens?: string, x-request-id: string}>
*/
final class MetaInformation implements MetaInformationContract
{
/**
* @use ArrayAccessible<array{x-request-id: string, openai-model?: string, openai-organization?: string, openai-processing-ms: int, openai-version?: string, x-ratelimit-limit-requests?: int, x-ratelimit-limit-tokens?: int, x-ratelimit-remaining-requests?: int, x-ratelimit-remaining-tokens?: int, x-ratelimit-reset-requests?: string, x-ratelimit-reset-tokens?: string, x-request-id: string}>
* @use ArrayAccessible<array{x-request-id: string, openai-model?: string, openai-organization?: string, openai-processing-ms?: int, openai-version?: string, x-ratelimit-limit-requests?: int, x-ratelimit-limit-tokens?: int, x-ratelimit-remaining-requests?: int, x-ratelimit-remaining-tokens?: int, x-ratelimit-reset-requests?: string, x-ratelimit-reset-tokens?: string, x-request-id: string}>
*/
use ArrayAccessible;

Expand All @@ -34,7 +34,7 @@ public static function from(array $headers): self
'model' => $headers['openai-model'][0] ?? null,
'organization' => $headers['openai-organization'][0] ?? null,
'version' => $headers['openai-version'][0] ?? null,
'processingMs' => (int) $headers['openai-processing-ms'][0],
'processingMs' => isset($headers['openai-processing-ms'][0]) ? (int) $headers['openai-processing-ms'][0] : null,
]);

if (isset($headers['x-ratelimit-limit-requests'][0])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Responses/Meta/MetaInformationOpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public function __construct(
public readonly ?string $model,
public readonly ?string $organization,
public readonly ?string $version,
public readonly int $processingMs,
public readonly ?int $processingMs,
) {
}

/**
* @param array{model: ?string, organization: ?string, version: ?string, processingMs: int} $attributes
* @param array{model: ?string, organization: ?string, version: ?string, processingMs: ?int} $attributes
*/
public static function from(array $attributes): self
{
Expand Down
18 changes: 18 additions & 0 deletions tests/Responses/Meta/MetaInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@
->tokenLimit->toBeNull();
});

test('from azure response headers without processing time', function () {
$headers = metaHeadersFromAzure();
unset($headers['openai-processing-ms']);

$meta = MetaInformation::from((new \GuzzleHttp\Psr7\Response(headers: $headers))->getHeaders());

expect($meta)
->toBeInstanceOf(MetaInformation::class)
->requestId->toBe('3813fa4fa3f17bdf0d7654f0f49ebab4')
->openai->toBeInstanceOf(MetaInformationOpenAI::class)
->openai->model->toBe('gpt-3.5-turbo-instruct')
->openai->organization->toBeNull()
->openai->version->toBeNull()
->openai->processingMs->toBeNull()
->requestLimit->toBeNull()
->tokenLimit->toBeNull();
});

test('as array accessible', function () {
$meta = MetaInformation::from(metaHeaders());

Expand Down

0 comments on commit 2e4b267

Please sign in to comment.