|
| 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\VertexAi; |
| 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\VertexAi\TokenOutputProcessor; |
| 20 | +use Symfony\AI\Platform\Message\MessageBagInterface; |
| 21 | +use Symfony\AI\Platform\Model; |
| 22 | +use Symfony\AI\Platform\Result\Metadata\Metadata; |
| 23 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 24 | +use Symfony\AI\Platform\Result\ResultInterface; |
| 25 | +use Symfony\AI\Platform\Result\StreamResult; |
| 26 | +use Symfony\AI\Platform\Result\TextResult; |
| 27 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 28 | + |
| 29 | +#[CoversClass(TokenOutputProcessor::class)] |
| 30 | +#[UsesClass(Output::class)] |
| 31 | +#[UsesClass(TextResult::class)] |
| 32 | +#[UsesClass(StreamResult::class)] |
| 33 | +#[UsesClass(Metadata::class)] |
| 34 | +#[Small] |
| 35 | +final class TokenOutputProcessorTest extends TestCase |
| 36 | +{ |
| 37 | + public function testItDoesNothingWithoutRawResponse() |
| 38 | + { |
| 39 | + $processor = new TokenOutputProcessor(); |
| 40 | + $textResult = new TextResult('test'); |
| 41 | + $output = $this->createOutput($textResult); |
| 42 | + |
| 43 | + $processor->processOutput($output); |
| 44 | + |
| 45 | + $this->assertCount(0, $output->result->getMetadata()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testItAddsUsageTokensToMetadata() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + $textResult = new TextResult('test'); |
| 52 | + |
| 53 | + $rawResponse = $this->createRawResponse([ |
| 54 | + 'usageMetadata' => [ |
| 55 | + 'promptTokenCount' => 10, |
| 56 | + 'candidatesTokenCount' => 20, |
| 57 | + 'thoughtsTokenCount' => 20, |
| 58 | + 'totalTokenCount' => 50, |
| 59 | + ], |
| 60 | + ]); |
| 61 | + |
| 62 | + $textResult->setRawResult($rawResponse); |
| 63 | + $processor = new TokenOutputProcessor(); |
| 64 | + $output = $this->createOutput($textResult); |
| 65 | + |
| 66 | + // Act |
| 67 | + $processor->processOutput($output); |
| 68 | + |
| 69 | + // Assert |
| 70 | + $metadata = $output->result->getMetadata(); |
| 71 | + $this->assertCount(4, $metadata); |
| 72 | + $this->assertSame(10, $metadata->get('prompt_tokens')); |
| 73 | + $this->assertSame(20, $metadata->get('completion_tokens')); |
| 74 | + $this->assertSame(20, $metadata->get('thinking_tokens')); |
| 75 | + $this->assertSame(50, $metadata->get('total_tokens')); |
| 76 | + } |
| 77 | + |
| 78 | + public function testItHandlesMissingUsageFields() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + $textResult = new TextResult('test'); |
| 82 | + |
| 83 | + $rawResponse = $this->createRawResponse([ |
| 84 | + 'usageMetadata' => [ |
| 85 | + 'promptTokenCount' => 10, |
| 86 | + ], |
| 87 | + ]); |
| 88 | + |
| 89 | + $textResult->setRawResult($rawResponse); |
| 90 | + $processor = new TokenOutputProcessor(); |
| 91 | + $output = $this->createOutput($textResult); |
| 92 | + |
| 93 | + // Act |
| 94 | + $processor->processOutput($output); |
| 95 | + |
| 96 | + // Assert |
| 97 | + $metadata = $output->result->getMetadata(); |
| 98 | + $this->assertCount(4, $metadata); |
| 99 | + $this->assertSame(10, $metadata->get('prompt_tokens')); |
| 100 | + $this->assertNull($metadata->get('completion_tokens')); |
| 101 | + $this->assertNull($metadata->get('completion_tokens')); |
| 102 | + $this->assertNull($metadata->get('total_tokens')); |
| 103 | + } |
| 104 | + |
| 105 | + private function createRawResponse(array $data = []): RawHttpResult |
| 106 | + { |
| 107 | + $rawResponse = $this->createStub(ResponseInterface::class); |
| 108 | + |
| 109 | + $rawResponse->method('toArray')->willReturn($data); |
| 110 | + |
| 111 | + return new RawHttpResult($rawResponse); |
| 112 | + } |
| 113 | + |
| 114 | + private function createOutput(ResultInterface $result): Output |
| 115 | + { |
| 116 | + return new Output( |
| 117 | + $this->createStub(Model::class), |
| 118 | + $result, |
| 119 | + $this->createStub(MessageBagInterface::class), |
| 120 | + [], |
| 121 | + ); |
| 122 | + } |
| 123 | +} |
0 commit comments