|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Bridge\Generic\Tests\Embeddings; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\AI\Platform\Bridge\Generic\Embeddings\TokenUsageExtractor; |
| 16 | +use Symfony\AI\Platform\Result\InMemoryRawResult; |
| 17 | +use Symfony\AI\Platform\TokenUsage\TokenUsage; |
| 18 | + |
| 19 | +final class TokenUsageExtractorTest extends TestCase |
| 20 | +{ |
| 21 | + public function testItReturnsNullWithoutUsageData() |
| 22 | + { |
| 23 | + $extractor = new TokenUsageExtractor(); |
| 24 | + |
| 25 | + $this->assertNull($extractor->extract(new InMemoryRawResult(['some' => 'data']))); |
| 26 | + } |
| 27 | + |
| 28 | + public function testItExtractsTokenUsage() |
| 29 | + { |
| 30 | + $extractor = new TokenUsageExtractor(); |
| 31 | + $result = new InMemoryRawResult([ |
| 32 | + 'usage' => [ |
| 33 | + 'prompt_tokens' => 20, |
| 34 | + 'total_tokens' => 20, |
| 35 | + ], |
| 36 | + ]); |
| 37 | + |
| 38 | + $tokenUsage = $extractor->extract($result); |
| 39 | + |
| 40 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 41 | + $this->assertSame(20, $tokenUsage->getPromptTokens()); |
| 42 | + $this->assertNull($tokenUsage->getCompletionTokens()); |
| 43 | + $this->assertSame(20, $tokenUsage->getTotalTokens()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testItHandlesMissingUsageFields() |
| 47 | + { |
| 48 | + $extractor = new TokenUsageExtractor(); |
| 49 | + $result = new InMemoryRawResult([ |
| 50 | + 'usage' => [ |
| 51 | + 'prompt_tokens' => 5, |
| 52 | + ], |
| 53 | + ]); |
| 54 | + |
| 55 | + $tokenUsage = $extractor->extract($result); |
| 56 | + |
| 57 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 58 | + $this->assertSame(5, $tokenUsage->getPromptTokens()); |
| 59 | + $this->assertNull($tokenUsage->getTotalTokens()); |
| 60 | + } |
| 61 | +} |
0 commit comments