Skip to content

Commit

Permalink
Merge pull request #494 from IsraelOrtuno/main
Browse files Browse the repository at this point in the history
fix: add missing properties (specially `cached_usage`) to chat `CreateResponseUsage`
  • Loading branch information
gehrisandro authored Nov 12, 2024
2 parents 2b4522b + a49b26f commit 04c08f2
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Responses/Chat/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function __construct(
/**
* Acts as static factory, and returns a new Response instance.
*
* @param array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}} $attributes
* @param array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}, completion_tokens_details?:array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}}} $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
Expand Down
20 changes: 17 additions & 3 deletions src/Responses/Chat/CreateResponseUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,43 @@ private function __construct(
public readonly int $promptTokens,
public readonly ?int $completionTokens,
public readonly int $totalTokens,
public readonly ?CreateResponseUsagePromptTokensDetails $promptTokensDetails,
public readonly ?CreateResponseUsageCompletionTokensDetails $completionTokensDetails
) {}

/**
* @param array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int} $attributes
* @param array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}, completion_tokens_details?:array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['prompt_tokens'],
$attributes['completion_tokens'] ?? null,
$attributes['total_tokens'],
isset($attributes['prompt_tokens_details']) ? CreateResponseUsagePromptTokensDetails::from($attributes['prompt_tokens_details']) : null,
isset($attributes['completion_tokens_details']) ? CreateResponseUsageCompletionTokensDetails::from($attributes['completion_tokens_details']) : null
);
}

/**
* @return array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}
* @return array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}}
*/
public function toArray(): array
{
return [
$result = [
'prompt_tokens' => $this->promptTokens,
'completion_tokens' => $this->completionTokens,
'total_tokens' => $this->totalTokens,
];

if ($this->promptTokensDetails) {
$result['prompt_tokens_details'] = $this->promptTokensDetails->toArray();
}

if ($this->completionTokensDetails) {
$result['completion_tokens_details'] = $this->completionTokensDetails->toArray();
}

return $result;
}
}
46 changes: 46 additions & 0 deletions src/Responses/Chat/CreateResponseUsageCompletionTokensDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Chat;

final class CreateResponseUsageCompletionTokensDetails
{
private function __construct(
public readonly ?int $audioTokens,
public readonly int $reasoningTokens,
public readonly int $acceptedPredictionTokens,
public readonly int $rejectedPredictionTokens
) {}

/**
* @param array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['audio_tokens'] ?? null,
$attributes['reasoning_tokens'],
$attributes['accepted_prediction_tokens'],
$attributes['rejected_prediction_tokens'],
);
}

/**
* @return array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}
*/
public function toArray(): array
{
$result = [
'reasoning_tokens' => $this->reasoningTokens,
'accepted_prediction_tokens' => $this->acceptedPredictionTokens,
'rejected_prediction_tokens' => $this->rejectedPredictionTokens,
];

if (! is_null($this->audioTokens)) {
$result['audio_tokens'] = $this->audioTokens;
}

return $result;
}
}
40 changes: 40 additions & 0 deletions src/Responses/Chat/CreateResponseUsagePromptTokensDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace OpenAI\Responses\Chat;

final class CreateResponseUsagePromptTokensDetails
{
private function __construct(
public readonly ?int $audioTokens,
public readonly int $cachedTokens
) {}

/**
* @param array{audio_tokens?:int, cached_tokens?: int} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['audio_tokens'] ?? null,
$attributes['cached_tokens'] ?? 0,
);
}

/**
* @return array{cached_tokens: int, audio_tokens?:int}
*/
public function toArray(): array
{
$result = [
'cached_tokens' => $this->cachedTokens,
];

if (! is_null($this->audioTokens)) {
$result['audio_tokens'] = $this->audioTokens;
}

return $result;
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ function chatCompletion(): array
'prompt_tokens' => 9,
'completion_tokens' => 12,
'total_tokens' => 21,
'prompt_tokens_details' => [
'cached_tokens' => 5,
],
'completion_tokens_details' => [
'reasoning_tokens' => 0,
'accepted_prediction_tokens' => 0,
'rejected_prediction_tokens' => 0,
],
],
];
}
Expand Down
6 changes: 5 additions & 1 deletion tests/Responses/Chat/CreateResponseUsage.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php

use OpenAI\Responses\Chat\CreateResponseUsage;
use OpenAI\Responses\Chat\CreateResponseUsageCompletionTokensDetails;
use OpenAI\Responses\Chat\CreateResponseUsagePromptTokensDetails;

test('from', function () {
$result = CreateResponseUsage::from(chatCompletion()['usage']);

expect($result)
->promptTokens->toBe(9)
->completionTokens->toBe(12)
->totalTokens->toBe(21);
->totalTokens->toBe(21)
->promptTokensDetails->toBeInstanceOf(CreateResponseUsagePromptTokensDetails::class)
->completionTokensDetails->toBeInstanceOf(CreateResponseUsageCompletionTokensDetails::class);
});

test('to array', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use OpenAI\Responses\Chat\CreateResponseUsageCompletionTokensDetails;

test('from', function () {
$result = CreateResponseUsageCompletionTokensDetails::from(chatCompletion()['usage']['completion_tokens_details']);

expect($result)
->audioTokens->toBeNull()
->reasoningTokens->toBe(0)
->acceptedPredictionTokens->toBe(0)
->rejectedPredictionTokens->toBe(0);
});

test('to array', function () {
$result = CreateResponseUsageCompletionTokensDetails::from(chatCompletion()['usage']['completion_tokens_details']);

expect($result->toArray())
->toBe(chatCompletion()['usage']['completion_tokens_details']);
});
17 changes: 17 additions & 0 deletions tests/Responses/Chat/CreateResponseUsagePromptTokensDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use OpenAI\Responses\Chat\CreateResponseUsagePromptTokensDetails;

test('from', function () {
$result = CreateResponseUsagePromptTokensDetails::from(chatCompletion()['usage']['prompt_tokens_details']);

expect($result)
->cachedTokens->toBe(5);
});

test('to array', function () {
$result = CreateResponseUsagePromptTokensDetails::from(chatCompletion()['usage']['prompt_tokens_details']);

expect($result->toArray())
->toBe(chatCompletion()['usage']['prompt_tokens_details']);
});

0 comments on commit 04c08f2

Please sign in to comment.