|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Tests\Platform\Bridge\Albert; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Platform\Bridge\Albert\EmbeddingsModelClient; |
| 8 | +use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings; |
| 9 | +use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT; |
| 10 | +use PhpLlm\LlmChain\Platform\Exception\InvalidArgumentException; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 13 | +use PHPUnit\Framework\Attributes\Small; |
| 14 | +use PHPUnit\Framework\Attributes\Test; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 17 | +use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 18 | + |
| 19 | +#[CoversClass(EmbeddingsModelClient::class)] |
| 20 | +#[Small] |
| 21 | +final class EmbeddingsModelClientTest extends TestCase |
| 22 | +{ |
| 23 | + #[Test] |
| 24 | + public function constructorThrowsExceptionForEmptyApiKey(): void |
| 25 | + { |
| 26 | + $this->expectException(InvalidArgumentException::class); |
| 27 | + $this->expectExceptionMessage('The API key must not be empty.'); |
| 28 | + |
| 29 | + new EmbeddingsModelClient( |
| 30 | + new MockHttpClient(), |
| 31 | + '', |
| 32 | + 'https://albert.example.com/' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + public function constructorThrowsExceptionForEmptyBaseUrl(): void |
| 38 | + { |
| 39 | + $this->expectException(InvalidArgumentException::class); |
| 40 | + $this->expectExceptionMessage('The base URL must not be empty.'); |
| 41 | + |
| 42 | + new EmbeddingsModelClient( |
| 43 | + new MockHttpClient(), |
| 44 | + 'test-api-key', |
| 45 | + '' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + #[Test] |
| 50 | + public function supportsEmbeddingsModel(): void |
| 51 | + { |
| 52 | + $client = new EmbeddingsModelClient( |
| 53 | + new MockHttpClient(), |
| 54 | + 'test-api-key', |
| 55 | + 'https://albert.example.com/' |
| 56 | + ); |
| 57 | + |
| 58 | + $embeddingsModel = new Embeddings('text-embedding-ada-002'); |
| 59 | + self::assertTrue($client->supports($embeddingsModel)); |
| 60 | + } |
| 61 | + |
| 62 | + #[Test] |
| 63 | + public function doesNotSupportNonEmbeddingsModel(): void |
| 64 | + { |
| 65 | + $client = new EmbeddingsModelClient( |
| 66 | + new MockHttpClient(), |
| 67 | + 'test-api-key', |
| 68 | + 'https://albert.example.com/' |
| 69 | + ); |
| 70 | + |
| 71 | + $gptModel = new GPT('gpt-3.5-turbo'); |
| 72 | + self::assertFalse($client->supports($gptModel)); |
| 73 | + } |
| 74 | + |
| 75 | + #[Test] |
| 76 | + #[DataProvider('requestDataProvider')] |
| 77 | + public function requestSendsCorrectHttpRequest(array|string $payload, array $options, array|string $expectedJson): void |
| 78 | + { |
| 79 | + $capturedRequest = null; |
| 80 | + $httpClient = new MockHttpClient(function ($method, $url, $options) use (&$capturedRequest) { |
| 81 | + $capturedRequest = ['method' => $method, 'url' => $url, 'options' => $options]; |
| 82 | + |
| 83 | + return new JsonMockResponse(['data' => []]); |
| 84 | + }); |
| 85 | + |
| 86 | + $client = new EmbeddingsModelClient( |
| 87 | + $httpClient, |
| 88 | + 'test-api-key', |
| 89 | + 'https://albert.example.com/v1/' |
| 90 | + ); |
| 91 | + |
| 92 | + $model = new Embeddings('text-embedding-ada-002'); |
| 93 | + $response = $client->request($model, $payload, $options); |
| 94 | + |
| 95 | + self::assertNotNull($capturedRequest); |
| 96 | + self::assertSame('POST', $capturedRequest['method']); |
| 97 | + self::assertSame('https://albert.example.com/v1/embeddings', $capturedRequest['url']); |
| 98 | + self::assertArrayHasKey('normalized_headers', $capturedRequest['options']); |
| 99 | + self::assertArrayHasKey('authorization', $capturedRequest['options']['normalized_headers']); |
| 100 | + self::assertStringContainsString('Bearer test-api-key', $capturedRequest['options']['normalized_headers']['authorization'][0]); |
| 101 | + |
| 102 | + // Check JSON body - it might be in 'body' after processing |
| 103 | + if (isset($capturedRequest['options']['body'])) { |
| 104 | + $actualJson = json_decode($capturedRequest['options']['body'], true); |
| 105 | + self::assertEquals($expectedJson, $actualJson); |
| 106 | + } else { |
| 107 | + self::assertSame($expectedJson, $capturedRequest['options']['json']); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static function requestDataProvider(): \Iterator |
| 112 | + { |
| 113 | + yield 'with array payload and no options' => [ |
| 114 | + 'payload' => ['input' => 'test text', 'model' => 'text-embedding-ada-002'], |
| 115 | + 'options' => [], |
| 116 | + 'expectedJson' => ['input' => 'test text', 'model' => 'text-embedding-ada-002'], |
| 117 | + ]; |
| 118 | + |
| 119 | + yield 'with string payload and no options' => [ |
| 120 | + 'payload' => 'test text', |
| 121 | + 'options' => [], |
| 122 | + 'expectedJson' => 'test text', |
| 123 | + ]; |
| 124 | + |
| 125 | + yield 'with array payload and options' => [ |
| 126 | + 'payload' => ['input' => 'test text', 'model' => 'text-embedding-ada-002'], |
| 127 | + 'options' => ['dimensions' => 1536], |
| 128 | + 'expectedJson' => ['dimensions' => 1536, 'input' => 'test text', 'model' => 'text-embedding-ada-002'], |
| 129 | + ]; |
| 130 | + |
| 131 | + yield 'options override payload values' => [ |
| 132 | + 'payload' => ['input' => 'test text', 'model' => 'text-embedding-ada-002'], |
| 133 | + 'options' => ['model' => 'text-embedding-3-small'], |
| 134 | + 'expectedJson' => ['model' => 'text-embedding-3-small', 'input' => 'test text'], |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + #[Test] |
| 139 | + public function requestHandlesBaseUrlWithoutTrailingSlash(): void |
| 140 | + { |
| 141 | + $capturedUrl = null; |
| 142 | + $httpClient = new MockHttpClient(function ($method, $url) use (&$capturedUrl) { |
| 143 | + $capturedUrl = $url; |
| 144 | + |
| 145 | + return new JsonMockResponse(['data' => []]); |
| 146 | + }); |
| 147 | + |
| 148 | + $client = new EmbeddingsModelClient( |
| 149 | + $httpClient, |
| 150 | + 'test-api-key', |
| 151 | + 'https://albert.example.com/v1' |
| 152 | + ); |
| 153 | + |
| 154 | + $model = new Embeddings('text-embedding-ada-002'); |
| 155 | + $client->request($model, ['input' => 'test']); |
| 156 | + |
| 157 | + self::assertSame('https://albert.example.com/v1/embeddings', $capturedUrl); |
| 158 | + } |
| 159 | + |
| 160 | + #[Test] |
| 161 | + public function requestHandlesBaseUrlWithTrailingSlash(): void |
| 162 | + { |
| 163 | + $capturedUrl = null; |
| 164 | + $httpClient = new MockHttpClient(function ($method, $url) use (&$capturedUrl) { |
| 165 | + $capturedUrl = $url; |
| 166 | + |
| 167 | + return new JsonMockResponse(['data' => []]); |
| 168 | + }); |
| 169 | + |
| 170 | + $client = new EmbeddingsModelClient( |
| 171 | + $httpClient, |
| 172 | + 'test-api-key', |
| 173 | + 'https://albert.example.com/v1/' |
| 174 | + ); |
| 175 | + |
| 176 | + $model = new Embeddings('text-embedding-ada-002'); |
| 177 | + $client->request($model, ['input' => 'test']); |
| 178 | + |
| 179 | + self::assertSame('https://albert.example.com/v1/embeddings', $capturedUrl); |
| 180 | + } |
| 181 | +} |
0 commit comments