-
-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #494 from IsraelOrtuno/main
fix: add missing properties (specially `cached_usage`) to chat `CreateResponseUsage`
- Loading branch information
Showing
8 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Responses/Chat/CreateResponseUsageCompletionTokensDetails.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/Responses/Chat/CreateResponseUsagePromptTokensDetails.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/Responses/Chat/CreateResponseUsageCompletionTokensDetails.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
tests/Responses/Chat/CreateResponseUsagePromptTokensDetails.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); |