|
| 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; |
| 13 | + |
| 14 | +use Symfony\AI\Store\Document\VectorDocument; |
| 15 | +use Symfony\AI\Store\Exception\UnsupportedQueryTypeException; |
| 16 | +use Symfony\AI\Store\Query\HybridQuery; |
| 17 | +use Symfony\AI\Store\Query\QueryInterface; |
| 18 | +use Symfony\AI\Store\Query\TextQuery; |
| 19 | +use Symfony\AI\Store\Query\VectorQuery; |
| 20 | + |
| 21 | +/** |
| 22 | + * Combines vector and text stores using Reciprocal Rank Fusion (RRF). |
| 23 | + * |
| 24 | + * Decomposes HybridQuery into VectorQuery and TextQuery, queries both |
| 25 | + * sub-stores independently, and merges results using RRF scoring. |
| 26 | + * |
| 27 | + * @author Johannes Wachter <[email protected]> |
| 28 | + */ |
| 29 | +final class CombinedStore implements StoreInterface |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly StoreInterface $vectorStore, |
| 33 | + private readonly StoreInterface $textStore, |
| 34 | + private readonly int $rrfK = 60, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + public function add(VectorDocument|array $documents): void |
| 39 | + { |
| 40 | + $this->vectorStore->add($documents); |
| 41 | + |
| 42 | + if ($this->textStore !== $this->vectorStore) { |
| 43 | + $this->textStore->add($documents); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function remove(string|array $ids, array $options = []): void |
| 48 | + { |
| 49 | + $this->vectorStore->remove($ids, $options); |
| 50 | + |
| 51 | + if ($this->textStore !== $this->vectorStore) { |
| 52 | + $this->textStore->remove($ids, $options); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function query(QueryInterface $query, array $options = []): iterable |
| 57 | + { |
| 58 | + if ($query instanceof HybridQuery) { |
| 59 | + return $this->hybridQuery($query, $options); |
| 60 | + } |
| 61 | + |
| 62 | + if ($query instanceof VectorQuery && $this->vectorStore->supports(VectorQuery::class)) { |
| 63 | + return $this->vectorStore->query($query, $options); |
| 64 | + } |
| 65 | + |
| 66 | + if ($query instanceof TextQuery && $this->textStore->supports(TextQuery::class)) { |
| 67 | + return $this->textStore->query($query, $options); |
| 68 | + } |
| 69 | + |
| 70 | + throw new UnsupportedQueryTypeException($query::class, $this); |
| 71 | + } |
| 72 | + |
| 73 | + public function supports(string $queryClass): bool |
| 74 | + { |
| 75 | + if (HybridQuery::class === $queryClass) { |
| 76 | + return $this->vectorStore->supports(VectorQuery::class) |
| 77 | + && $this->textStore->supports(TextQuery::class); |
| 78 | + } |
| 79 | + |
| 80 | + return $this->vectorStore->supports($queryClass) |
| 81 | + || $this->textStore->supports($queryClass); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param array<string, mixed> $options |
| 86 | + * |
| 87 | + * @return list<VectorDocument> |
| 88 | + */ |
| 89 | + private function hybridQuery(HybridQuery $query, array $options): array |
| 90 | + { |
| 91 | + $vectorResults = iterator_to_array( |
| 92 | + $this->vectorStore->query(new VectorQuery($query->getVector()), $options), |
| 93 | + ); |
| 94 | + |
| 95 | + $textResults = iterator_to_array( |
| 96 | + $this->textStore->query(new TextQuery($query->getText()), $options), |
| 97 | + ); |
| 98 | + |
| 99 | + return $this->reciprocalRankFusion($vectorResults, $textResults); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param list<VectorDocument> $list1 |
| 104 | + * @param list<VectorDocument> $list2 |
| 105 | + * |
| 106 | + * @return list<VectorDocument> |
| 107 | + */ |
| 108 | + private function reciprocalRankFusion(array $list1, array $list2): array |
| 109 | + { |
| 110 | + /** @var array<string, float> $scores */ |
| 111 | + $scores = []; |
| 112 | + |
| 113 | + /** @var array<string, VectorDocument> $documentsById */ |
| 114 | + $documentsById = []; |
| 115 | + |
| 116 | + foreach ($list1 as $rank => $document) { |
| 117 | + $id = (string) $document->getId(); |
| 118 | + $scores[$id] = ($scores[$id] ?? 0.0) + 1.0 / ($this->rrfK + $rank + 1); |
| 119 | + $documentsById[$id] = $document; |
| 120 | + } |
| 121 | + |
| 122 | + foreach ($list2 as $rank => $document) { |
| 123 | + $id = (string) $document->getId(); |
| 124 | + $scores[$id] = ($scores[$id] ?? 0.0) + 1.0 / ($this->rrfK + $rank + 1); |
| 125 | + $documentsById[$id] = $document; |
| 126 | + } |
| 127 | + |
| 128 | + arsort($scores); |
| 129 | + |
| 130 | + $result = []; |
| 131 | + foreach (array_keys($scores) as $id) { |
| 132 | + $result[] = $documentsById[$id]->withScore($scores[$id]); |
| 133 | + } |
| 134 | + |
| 135 | + return $result; |
| 136 | + } |
| 137 | +} |
0 commit comments