Skip to content

Commit 498745c

Browse files
committed
feature #641 [AI Bundle] Improvement on alias for store (Guikingone)
This PR was merged into the main branch. Discussion ---------- [AI Bundle] Improvement on alias for store | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fix none | License | MIT Hi 👋🏻 A small improvement for the store alias, this approach allows to use the same name for various store (like `foo` for a memory one, a qdrant one, etc) without injection issues. Commits ------- 8865f21 refactor(aibundle): improvement on alias for store
2 parents d5aa416 + 8865f21 commit 498745c

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
705705
->setArguments($arguments);
706706

707707
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
708-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
708+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
709+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
709710
}
710711
}
711712

@@ -737,7 +738,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
737738
->setArguments($arguments);
738739

739740
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
740-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
741+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
742+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
741743
}
742744
}
743745

@@ -752,7 +754,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
752754
->addTag('ai.store');
753755

754756
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
755-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
757+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
758+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
756759
}
757760
}
758761

@@ -779,7 +782,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
779782
;
780783

781784
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
782-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
785+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
786+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
783787
}
784788
}
785789

@@ -810,6 +814,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
810814
->setArguments($arguments);
811815

812816
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
817+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
818+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
813819
}
814820
}
815821

@@ -840,7 +846,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
840846
->setArguments($arguments);
841847

842848
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
843-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
849+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
850+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
844851
}
845852
}
846853

@@ -865,7 +872,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
865872
->setArguments($arguments);
866873

867874
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
868-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
875+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
876+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
869877
}
870878
}
871879

@@ -897,7 +905,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
897905
->setArguments($arguments);
898906

899907
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
900-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
908+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
909+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
901910
}
902911
}
903912

@@ -924,7 +933,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
924933
->setArguments($arguments);
925934

926935
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
927-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
936+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
937+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
928938
}
929939
}
930940

@@ -962,7 +972,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
962972
->setArguments($arguments);
963973

964974
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
965-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
975+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
976+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
966977
}
967978
}
968979

@@ -987,7 +998,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
987998
->setArguments($arguments);
988999

9891000
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
990-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1001+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1002+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
9911003
}
9921004
}
9931005

@@ -1014,7 +1026,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10141026
->setArguments($arguments);
10151027

10161028
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1017-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1029+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1030+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10181031
}
10191032
}
10201033

@@ -1055,7 +1068,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10551068
->setArguments($arguments);
10561069

10571070
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1058-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1071+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1072+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10591073
}
10601074
}
10611075

@@ -1082,7 +1096,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10821096
->setArguments($arguments);
10831097

10841098
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1085-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1099+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1100+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10861101
}
10871102
}
10881103

@@ -1101,6 +1116,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
11011116
->setArguments($arguments);
11021117

11031118
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1119+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1120+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
11041121
}
11051122
}
11061123
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
3030
use Symfony\AI\Store\Document\Transformer\TextTrimTransformer;
3131
use Symfony\AI\Store\Document\Vectorizer;
32+
use Symfony\AI\Store\StoreInterface;
3233
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
3334
use Symfony\Component\DependencyInjection\ContainerBuilder;
3435
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -100,6 +101,37 @@ public function testInjectionAgentAliasIsRegistered()
100101
$this->assertTrue($container->hasAlias(AgentInterface::class.' $myAgentAgent'));
101102
}
102103

104+
public function testInjectionStoreAliasIsRegistered()
105+
{
106+
$container = $this->buildContainer([
107+
'ai' => [
108+
'store' => [
109+
'memory' => [
110+
'main' => [
111+
'strategy' => 'cosine',
112+
],
113+
'secondary_with_custom_strategy' => [
114+
'strategy' => 'manhattan',
115+
],
116+
],
117+
'weaviate' => [
118+
'main' => [
119+
'endpoint' => 'http://localhost:8080',
120+
'api_key' => 'bar',
121+
'collection' => 'my_weaviate_collection',
122+
],
123+
],
124+
],
125+
],
126+
]);
127+
128+
$this->assertTrue($container->hasAlias(StoreInterface::class.' $main'));
129+
$this->assertTrue($container->hasAlias('.'.StoreInterface::class.' $secondary_with_custom_strategy'));
130+
$this->assertTrue($container->hasAlias(StoreInterface::class.' $secondaryWithCustomStrategy'));
131+
$this->assertTrue($container->hasAlias('.'.StoreInterface::class.' $weaviate_main'));
132+
$this->assertTrue($container->hasAlias(StoreInterface::class.' $weaviateMain'));
133+
}
134+
103135
public function testAgentHasTag()
104136
{
105137
$container = $this->buildContainer([

0 commit comments

Comments
 (0)