|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Blog; |
| 6 | + |
| 7 | +use App\Blog\Embedder; |
| 8 | +use App\Blog\Loader; |
| 9 | +use App\Blog\Post; |
| 10 | +use Codewithkyrian\ChromaDB\Client; |
| 11 | +use Codewithkyrian\ChromaDB\Resources\CollectionResource; |
| 12 | +use PhpLlm\LlmChain\Document\Vector; |
| 13 | +use PhpLlm\LlmChain\Model\Model; |
| 14 | +use PhpLlm\LlmChain\Model\Response\AsyncResponse; |
| 15 | +use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; |
| 16 | +use PhpLlm\LlmChain\Model\Response\VectorResponse; |
| 17 | +use PhpLlm\LlmChain\Platform\ResponseConverter; |
| 18 | +use PhpLlm\LlmChain\PlatformInterface; |
| 19 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 20 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 23 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 24 | +use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; |
| 25 | + |
| 26 | +#[CoversClass(Embedder::class)] |
| 27 | +#[UsesClass(Loader::class)] |
| 28 | +#[UsesClass(Post::class)] |
| 29 | +final class EmbedderTest extends TestCase |
| 30 | +{ |
| 31 | + public function testEmbedBlog(): void |
| 32 | + { |
| 33 | + $response = MockResponse::fromFile(__DIR__.'/fixtures/blog.rss'); |
| 34 | + $client = new MockHttpClient([$response, $response]); |
| 35 | + $loader = new Loader($client); |
| 36 | + $platform = $this->createMock(PlatformInterface::class); |
| 37 | + $chromaClient = $this->createMock(Client::class); |
| 38 | + $posts = $loader->load(); |
| 39 | + $vectors = [ |
| 40 | + new Vector([0.1, 0.2, 0.3]), |
| 41 | + new Vector([0.4, 0.5, 0.6]), |
| 42 | + new Vector([0.7, 0.8, 0.9]), |
| 43 | + new Vector([1.0, 1.1, 1.2]), |
| 44 | + new Vector([1.3, 1.4, 1.5]), |
| 45 | + new Vector([1.6, 1.7, 1.8]), |
| 46 | + new Vector([1.9, 2.0, 2.1]), |
| 47 | + new Vector([2.2, 2.3, 2.4]), |
| 48 | + new Vector([2.5, 2.6, 2.7]), |
| 49 | + new Vector([2.8, 2.9, 3.0]), |
| 50 | + ]; |
| 51 | + $platform |
| 52 | + ->method('request') |
| 53 | + ->willReturn($this->createAsyncResponse($vectors)); |
| 54 | + |
| 55 | + $collection = $this->createMock(CollectionResource::class); |
| 56 | + $chromaClient |
| 57 | + ->expects($this->once()) |
| 58 | + ->method('getOrCreateCollection') |
| 59 | + ->with('symfony_blog') |
| 60 | + ->willReturn($collection); |
| 61 | + |
| 62 | + $collection |
| 63 | + ->expects($this->once()) |
| 64 | + ->method('upsert') |
| 65 | + ->with( |
| 66 | + array_map(fn (Post $post) => $post->id, $posts), |
| 67 | + array_map(fn (Vector $vector) => $vector->getData(), $vectors), |
| 68 | + $posts, |
| 69 | + ); |
| 70 | + |
| 71 | + $embedder = new Embedder($loader, $platform, $chromaClient); |
| 72 | + $embedder->embedBlog(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param Vector[] $vectors |
| 77 | + */ |
| 78 | + private function createAsyncResponse(array $vectors): AsyncResponse |
| 79 | + { |
| 80 | + $converter = new class($vectors) implements ResponseConverter { |
| 81 | + /** |
| 82 | + * @param Vector[] $vectors |
| 83 | + */ |
| 84 | + public function __construct(private readonly array $vectors) |
| 85 | + { |
| 86 | + } |
| 87 | + |
| 88 | + public function supports(Model $model, object|array|string $input): bool |
| 89 | + { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + public function convert(HttpResponse $response, array $options = []): LlmResponse |
| 94 | + { |
| 95 | + return new VectorResponse(...$this->vectors); |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + return new AsyncResponse($converter, new MockResponse()); |
| 100 | + } |
| 101 | +} |
0 commit comments