Skip to content

Commit 6077003

Browse files
committed
refactor(aibundle): improvements on configuration / tags
1 parent 6ed950f commit 6077003

File tree

3 files changed

+92
-51
lines changed

3 files changed

+92
-51
lines changed

src/ai-bundle/config/options.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@
442442
->treatNullLike(['enabled' => true])
443443
->beforeNormalization()
444444
->ifArray()
445-
->then(function (array $v) {
445+
->then(function (array $v): array {
446446
return [
447447
'enabled' => $v['enabled'] ?? true,
448448
'services' => $v['services'] ?? $v,
@@ -564,7 +564,7 @@
564564
->stringNode('table')->isRequired()->cannotBeEmpty()->end()
565565
->end()
566566
->validate()
567-
->ifTrue(static fn ($v) => !isset($v['dsn']) && !isset($v['http_client']))
567+
->ifTrue(static fn ($v): bool => !isset($v['dsn']) && !isset($v['http_client']))
568568
->thenInvalid('Either "dsn" or "http_client" must be configured.')
569569
->end()
570570
->end()
@@ -819,11 +819,11 @@
819819
->stringNode('dbal_connection')->cannotBeEmpty()->end()
820820
->end()
821821
->validate()
822-
->ifTrue(static fn ($v) => !isset($v['dsn']) && !isset($v['dbal_connection']))
822+
->ifTrue(static fn (array $v): bool => !isset($v['dsn']) && !isset($v['dbal_connection']))
823823
->thenInvalid('Either "dsn" or "dbal_connection" must be configured.')
824824
->end()
825825
->validate()
826-
->ifTrue(static fn ($v) => isset($v['dsn'], $v['dbal_connection']))
826+
->ifTrue(static fn (array $v): bool => isset($v['dsn'], $v['dbal_connection']))
827827
->thenInvalid('Either "dsn" or "dbal_connection" can be configured, but not both.')
828828
->end()
829829
->end()

src/ai-bundle/src/AiBundle.php

Lines changed: 84 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ private function processAgentConfig(string $name, array $config, ContainerBuilde
929929
* @param array<string, mixed> $stores
930930
* @param array<string, mixed> $setupStoresOptions
931931
*/
932-
private function processStoreConfig(string $type, array $stores, ContainerBuilder $container, &$setupStoresOptions): void
932+
private function processStoreConfig(string $type, array $stores, ContainerBuilder $container, array &$setupStoresOptions): void
933933
{
934934
if ('azure_search' === $type) {
935935
foreach ($stores as $name => $store) {
@@ -947,8 +947,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
947947

948948
$definition = new Definition(AzureSearchStore::class);
949949
$definition
950-
->addTag('ai.store')
951-
->setArguments($arguments);
950+
->setLazy(true)
951+
->setArguments($arguments)
952+
->addTag('proxy', ['interface' => StoreInterface::class])
953+
->addTag('ai.store');
952954

953955
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
954956
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -961,6 +963,7 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
961963
$arguments = [
962964
new Reference($store['service']),
963965
new Definition(DistanceCalculator::class),
966+
$store['cache_key'] ?? $name,
964967
];
965968

966969
if (\array_key_exists('strategy', $store) && null !== $store['strategy']) {
@@ -974,13 +977,11 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
974977
$arguments[1] = new Reference('ai.store.distance_calculator.'.$name);
975978
}
976979

977-
$arguments[2] = \array_key_exists('cache_key', $store) && null !== $store['cache_key']
978-
? $store['cache_key']
979-
: $name;
980-
981980
$definition = new Definition(CacheStore::class);
982981
$definition
982+
->setLazy(true)
983983
->setArguments($arguments)
984+
->addTag('proxy', ['interface' => StoreInterface::class])
984985
->addTag('ai.store');
985986

986987
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
@@ -993,10 +994,12 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
993994
foreach ($stores as $name => $store) {
994995
$definition = new Definition(ChromaDbStore::class);
995996
$definition
997+
->setLazy(true)
996998
->setArguments([
997999
new Reference($store['client']),
9981000
$store['collection'],
9991001
])
1002+
->addTag('proxy', ['interface' => StoreInterface::class])
10001003
->addTag('ai.store');
10011004

10021005
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
@@ -1019,11 +1022,13 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10191022

10201023
$definition = new Definition(ClickHouseStore::class);
10211024
$definition
1025+
->setLazy(true)
10221026
->setArguments([
10231027
$httpClient,
10241028
$store['database'],
10251029
$store['table'],
10261030
])
1031+
->addTag('proxy', ['interface' => StoreInterface::class])
10271032
->addTag('ai.store')
10281033
;
10291034

@@ -1056,8 +1061,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10561061

10571062
$definition = new Definition(CloudflareStore::class);
10581063
$definition
1059-
->addTag('ai.store')
1060-
->setArguments($arguments);
1064+
->setLazy(true)
1065+
->setArguments($arguments)
1066+
->addTag('proxy', ['interface' => StoreInterface::class])
1067+
->addTag('ai.store');
10611068

10621069
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
10631070
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1095,7 +1102,9 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10951102

10961103
$definition = new Definition(ManticoreStore::class);
10971104
$definition
1105+
->setLazy(true)
10981106
->addTag('ai.store')
1107+
->addTag('proxy', ['interface' => StoreInterface::class])
10991108
->setArguments($arguments);
11001109

11011110
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
@@ -1116,8 +1125,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
11161125
$definition = new Definition(MariaDbStore::class);
11171126
$definition->setFactory([MariaDbStore::class, 'fromDbal']);
11181127
$definition
1119-
->addTag('ai.store')
1120-
->setArguments($arguments);
1128+
->setLazy(true)
1129+
->setArguments($arguments)
1130+
->addTag('proxy', ['interface' => StoreInterface::class])
1131+
->addTag('ai.store');
11211132

11221133
$serviceId = 'ai.store.'.$type.'.'.$name;
11231134
$container->setDefinition($serviceId, $definition);
@@ -1155,8 +1166,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
11551166

11561167
$definition = new Definition(MeilisearchStore::class);
11571168
$definition
1158-
->addTag('ai.store')
1159-
->setArguments($arguments);
1169+
->setLazy(true)
1170+
->setArguments($arguments)
1171+
->addTag('proxy', ['interface' => StoreInterface::class])
1172+
->addTag('ai.store');
11601173

11611174
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
11621175
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1166,7 +1179,9 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
11661179

11671180
if ('memory' === $type) {
11681181
foreach ($stores as $name => $store) {
1169-
$arguments = [];
1182+
$arguments = [
1183+
new Definition(DistanceCalculator::class),
1184+
];
11701185

11711186
if (\array_key_exists('strategy', $store) && null !== $store['strategy']) {
11721187
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
@@ -1181,8 +1196,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
11811196

11821197
$definition = new Definition(InMemoryStore::class);
11831198
$definition
1184-
->addTag('ai.store')
1185-
->setArguments($arguments);
1199+
->setLazy(true)
1200+
->setArguments($arguments)
1201+
->addTag('proxy', ['interface' => StoreInterface::class])
1202+
->addTag('ai.store');
11861203

11871204
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
11881205
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1214,8 +1231,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
12141231

12151232
$definition = new Definition(MilvusStore::class);
12161233
$definition
1217-
->addTag('ai.store')
1218-
->setArguments($arguments);
1234+
->setLazy(true)
1235+
->setArguments($arguments)
1236+
->addTag('proxy', ['interface' => StoreInterface::class])
1237+
->addTag('ai.store');
12191238

12201239
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
12211240
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1242,8 +1261,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
12421261

12431262
$definition = new Definition(MongoDbStore::class);
12441263
$definition
1245-
->addTag('ai.store')
1246-
->setArguments($arguments);
1264+
->setLazy(true)
1265+
->setArguments($arguments)
1266+
->addTag('proxy', ['interface' => StoreInterface::class])
1267+
->addTag('ai.store');
12471268

12481269
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
12491270
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1281,8 +1302,9 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
12811302

12821303
$definition = new Definition(Neo4jStore::class);
12831304
$definition
1284-
->addTag('ai.store')
1285-
->setArguments($arguments);
1305+
->setArguments($arguments)
1306+
->addTag('proxy', ['interface' => StoreInterface::class])
1307+
->addTag('ai.store');
12861308

12871309
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
12881310
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1307,8 +1329,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
13071329

13081330
$definition = new Definition(PineconeStore::class);
13091331
$definition
1310-
->addTag('ai.store')
1311-
->setArguments($arguments);
1332+
->setLazy(true)
1333+
->setArguments($arguments)
1334+
->addTag('proxy', ['interface' => StoreInterface::class])
1335+
->addTag('ai.store');
13121336

13131337
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
13141338
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1339,8 +1363,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
13391363

13401364
$definition = new Definition(QdrantStore::class);
13411365
$definition
1342-
->addTag('ai.store')
1343-
->setArguments($arguments);
1366+
->setLazy(true)
1367+
->setArguments($arguments)
1368+
->addTag('proxy', ['interface' => StoreInterface::class])
1369+
->addTag('ai.store');
13441370

13451371
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
13461372
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1359,14 +1385,15 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
13591385

13601386
$definition = new Definition(RedisStore::class);
13611387
$definition
1362-
->addTag('ai.store')
1388+
->setLazy(true)
13631389
->setArguments([
13641390
$redisClient,
13651391
$store['index_name'],
13661392
$store['key_prefix'],
13671393
$store['distance'],
13681394
])
1369-
;
1395+
->addTag('proxy', ['interface' => StoreInterface::class])
1396+
->addTag('ai.store');
13701397

13711398
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
13721399
}
@@ -1405,8 +1432,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
14051432

14061433
$definition = new Definition(SurrealDbStore::class);
14071434
$definition
1408-
->addTag('ai.store')
1409-
->setArguments($arguments);
1435+
->setLazy(true)
1436+
->setArguments($arguments)
1437+
->addTag('proxy', ['interface' => StoreInterface::class])
1438+
->addTag('ai.store');
14101439

14111440
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
14121441
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1433,8 +1462,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
14331462

14341463
$definition = new Definition(TypesenseStore::class);
14351464
$definition
1436-
->addTag('ai.store')
1437-
->setArguments($arguments);
1465+
->setLazy(true)
1466+
->setArguments($arguments)
1467+
->addTag('proxy', ['interface' => StoreInterface::class])
1468+
->addTag('ai.store');
14381469

14391470
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
14401471
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1444,17 +1475,17 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
14441475

14451476
if ('weaviate' === $type) {
14461477
foreach ($stores as $name => $store) {
1447-
$arguments = [
1448-
new Reference('http_client'),
1449-
$store['endpoint'],
1450-
$store['api_key'],
1451-
$store['collection'],
1452-
];
1453-
14541478
$definition = new Definition(WeaviateStore::class);
14551479
$definition
1456-
->addTag('ai.store')
1457-
->setArguments($arguments);
1480+
->setLazy(true)
1481+
->setArguments([
1482+
new Reference('http_client'),
1483+
$store['endpoint'],
1484+
$store['api_key'],
1485+
$store['collection'],
1486+
])
1487+
->addTag('proxy', ['interface' => StoreInterface::class])
1488+
->addTag('ai.store');
14581489

14591490
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
14601491
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1495,8 +1526,10 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
14951526
}
14961527

14971528
$definition
1498-
->addTag('ai.store')
1499-
->setArguments($arguments);
1529+
->setLazy(true)
1530+
->setArguments($arguments)
1531+
->addTag('proxy', ['interface' => StoreInterface::class])
1532+
->addTag('ai.store');
15001533

15011534
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
15021535
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
@@ -1530,11 +1563,14 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
15301563

15311564
$definition = new Definition(SupabaseStore::class);
15321565
$definition
1533-
->addTag('ai.store')
1534-
->setArguments($arguments);
1566+
->setLazy(true)
1567+
->setArguments($arguments)
1568+
->addTag('proxy', ['interface' => StoreInterface::class])
1569+
->addTag('ai.store');
15351570

15361571
$container->setDefinition('ai.store.supabase.'.$name, $definition);
1537-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1572+
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
1573+
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $type.'_'.$name);
15381574
}
15391575
}
15401576
}
@@ -1750,10 +1786,12 @@ private function processChatConfig(string $name, array $configuration, Container
17501786
{
17511787
$definition = new Definition(Chat::class);
17521788
$definition
1789+
->setLazy(true)
17531790
->setArguments([
17541791
new Reference($configuration['agent']),
17551792
new Reference($configuration['message_store']),
17561793
])
1794+
->addTag('proxy', ['interface' => ChatInterface::class])
17571795
->addTag('ai.chat');
17581796

17591797
$container->setDefinition('ai.chat.'.$name, $definition);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\AI\Platform\Bridge\Ollama\OllamaApiCatalog;
2828
use Symfony\AI\Platform\Capability;
2929
use Symfony\AI\Platform\Model;
30+
use Symfony\AI\Store\Bridge\Local\DistanceCalculator;
3031
use Symfony\AI\Store\Document\Filter\TextContainsFilter;
3132
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
3233
use Symfony\AI\Store\Document\Transformer\TextTrimTransformer;
@@ -483,7 +484,9 @@ public function testInMemoryStoreWithoutCustomStrategyCanBeConfigured()
483484
$this->assertTrue($container->hasDefinition('ai.store.memory.my_memory_store_with_custom_strategy'));
484485

485486
$definition = $container->getDefinition('ai.store.memory.my_memory_store_with_custom_strategy');
486-
$this->assertCount(0, $definition->getArguments());
487+
$this->assertCount(1, $definition->getArguments());
488+
$this->assertInstanceOf(Definition::class, $definition->getArgument(0));
489+
$this->assertSame(DistanceCalculator::class, $definition->getArgument(0)->getClass());
487490
}
488491

489492
public function testInMemoryStoreWithCustomStrategyCanBeConfigured()

0 commit comments

Comments
 (0)