Skip to content

Commit

Permalink
fix: missing openai-version header from azure - fixes #201
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Aug 31, 2023
1 parent ca17a3d commit ddab64d
Show file tree
Hide file tree
Showing 4 changed files with 29 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 @@ -33,7 +33,7 @@ public static function from(array $headers): self
$openai = MetaInformationOpenAI::from([
'model' => $headers['openai-model'][0] ?? null,
'organization' => $headers['openai-organization'][0] ?? null,
'version' => $headers['openai-version'][0],
'version' => $headers['openai-version'][0] ?? null,
'processingMs' => (int) $headers['openai-processing-ms'][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 @@ -7,13 +7,13 @@ final class MetaInformationOpenAI
public function __construct(
public readonly ?string $model,
public readonly ?string $organization,
public readonly string $version,
public readonly ?string $version,
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
9 changes: 9 additions & 0 deletions tests/Fixtures/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ function metaHeaders(): array
];
}

function metaHeadersFromAzure(): array
{
return [
'openai-model' => ['text-davinci-003'],
'openai-processing-ms' => [3482.8264],
'x-request-id' => ['3813fa4fa3f17bdf0d7654f0f49ebab4'],
];
}

function meta(): MetaInformation
{
return MetaInformation::from(metaHeaders());
Expand Down
15 changes: 15 additions & 0 deletions tests/Responses/Meta/MetaInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
->tokenLimit->reset->toBe('2ms');
});

test('from azure response headers', function () {
$meta = MetaInformation::from((new \GuzzleHttp\Psr7\Response(headers: metaHeadersFromAzure()))->getHeaders());

expect($meta)
->toBeInstanceOf(MetaInformation::class)
->requestId->toBe('3813fa4fa3f17bdf0d7654f0f49ebab4')
->openai->toBeInstanceOf(MetaInformationOpenAI::class)
->openai->model->toBe('text-davinci-003')
->openai->organization->toBeNull()
->openai->version->toBeNull()
->openai->processingMs->toBe(3482)
->requestLimit->toBeNull()
->tokenLimit->toBeNull();
});

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

Expand Down

0 comments on commit ddab64d

Please sign in to comment.