|
| 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\Store\Bridge\Cache; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Symfony\AI\Platform\Vector\Vector; |
| 16 | +use Symfony\AI\Store\Distance\DistanceCalculator; |
| 17 | +use Symfony\AI\Store\Document\Metadata; |
| 18 | +use Symfony\AI\Store\Document\VectorDocument; |
| 19 | +use Symfony\AI\Store\Exception\InvalidArgumentException; |
| 20 | +use Symfony\AI\Store\Exception\RuntimeException; |
| 21 | +use Symfony\AI\Store\ManagedStoreInterface; |
| 22 | +use Symfony\AI\Store\StoreInterface; |
| 23 | +use Symfony\Component\Uid\Uuid; |
| 24 | +use Symfony\Contracts\Cache\CacheInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Guillaume Loulier <[email protected]> |
| 28 | + */ |
| 29 | +final class Store implements ManagedStoreInterface, StoreInterface |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly CacheInterface&CacheItemPoolInterface $cache, |
| 33 | + private readonly DistanceCalculator $distanceCalculator = new DistanceCalculator(), |
| 34 | + private readonly string $cacheKey = '_vectors', |
| 35 | + ) { |
| 36 | + if (!interface_exists(CacheInterface::class)) { |
| 37 | + throw new RuntimeException('For using the Cache store as vector store, a symfony/contracts cache implementation is required. Try running "composer require symfony/cache" or another symfony/contracts compatible cache.'); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function setup(array $options = []): void |
| 42 | + { |
| 43 | + if ([] !== $options) { |
| 44 | + throw new InvalidArgumentException('No supported options.'); |
| 45 | + } |
| 46 | + |
| 47 | + if ($this->cache->hasItem($this->cacheKey)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $this->cache->get($this->cacheKey, static fn (): array => []); |
| 52 | + } |
| 53 | + |
| 54 | + public function add(VectorDocument ...$documents): void |
| 55 | + { |
| 56 | + $existingVectors = $this->cache->get($this->cacheKey, static fn (): array => []); |
| 57 | + |
| 58 | + $newVectors = array_map(static fn (VectorDocument $document): array => [ |
| 59 | + 'id' => $document->id->toRfc4122(), |
| 60 | + 'vector' => $document->vector->getData(), |
| 61 | + 'metadata' => $document->metadata->getArrayCopy(), |
| 62 | + ], $documents); |
| 63 | + |
| 64 | + $cacheItem = $this->cache->getItem($this->cacheKey); |
| 65 | + |
| 66 | + $cacheItem->set([ |
| 67 | + ...$existingVectors, |
| 68 | + ...$newVectors, |
| 69 | + ]); |
| 70 | + |
| 71 | + $this->cache->save($cacheItem); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param array{ |
| 76 | + * maxItems?: positive-int, |
| 77 | + * filter?: callable(VectorDocument): bool |
| 78 | + * } $options If maxItems is provided, only the top N results will be returned. |
| 79 | + * If filter is provided, only documents matching the filter will be considered. |
| 80 | + */ |
| 81 | + public function query(Vector $vector, array $options = []): iterable |
| 82 | + { |
| 83 | + $documents = $this->cache->get($this->cacheKey, static fn (): array => []); |
| 84 | + |
| 85 | + $vectorDocuments = array_map(static fn (array $document): VectorDocument => new VectorDocument( |
| 86 | + id: Uuid::fromString($document['id']), |
| 87 | + vector: new Vector($document['vector']), |
| 88 | + metadata: new Metadata($document['metadata']), |
| 89 | + ), $documents); |
| 90 | + |
| 91 | + if (isset($options['filter'])) { |
| 92 | + $vectorDocuments = array_values(array_filter($vectorDocuments, $options['filter'])); |
| 93 | + } |
| 94 | + |
| 95 | + yield from $this->distanceCalculator->calculate($vectorDocuments, $vector, $options['maxItems'] ?? null); |
| 96 | + } |
| 97 | + |
| 98 | + public function drop(): void |
| 99 | + { |
| 100 | + $this->cache->clear(); |
| 101 | + } |
| 102 | +} |
0 commit comments