|
| 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\Voyage; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\Attributes\Small; |
| 17 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\AI\Platform\Bridge\Voyage\ResultConverter; |
| 20 | +use Symfony\AI\Platform\Bridge\Voyage\Voyage; |
| 21 | +use Symfony\AI\Platform\Exception\RuntimeException; |
| 22 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 23 | +use Symfony\AI\Platform\Result\VectorResult; |
| 24 | +use Symfony\AI\Platform\Vector\Vector; |
| 25 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Oskar Stark <[email protected]> |
| 29 | + */ |
| 30 | +#[CoversClass(ResultConverter::class)] |
| 31 | +#[Small] |
| 32 | +#[UsesClass(Vector::class)] |
| 33 | +#[UsesClass(VectorResult::class)] |
| 34 | +#[UsesClass(Voyage::class)] |
| 35 | +class ResultConverterTest extends TestCase |
| 36 | +{ |
| 37 | + public function testItConvertsAResponseToAVectorResult() |
| 38 | + { |
| 39 | + $result = $this->createStub(ResponseInterface::class); |
| 40 | + $result |
| 41 | + ->method('toArray') |
| 42 | + ->willReturn([ |
| 43 | + 'data' => [ |
| 44 | + [ |
| 45 | + 'embedding' => [0.1, 0.2, 0.3], |
| 46 | + ], |
| 47 | + ], |
| 48 | + ]); |
| 49 | + |
| 50 | + $converter = new ResultConverter(); |
| 51 | + $vectorResult = $converter->convert(new RawHttpResult($result)); |
| 52 | + |
| 53 | + $this->assertInstanceOf(VectorResult::class, $vectorResult); |
| 54 | + $this->assertSame([0.1, 0.2, 0.3], $vectorResult->getContent()[0]->getData()); |
| 55 | + } |
| 56 | + |
| 57 | + public function testItConvertsMultipleEmbeddings() |
| 58 | + { |
| 59 | + $result = $this->createStub(ResponseInterface::class); |
| 60 | + $result |
| 61 | + ->method('toArray') |
| 62 | + ->willReturn([ |
| 63 | + 'data' => [ |
| 64 | + [ |
| 65 | + 'embedding' => [0.1, 0.2, 0.3], |
| 66 | + ], |
| 67 | + [ |
| 68 | + 'embedding' => [0.4, 0.5, 0.6], |
| 69 | + ], |
| 70 | + ], |
| 71 | + ]); |
| 72 | + |
| 73 | + $converter = new ResultConverter(); |
| 74 | + $vectorResult = $converter->convert(new RawHttpResult($result)); |
| 75 | + |
| 76 | + $this->assertInstanceOf(VectorResult::class, $vectorResult); |
| 77 | + // The converter returns only the first vector |
| 78 | + $this->assertSame([0.1, 0.2, 0.3], $vectorResult->getContent()[0]->getData()); |
| 79 | + } |
| 80 | + |
| 81 | + public function testItThrowsExceptionWhenResponseDoesNotContainData() |
| 82 | + { |
| 83 | + $result = $this->createStub(ResponseInterface::class); |
| 84 | + $result |
| 85 | + ->method('toArray') |
| 86 | + ->willReturn(['invalid' => 'response']); |
| 87 | + |
| 88 | + $converter = new ResultConverter(); |
| 89 | + |
| 90 | + $this->expectException(RuntimeException::class); |
| 91 | + $this->expectExceptionMessage('Response does not contain embedding data.'); |
| 92 | + |
| 93 | + $converter->convert(new RawHttpResult($result)); |
| 94 | + } |
| 95 | + |
| 96 | + #[DataProvider('voyageModelsProvider')] |
| 97 | + public function testItSupportsVoyageModel(string $modelName) |
| 98 | + { |
| 99 | + $converter = new ResultConverter(); |
| 100 | + |
| 101 | + $this->assertTrue($converter->supports(new Voyage($modelName))); |
| 102 | + } |
| 103 | + |
| 104 | + public static function voyageModelsProvider(): iterable |
| 105 | + { |
| 106 | + yield 'V3' => [Voyage::V3]; |
| 107 | + yield 'V3_LITE' => [Voyage::V3_LITE]; |
| 108 | + yield 'FINANCE_2' => [Voyage::FINANCE_2]; |
| 109 | + yield 'MULTILINGUAL_2' => [Voyage::MULTILINGUAL_2]; |
| 110 | + yield 'LAW_2' => [Voyage::LAW_2]; |
| 111 | + yield 'CODE_2' => [Voyage::CODE_2]; |
| 112 | + } |
| 113 | +} |
0 commit comments