From fdd3fa6330ab7d63239602d46f03455afd6b7582 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 5 Sep 2025 11:31:57 +0200 Subject: [PATCH] [Store] Add IndexerInterface for better abstraction and testability - Add IndexerInterface with index method for TextDocument processing - Update Indexer class to implement IndexerInterface - Update AiBundle to use IndexerInterface for dependency injection aliasing - Update demo Embedder to use IndexerInterface instead of concrete class --- demo/src/Blog/Embedder.php | 4 ++-- src/ai-bundle/src/AiBundle.php | 3 ++- src/store/src/Indexer.php | 2 +- src/store/src/IndexerInterface.php | 28 ++++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/store/src/IndexerInterface.php diff --git a/demo/src/Blog/Embedder.php b/demo/src/Blog/Embedder.php index 3491f564e..ae3a92d03 100644 --- a/demo/src/Blog/Embedder.php +++ b/demo/src/Blog/Embedder.php @@ -13,13 +13,13 @@ use Symfony\AI\Store\Document\Metadata; use Symfony\AI\Store\Document\TextDocument; -use Symfony\AI\Store\Indexer; +use Symfony\AI\Store\IndexerInterface; final readonly class Embedder { public function __construct( private FeedLoader $loader, - private Indexer $indexer, + private IndexerInterface $indexer, ) { } diff --git a/src/ai-bundle/src/AiBundle.php b/src/ai-bundle/src/AiBundle.php index e85ad6165..a7b76a15e 100644 --- a/src/ai-bundle/src/AiBundle.php +++ b/src/ai-bundle/src/AiBundle.php @@ -67,6 +67,7 @@ use Symfony\AI\Store\Bridge\Weaviate\Store as WeaviateStore; use Symfony\AI\Store\Document\Vectorizer; use Symfony\AI\Store\Indexer; +use Symfony\AI\Store\IndexerInterface; use Symfony\AI\Store\StoreInterface; use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; use Symfony\Component\DependencyInjection\Attribute\Target; @@ -144,7 +145,7 @@ public function loadExtension(array $config, ContainerConfigurator $container, C $this->processIndexerConfig($indexerName, $indexer, $builder); } if (1 === \count($config['indexer']) && isset($indexerName)) { - $builder->setAlias(Indexer::class, 'ai.indexer.'.$indexerName); + $builder->setAlias(IndexerInterface::class, 'ai.indexer.'.$indexerName); } $builder->registerAttributeForAutoconfiguration(AsTool::class, static function (ChildDefinition $definition, AsTool $attribute): void { diff --git a/src/store/src/Indexer.php b/src/store/src/Indexer.php index 385f10a5b..5965aaaf8 100644 --- a/src/store/src/Indexer.php +++ b/src/store/src/Indexer.php @@ -21,7 +21,7 @@ * * @author Christopher Hertel */ -final readonly class Indexer +final readonly class Indexer implements IndexerInterface { public function __construct( private Vectorizer $vectorizer, diff --git a/src/store/src/IndexerInterface.php b/src/store/src/IndexerInterface.php new file mode 100644 index 000000000..18be074ac --- /dev/null +++ b/src/store/src/IndexerInterface.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\AI\Store; + +use Symfony\AI\Store\Document\TextDocument; + +/** + * Converts a collection of TextDocuments into VectorDocuments and pushes them to a store implementation. + * + * @author Oskar Stark + */ +interface IndexerInterface +{ + /** + * @param TextDocument|iterable $documents + * @param int $chunkSize number of documents to vectorize and store in one batch + */ + public function index(TextDocument|iterable $documents, int $chunkSize = 50): void; +}