|
| 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\Tests\Bridge\Ollama; |
| 13 | + |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\AI\Platform\Bridge\Ollama\TokenUsageExtractor; |
| 17 | +use Symfony\AI\Platform\Result\InMemoryRawResult; |
| 18 | +use Symfony\AI\Platform\TokenUsage\TokenUsage; |
| 19 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 20 | + |
| 21 | +final class TokenUsageExtractorTest extends TestCase |
| 22 | +{ |
| 23 | + public function testItHandlesStreamResponsesWithoutProcessing() |
| 24 | + { |
| 25 | + $extractor = new TokenUsageExtractor(); |
| 26 | + |
| 27 | + $this->assertNull($extractor->extract(new InMemoryRawResult(), ['stream' => true])); |
| 28 | + } |
| 29 | + |
| 30 | + public function testItDoesNothingWithoutUsageData() |
| 31 | + { |
| 32 | + $extractor = new TokenUsageExtractor(); |
| 33 | + |
| 34 | + $this->assertNull($extractor->extract(new InMemoryRawResult(['some' => 'data']))); |
| 35 | + } |
| 36 | + |
| 37 | + public function testItExtractsTokenUsage() |
| 38 | + { |
| 39 | + $extractor = new TokenUsageExtractor(); |
| 40 | + $result = new InMemoryRawResult([], object: $this->createResponseObject()); |
| 41 | + |
| 42 | + $tokenUsage = $extractor->extract($result); |
| 43 | + |
| 44 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 45 | + $this->assertSame(10, $tokenUsage->getPromptTokens()); |
| 46 | + $this->assertSame(10, $tokenUsage->getCompletionTokens()); |
| 47 | + } |
| 48 | + |
| 49 | + public function testItExtractsTokenUsageFromStreamResult() |
| 50 | + { |
| 51 | + $extractor = new TokenUsageExtractor(); |
| 52 | + |
| 53 | + $result = new InMemoryRawResult([], [ |
| 54 | + [ |
| 55 | + 'model' => 'foo', |
| 56 | + 'response' => 'First chunk', |
| 57 | + 'done' => false, |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'model' => 'foo', |
| 61 | + 'response' => 'Hello World!', |
| 62 | + 'done' => true, |
| 63 | + 'prompt_eval_count' => 10, |
| 64 | + 'eval_count' => 10, |
| 65 | + ], |
| 66 | + ], object: $this->createResponseObject()); |
| 67 | + |
| 68 | + $tokenUsage = $extractor->extract($result, ['stream' => true]); |
| 69 | + |
| 70 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 71 | + $this->assertSame(10, $tokenUsage->getPromptTokens()); |
| 72 | + $this->assertSame(10, $tokenUsage->getCompletionTokens()); |
| 73 | + } |
| 74 | + |
| 75 | + private function createResponseObject(): ResponseInterface|MockObject |
| 76 | + { |
| 77 | + $response = $this->createStub(ResponseInterface::class); |
| 78 | + $response->method('toArray')->willReturn([ |
| 79 | + 'model' => 'foo', |
| 80 | + 'response' => 'Hello World!', |
| 81 | + 'done' => true, |
| 82 | + 'prompt_eval_count' => 10, |
| 83 | + 'eval_count' => 10, |
| 84 | + ]); |
| 85 | + |
| 86 | + return $response; |
| 87 | + } |
| 88 | +} |
0 commit comments