|
| 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\ModelCatalog; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\AI\Platform\Capability; |
| 16 | +use Symfony\AI\Platform\Exception\InvalidArgumentException; |
| 17 | +use Symfony\AI\Platform\Model; |
| 18 | +use Symfony\AI\Platform\ModelCatalog\AbstractModelCatalog; |
| 19 | + |
| 20 | +final class AbstractModelCatalogTest extends TestCase |
| 21 | +{ |
| 22 | + public function testGetModelWithoutQueryParameters() |
| 23 | + { |
| 24 | + $catalog = $this->createTestCatalog(); |
| 25 | + $model = $catalog->getModel('test-model'); |
| 26 | + |
| 27 | + $this->assertSame('test-model', $model->getName()); |
| 28 | + $this->assertSame([], $model->getOptions()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testGetModelWithStringQueryParameter() |
| 32 | + { |
| 33 | + $catalog = $this->createTestCatalog(); |
| 34 | + $model = $catalog->getModel('test-model?param=value'); |
| 35 | + |
| 36 | + $this->assertSame('test-model', $model->getName()); |
| 37 | + $this->assertSame(['param' => 'value'], $model->getOptions()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testGetModelWithIntegerQueryParameter() |
| 41 | + { |
| 42 | + $catalog = $this->createTestCatalog(); |
| 43 | + $model = $catalog->getModel('test-model?max_tokens=500'); |
| 44 | + |
| 45 | + $this->assertSame('test-model', $model->getName()); |
| 46 | + $options = $model->getOptions(); |
| 47 | + $this->assertArrayHasKey('max_tokens', $options); |
| 48 | + $this->assertIsInt($options['max_tokens']); |
| 49 | + $this->assertSame(500, $options['max_tokens']); |
| 50 | + } |
| 51 | + |
| 52 | + public function testGetModelWithMultipleQueryParameters() |
| 53 | + { |
| 54 | + $catalog = $this->createTestCatalog(); |
| 55 | + $model = $catalog->getModel('test-model?max_tokens=500&temperature=0.7&stream=true'); |
| 56 | + |
| 57 | + $this->assertSame('test-model', $model->getName()); |
| 58 | + $options = $model->getOptions(); |
| 59 | + |
| 60 | + $this->assertArrayHasKey('max_tokens', $options); |
| 61 | + $this->assertIsInt($options['max_tokens']); |
| 62 | + $this->assertSame(500, $options['max_tokens']); |
| 63 | + |
| 64 | + $this->assertArrayHasKey('temperature', $options); |
| 65 | + $this->assertIsFloat($options['temperature']); |
| 66 | + $this->assertSame(0.7, $options['temperature']); |
| 67 | + |
| 68 | + $this->assertArrayHasKey('stream', $options); |
| 69 | + $this->assertSame('true', $options['stream']); |
| 70 | + } |
| 71 | + |
| 72 | + public function testGetModelWithNestedArrayQueryParameters() |
| 73 | + { |
| 74 | + $catalog = $this->createTestCatalog(); |
| 75 | + $model = $catalog->getModel('test-model?options[max_tokens]=500&options[temperature]=0.7&options[metadata][version]=1'); |
| 76 | + |
| 77 | + $this->assertSame('test-model', $model->getName()); |
| 78 | + $options = $model->getOptions(); |
| 79 | + |
| 80 | + $this->assertIsArray($options['options']); |
| 81 | + $this->assertSame(500, $options['options']['max_tokens']); |
| 82 | + $this->assertIsInt($options['options']['max_tokens']); |
| 83 | + $this->assertSame(0.7, $options['options']['temperature']); |
| 84 | + $this->assertIsFloat($options['options']['temperature']); |
| 85 | + $this->assertIsArray($options['options']['metadata']); |
| 86 | + $this->assertSame(1, $options['options']['metadata']['version']); |
| 87 | + $this->assertIsInt($options['options']['metadata']['version']); |
| 88 | + } |
| 89 | + |
| 90 | + public function testGetModelWithEmptyModelNameThrowsException() |
| 91 | + { |
| 92 | + $catalog = $this->createTestCatalog(); |
| 93 | + |
| 94 | + $this->expectException(InvalidArgumentException::class); |
| 95 | + $this->expectExceptionMessage('Model name cannot be empty.'); |
| 96 | + |
| 97 | + /* @phpstan-ignore argument.type */ |
| 98 | + $catalog->getModel(''); |
| 99 | + } |
| 100 | + |
| 101 | + public function testGetModelWithOnlyQueryStringThrowsException() |
| 102 | + { |
| 103 | + $catalog = $this->createTestCatalog(); |
| 104 | + |
| 105 | + $this->expectException(InvalidArgumentException::class); |
| 106 | + $this->expectExceptionMessage('Model name cannot be empty.'); |
| 107 | + |
| 108 | + $catalog->getModel('?max_tokens=500'); |
| 109 | + } |
| 110 | + |
| 111 | + public function testNumericStringsAreConvertedRecursively() |
| 112 | + { |
| 113 | + $catalog = $this->createTestCatalog(); |
| 114 | + $model = $catalog->getModel('test-model?a[b][c]=123&a[b][d]=text&a[e]=456'); |
| 115 | + |
| 116 | + $options = $model->getOptions(); |
| 117 | + |
| 118 | + $this->assertIsArray($options['a']); |
| 119 | + $this->assertIsArray($options['a']['b']); |
| 120 | + $this->assertSame(123, $options['a']['b']['c']); |
| 121 | + $this->assertIsInt($options['a']['b']['c']); |
| 122 | + $this->assertSame('text', $options['a']['b']['d']); |
| 123 | + $this->assertIsString($options['a']['b']['d']); |
| 124 | + $this->assertSame(456, $options['a']['e']); |
| 125 | + $this->assertIsInt($options['a']['e']); |
| 126 | + } |
| 127 | + |
| 128 | + private function createTestCatalog(): AbstractModelCatalog |
| 129 | + { |
| 130 | + return new class extends AbstractModelCatalog { |
| 131 | + public function __construct() |
| 132 | + { |
| 133 | + $this->models = [ |
| 134 | + 'test-model' => [ |
| 135 | + 'class' => Model::class, |
| 136 | + 'capabilities' => [Capability::INPUT_TEXT], |
| 137 | + ], |
| 138 | + ]; |
| 139 | + } |
| 140 | + }; |
| 141 | + } |
| 142 | +} |
0 commit comments