* * 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 */ 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'], ); } }