Files
archived-ai-ollama-platform/TokenUsageExtractor.php
2025-12-23 11:47:22 +01:00

43 lines
1.2 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\Ollama;
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 Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class TokenUsageExtractor implements TokenUsageExtractorInterface
{
public function extract(RawResultInterface $rawResult, array $options = []): ?TokenUsageInterface
{
if ($options['stream'] ?? false) {
// Streams have to be handled manually as the tokens are part of the streamed chunks
return null;
}
$payload = $rawResult->getData();
if (!isset($payload['prompt_eval_count'], $payload['eval_count'])) {
return null;
}
return new TokenUsage(
$payload['prompt_eval_count'],
$payload['eval_count'],
);
}
}