|
| 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\Gemini; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Small; |
| 16 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Symfony\AI\Agent\Output; |
| 19 | +use Symfony\AI\Platform\Bridge\Gemini\TokenOutputProcessor; |
| 20 | +use Symfony\AI\Platform\Message\MessageBag; |
| 21 | +use Symfony\AI\Platform\Metadata\Metadata; |
| 22 | +use Symfony\AI\Platform\Metadata\TokenUsage; |
| 23 | +use Symfony\AI\Platform\Model; |
| 24 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 25 | +use Symfony\AI\Platform\Result\ResultInterface; |
| 26 | +use Symfony\AI\Platform\Result\StreamResult; |
| 27 | +use Symfony\AI\Platform\Result\TextResult; |
| 28 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 29 | + |
| 30 | +#[CoversClass(TokenOutputProcessor::class)] |
| 31 | +#[UsesClass(Output::class)] |
| 32 | +#[UsesClass(TextResult::class)] |
| 33 | +#[UsesClass(StreamResult::class)] |
| 34 | +#[UsesClass(Metadata::class)] |
| 35 | +#[UsesClass(TokenUsage::class)] |
| 36 | +#[Small] |
| 37 | +final class TokenOutputProcessorTest extends TestCase |
| 38 | +{ |
| 39 | + public function testItHandlesStreamResponsesWithoutProcessing() |
| 40 | + { |
| 41 | + $processor = new TokenOutputProcessor(); |
| 42 | + $streamResult = new StreamResult((static function () { yield 'test'; })()); |
| 43 | + $output = $this->createOutput($streamResult); |
| 44 | + |
| 45 | + $processor->processOutput($output); |
| 46 | + |
| 47 | + $metadata = $output->result->getMetadata(); |
| 48 | + $this->assertCount(0, $metadata); |
| 49 | + } |
| 50 | + |
| 51 | + public function testItDoesNothingWithoutRawResponse() |
| 52 | + { |
| 53 | + $processor = new TokenOutputProcessor(); |
| 54 | + $textResult = new TextResult('test'); |
| 55 | + $output = $this->createOutput($textResult); |
| 56 | + |
| 57 | + $processor->processOutput($output); |
| 58 | + |
| 59 | + $metadata = $output->result->getMetadata(); |
| 60 | + $this->assertCount(0, $metadata); |
| 61 | + } |
| 62 | + |
| 63 | + public function testItAddsRemainingTokensToMetadata() |
| 64 | + { |
| 65 | + $processor = new TokenOutputProcessor(); |
| 66 | + $textResult = new TextResult('test'); |
| 67 | + |
| 68 | + $textResult->setRawResult($this->createRawResult()); |
| 69 | + |
| 70 | + $output = $this->createOutput($textResult); |
| 71 | + |
| 72 | + $processor->processOutput($output); |
| 73 | + |
| 74 | + $metadata = $output->result->getMetadata(); |
| 75 | + $tokenUsage = $metadata->get('token_usage'); |
| 76 | + |
| 77 | + $this->assertCount(1, $metadata); |
| 78 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 79 | + $this->assertNull($tokenUsage->remainingTokens); |
| 80 | + } |
| 81 | + |
| 82 | + public function testItAddsUsageTokensToMetadata() |
| 83 | + { |
| 84 | + $processor = new TokenOutputProcessor(); |
| 85 | + $textResult = new TextResult('test'); |
| 86 | + |
| 87 | + $rawResult = $this->createRawResult([ |
| 88 | + 'usageMetadata' => [ |
| 89 | + 'promptTokenCount' => 10, |
| 90 | + 'candidatesTokenCount' => 20, |
| 91 | + 'totalTokenCount' => 50, |
| 92 | + 'thoughtsTokenCount' => 20, |
| 93 | + 'cachedContentTokenCount' => 40, |
| 94 | + 'toolUsePromptTokenCount' => 5, |
| 95 | + ], |
| 96 | + ]); |
| 97 | + |
| 98 | + $textResult->setRawResult($rawResult); |
| 99 | + |
| 100 | + $output = $this->createOutput($textResult); |
| 101 | + |
| 102 | + $processor->processOutput($output); |
| 103 | + |
| 104 | + $metadata = $output->result->getMetadata(); |
| 105 | + $tokenUsage = $metadata->get('token_usage'); |
| 106 | + |
| 107 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 108 | + $this->assertSame(10, $tokenUsage->promptTokens); |
| 109 | + $this->assertSame(5, $tokenUsage->toolTokens); |
| 110 | + $this->assertSame(20, $tokenUsage->completionTokens); |
| 111 | + $this->assertNull($tokenUsage->remainingTokens); |
| 112 | + $this->assertSame(20, $tokenUsage->thinkingTokens); |
| 113 | + $this->assertSame(40, $tokenUsage->cachedTokens); |
| 114 | + $this->assertSame(50, $tokenUsage->totalTokens); |
| 115 | + } |
| 116 | + |
| 117 | + public function testItHandlesMissingUsageFields() |
| 118 | + { |
| 119 | + $processor = new TokenOutputProcessor(); |
| 120 | + $textResult = new TextResult('test'); |
| 121 | + |
| 122 | + $rawResult = $this->createRawResult([ |
| 123 | + 'usageMetadata' => [ |
| 124 | + // Missing some fields |
| 125 | + 'promptTokenCount' => 10, |
| 126 | + ], |
| 127 | + ]); |
| 128 | + |
| 129 | + $textResult->setRawResult($rawResult); |
| 130 | + |
| 131 | + $output = $this->createOutput($textResult); |
| 132 | + |
| 133 | + $processor->processOutput($output); |
| 134 | + |
| 135 | + $metadata = $output->result->getMetadata(); |
| 136 | + $tokenUsage = $metadata->get('token_usage'); |
| 137 | + |
| 138 | + $this->assertInstanceOf(TokenUsage::class, $tokenUsage); |
| 139 | + $this->assertSame(10, $tokenUsage->promptTokens); |
| 140 | + $this->assertNull($tokenUsage->remainingTokens); |
| 141 | + $this->assertNull($tokenUsage->completionTokens); |
| 142 | + $this->assertNull($tokenUsage->totalTokens); |
| 143 | + } |
| 144 | + |
| 145 | + private function createRawResult(array $data = []): RawHttpResult |
| 146 | + { |
| 147 | + $rawResponse = $this->createStub(ResponseInterface::class); |
| 148 | + $rawResponse->method('toArray')->willReturn($data); |
| 149 | + |
| 150 | + return new RawHttpResult($rawResponse); |
| 151 | + } |
| 152 | + |
| 153 | + private function createOutput(ResultInterface $result): Output |
| 154 | + { |
| 155 | + return new Output( |
| 156 | + $this->createStub(Model::class), |
| 157 | + $result, |
| 158 | + $this->createStub(MessageBag::class), |
| 159 | + [], |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments