mirror of
https://github.com/symfony/ai-generic-platform.git
synced 2026-03-23 23:42:22 +01:00
44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Symfony\AI\Platform\Bridge\Generic\Completions;
|
|
|
|
use Symfony\AI\Platform\Result\RawResultInterface;
|
|
use Symfony\AI\Platform\TokenUsage\TokenUsage;
|
|
use Symfony\AI\Platform\TokenUsage\TokenUsageExtractorInterface;
|
|
use Symfony\AI\Platform\TokenUsage\TokenUsageInterface;
|
|
|
|
/**
|
|
* @author Christopher Hertel <mail@christopher-hertel.de>
|
|
*/
|
|
final class TokenUsageExtractor implements TokenUsageExtractorInterface
|
|
{
|
|
public function extract(RawResultInterface $rawResult, array $options = []): ?TokenUsageInterface
|
|
{
|
|
if ($options['stream'] ?? false) {
|
|
return null;
|
|
}
|
|
|
|
$content = $rawResult->getData();
|
|
|
|
if (!\array_key_exists('usage', $content)) {
|
|
return null;
|
|
}
|
|
|
|
return new TokenUsage(
|
|
promptTokens: $content['usage']['prompt_tokens'] ?? null,
|
|
completionTokens: $content['usage']['completion_tokens'] ?? null,
|
|
cachedTokens: $content['usage']['num_cached_tokens'] ?? null,
|
|
totalTokens: $content['usage']['total_tokens'] ?? null,
|
|
);
|
|
}
|
|
}
|