|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Tests\Platform\Bridge\Google\Embeddings; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings; |
| 8 | +use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\ModelClient; |
| 9 | +use PhpLlm\LlmChain\Platform\Response\VectorResponse; |
| 10 | +use PhpLlm\LlmChain\Platform\Vector\Vector; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\Attributes\Small; |
| 13 | +use PHPUnit\Framework\Attributes\Test; |
| 14 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 17 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 18 | + |
| 19 | +#[CoversClass(ModelClient::class)] |
| 20 | +#[Small] |
| 21 | +#[UsesClass(Vector::class)] |
| 22 | +#[UsesClass(VectorResponse::class)] |
| 23 | +#[UsesClass(Embeddings::class)] |
| 24 | +final class EmbeddingsModelClientTest extends TestCase |
| 25 | +{ |
| 26 | + #[Test] |
| 27 | + public function itMakesARequestWithCorrectPayload(): void |
| 28 | + { |
| 29 | + $response = $this->createStub(ResponseInterface::class); |
| 30 | + $response |
| 31 | + ->method('toArray') |
| 32 | + ->willReturn(json_decode($this->getEmbeddingStub(), true)); |
| 33 | + |
| 34 | + $httpClient = self::createMock(HttpClientInterface::class); |
| 35 | + $httpClient->expects(self::once()) |
| 36 | + ->method('request') |
| 37 | + ->with( |
| 38 | + 'POST', |
| 39 | + 'https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-exp-03-07:batchEmbedContents', |
| 40 | + [ |
| 41 | + 'headers' => ['x-goog-api-key' => 'test'], |
| 42 | + 'json' => [ |
| 43 | + 'requests' => [ |
| 44 | + [ |
| 45 | + 'model' => 'models/gemini-embedding-exp-03-07', |
| 46 | + 'content' => ['parts' => [['text' => 'payload1']]], |
| 47 | + 'outputDimensionality' => 1536, |
| 48 | + 'taskType' => 'CLASSIFICATION', |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'model' => 'models/gemini-embedding-exp-03-07', |
| 52 | + 'content' => ['parts' => [['text' => 'payload2']]], |
| 53 | + 'outputDimensionality' => 1536, |
| 54 | + 'taskType' => 'CLASSIFICATION', |
| 55 | + ], |
| 56 | + ], |
| 57 | + ], |
| 58 | + ], |
| 59 | + ) |
| 60 | + ->willReturn($response); |
| 61 | + |
| 62 | + $model = new Embeddings(Embeddings::GEMINI_EMBEDDING_EXP_03_07, ['dimensions' => 1536, 'task_type' => 'CLASSIFICATION']); |
| 63 | + |
| 64 | + $httpResponse = (new ModelClient($httpClient, 'test'))->request($model, ['payload1', 'payload2']); |
| 65 | + self::assertSame(json_decode($this->getEmbeddingStub(), true), $httpResponse->toArray()); |
| 66 | + } |
| 67 | + |
| 68 | + #[Test] |
| 69 | + public function itConvertsAResponseToAVectorResponse(): void |
| 70 | + { |
| 71 | + $response = $this->createStub(ResponseInterface::class); |
| 72 | + $response |
| 73 | + ->method('toArray') |
| 74 | + ->willReturn(json_decode($this->getEmbeddingStub(), true)); |
| 75 | + |
| 76 | + $httpClient = self::createMock(HttpClientInterface::class); |
| 77 | + |
| 78 | + $vectorResponse = (new ModelClient($httpClient, 'test'))->convert($response); |
| 79 | + $convertedContent = $vectorResponse->getContent(); |
| 80 | + |
| 81 | + self::assertCount(2, $convertedContent); |
| 82 | + |
| 83 | + self::assertSame([0.3, 0.4, 0.4], $convertedContent[0]->getData()); |
| 84 | + self::assertSame([0.0, 0.0, 0.2], $convertedContent[1]->getData()); |
| 85 | + } |
| 86 | + |
| 87 | + private function getEmbeddingStub(): string |
| 88 | + { |
| 89 | + return <<<'JSON' |
| 90 | + { |
| 91 | + "embeddings": [ |
| 92 | + { |
| 93 | + "values": [0.3, 0.4, 0.4] |
| 94 | + }, |
| 95 | + { |
| 96 | + "values": [0.0, 0.0, 0.2] |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | + JSON; |
| 101 | + } |
| 102 | +} |
0 commit comments