Skip to content

Commit e0a4904

Browse files
committed
feature #874 [AI Bundle] Add more alias for argument: Indexer and Vectorizer (lyrixx)
This PR was merged into the main branch. Discussion ---------- [AI Bundle] Add more alias for argument: Indexer and Vectorizer | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | | License | MIT Commits ------- 99e8c2c [AiBundle] Add more alias for argument: Indexer and Vectorizer
2 parents a9420af + 99e8c2c commit e0a4904

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
use Symfony\AI\Store\Bridge\Typesense\Store as TypesenseStore;
9292
use Symfony\AI\Store\Bridge\Weaviate\Store as WeaviateStore;
9393
use Symfony\AI\Store\Document\Vectorizer;
94+
use Symfony\AI\Store\Document\VectorizerInterface;
9495
use Symfony\AI\Store\Indexer;
9596
use Symfony\AI\Store\IndexerInterface;
9697
use Symfony\AI\Store\StoreInterface;
@@ -1627,7 +1628,9 @@ private function processVectorizerConfig(string $name, array $config, ContainerB
16271628
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
16281629
]);
16291630
$vectorizerDefinition->addTag('ai.vectorizer', ['name' => $name]);
1630-
$container->setDefinition('ai.vectorizer.'.$name, $vectorizerDefinition);
1631+
$serviceId = 'ai.vectorizer.'.$name;
1632+
$container->setDefinition($serviceId, $vectorizerDefinition);
1633+
$container->registerAliasForArgument($serviceId, VectorizerInterface::class, (new Target((string) $name))->getParsedName());
16311634
}
16321635

16331636
/**
@@ -1656,7 +1659,9 @@ private function processIndexerConfig(int|string $name, array $config, Container
16561659
]);
16571660
$definition->addTag('ai.indexer', ['name' => $name]);
16581661

1659-
$container->setDefinition('ai.indexer.'.$name, $definition);
1662+
$serviceId = 'ai.indexer.'.$name;
1663+
$container->setDefinition($serviceId, $definition);
1664+
$container->registerAliasForArgument($serviceId, IndexerInterface::class, (new Target((string) $name))->getParsedName());
16601665
}
16611666

16621667
/**

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
2828
use Symfony\AI\Store\Document\Transformer\TextTrimTransformer;
2929
use Symfony\AI\Store\Document\Vectorizer;
30+
use Symfony\AI\Store\Document\VectorizerInterface;
31+
use Symfony\AI\Store\IndexerInterface;
3032
use Symfony\AI\Store\StoreInterface;
3133
use Symfony\Component\Clock\ClockInterface;
3234
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -1784,6 +1786,25 @@ public function testVectorizerWithLoggerInjection()
17841786
$this->assertSame(ContainerInterface::IGNORE_ON_INVALID_REFERENCE, $arguments[2]->getInvalidBehavior());
17851787
}
17861788

1789+
public function testInjectionVectorizerAliasIsRegistered()
1790+
{
1791+
$container = $this->buildContainer([
1792+
'ai' => [
1793+
'vectorizer' => [
1794+
'test' => [
1795+
'model' => 'text-embedding-3-small?dimensions=512',
1796+
],
1797+
'another' => [
1798+
'model' => 'text-embedding-3-small?dimensions=512',
1799+
],
1800+
],
1801+
],
1802+
]);
1803+
1804+
$this->assertTrue($container->hasAlias(VectorizerInterface::class.' $test'));
1805+
$this->assertTrue($container->hasAlias(VectorizerInterface::class.' $another'));
1806+
}
1807+
17871808
public function testIndexerWithConfiguredVectorizer()
17881809
{
17891810
$container = $this->buildContainer([
@@ -2262,6 +2283,36 @@ public function testIndexerWithSourceFiltersAndTransformers()
22622283
$this->assertSame('logger', (string) $arguments[6]);
22632284
}
22642285

2286+
public function testInjectionIndexerAliasIsRegistered()
2287+
{
2288+
$container = $this->buildContainer([
2289+
'ai' => [
2290+
'store' => [
2291+
'memory' => [
2292+
'my_store' => [],
2293+
],
2294+
],
2295+
'indexer' => [
2296+
'my_indexer' => [
2297+
'loader' => InMemoryLoader::class,
2298+
'transformers' => [],
2299+
'vectorizer' => 'my_vectorizer_service',
2300+
'store' => 'ai.store.memory.my_store',
2301+
],
2302+
'another' => [
2303+
'loader' => InMemoryLoader::class,
2304+
'transformers' => [],
2305+
'vectorizer' => 'my_vectorizer_service',
2306+
'store' => 'ai.store.memory.my_store',
2307+
],
2308+
],
2309+
],
2310+
]);
2311+
2312+
$this->assertTrue($container->hasAlias(IndexerInterface::class.' $myIndexer'));
2313+
$this->assertTrue($container->hasAlias(IndexerInterface::class.' $another'));
2314+
}
2315+
22652316
public function testValidMultiAgentConfiguration()
22662317
{
22672318
$container = $this->buildContainer([

0 commit comments

Comments
 (0)