Skip to content

Commit a5a34a8

Browse files
committed
bug #1082 [AI Bundle] Fix unresolved Distance enum dependencies in configuration (OskarStark)
This PR was merged into the main branch. Discussion ---------- [AI Bundle] Fix unresolved Distance enum dependencies in configuration | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fixes #1075 | License | MIT The options.php file was importing Distance enums from optional store bridge packages (postgres-store and redis-store) unconditionally. This caused errors when these packages weren't installed since PHP tries to resolve imports at parse time. The fix uses string values for the distance configuration instead of enum references, and converts them to enums in AiBundle.php only after verifying the package is installed via willBeAvailable(). Commits ------- ca96627 [AI Bundle] Fix unresolved Distance enum dependencies in configuration
2 parents d8a3938 + ca96627 commit a5a34a8

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/ai-bundle/config/options.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
use Symfony\AI\Platform\Capability;
1919
use Symfony\AI\Platform\Model;
2020
use Symfony\AI\Platform\PlatformInterface;
21-
use Symfony\AI\Store\Bridge\Postgres\Distance as PostgresDistance;
22-
use Symfony\AI\Store\Bridge\Redis\Distance;
2321
use Symfony\AI\Store\Document\VectorizerInterface;
2422
use Symfony\AI\Store\StoreInterface;
2523
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -794,8 +792,8 @@
794792
->end()
795793
->enumNode('distance')
796794
->info('Distance metric to use for vector similarity search')
797-
->enumFqcn(PostgresDistance::class)
798-
->defaultValue(PostgresDistance::L2)
795+
->values(['cosine', 'inner_product', 'l1', 'l2'])
796+
->defaultValue('l2')
799797
->end()
800798
->stringNode('dbal_connection')->cannotBeEmpty()->end()
801799
->end()
@@ -844,8 +842,8 @@
844842
->end()
845843
->enumNode('distance')
846844
->info('Distance metric to use for vector similarity search')
847-
->values(Distance::cases())
848-
->defaultValue(Distance::Cosine)
845+
->values(['COSINE', 'L2', 'IP'])
846+
->defaultValue('COSINE')
849847
->end()
850848
->end()
851849
->validate()

src/ai-bundle/src/AiBundle.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@
9292
use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore;
9393
use Symfony\AI\Store\Bridge\OpenSearch\Store as OpenSearchStore;
9494
use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore;
95+
use Symfony\AI\Store\Bridge\Postgres\Distance as PostgresDistance;
9596
use Symfony\AI\Store\Bridge\Postgres\Store as PostgresStore;
9697
use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore;
98+
use Symfony\AI\Store\Bridge\Redis\Distance as RedisDistance;
9799
use Symfony\AI\Store\Bridge\Redis\Store as RedisStore;
98100
use Symfony\AI\Store\Bridge\Supabase\Store as SupabaseStore;
99101
use Symfony\AI\Store\Bridge\SurrealDb\Store as SurrealDbStore;
@@ -1437,9 +1439,7 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
14371439
];
14381440
}
14391441

1440-
if (\array_key_exists('distance', $store)) {
1441-
$arguments[3] = $store['distance'];
1442-
}
1442+
$arguments[3] = PostgresDistance::from($store['distance']);
14431443

14441444
$definition
14451445
->setLazy(true)
@@ -1507,7 +1507,7 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
15071507
$redisClient,
15081508
$store['index_name'] ?? $name,
15091509
$store['key_prefix'],
1510-
$store['distance'],
1510+
RedisDistance::from($store['distance']),
15111511
])
15121512
->addTag('proxy', ['interface' => StoreInterface::class])
15131513
->addTag('proxy', ['interface' => ManagedStoreInterface::class])

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore;
4646
use Symfony\AI\Store\Bridge\OpenSearch\Store as OpenSearchStore;
4747
use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore;
48-
use Symfony\AI\Store\Bridge\Postgres\Distance;
48+
use Symfony\AI\Store\Bridge\Postgres\Distance as PostgresDistance;
4949
use Symfony\AI\Store\Bridge\Postgres\Store as PostgresStore;
5050
use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore;
5151
use Symfony\AI\Store\Bridge\Redis\Distance as RedisDistance;
@@ -2588,7 +2588,7 @@ public function testPostgresStoreWithDifferentConnectionCanBeConfigured()
25882588
$this->assertSame('my_connection', (string) $definition->getArgument(0));
25892589
$this->assertSame('db', $definition->getArgument(1));
25902590
$this->assertSame('foo', $definition->getArgument(2));
2591-
$this->assertSame(Distance::L2, $definition->getArgument(3));
2591+
$this->assertSame(PostgresDistance::L2, $definition->getArgument(3));
25922592

25932593
$this->assertTrue($definition->hasTag('proxy'));
25942594
$this->assertSame([
@@ -2609,7 +2609,7 @@ public function testPostgresStoreWithDifferentConnectionCanBeConfigured()
26092609
'db' => [
26102610
'dbal_connection' => 'my_connection',
26112611
'vector_field' => 'foo',
2612-
'distance' => Distance::L1->value,
2612+
'distance' => PostgresDistance::L1->value,
26132613
],
26142614
],
26152615
],
@@ -2625,7 +2625,7 @@ public function testPostgresStoreWithDifferentConnectionCanBeConfigured()
26252625
$this->assertSame('my_connection', (string) $definition->getArgument(0));
26262626
$this->assertSame('db', $definition->getArgument(1));
26272627
$this->assertSame('foo', $definition->getArgument(2));
2628-
$this->assertSame(Distance::L1, $definition->getArgument(3));
2628+
$this->assertSame(PostgresDistance::L1, $definition->getArgument(3));
26292629

26302630
$this->assertTrue($definition->hasTag('proxy'));
26312631
$this->assertSame([
@@ -2647,7 +2647,7 @@ public function testPostgresStoreWithDifferentConnectionCanBeConfigured()
26472647
'dbal_connection' => 'my_connection',
26482648
'table_name' => 'foo',
26492649
'vector_field' => 'foo',
2650-
'distance' => Distance::L1->value,
2650+
'distance' => PostgresDistance::L1->value,
26512651
],
26522652
],
26532653
],
@@ -2663,7 +2663,7 @@ public function testPostgresStoreWithDifferentConnectionCanBeConfigured()
26632663
$this->assertSame('my_connection', (string) $definition->getArgument(0));
26642664
$this->assertSame('foo', $definition->getArgument(1));
26652665
$this->assertSame('foo', $definition->getArgument(2));
2666-
$this->assertSame(Distance::L1, $definition->getArgument(3));
2666+
$this->assertSame(PostgresDistance::L1, $definition->getArgument(3));
26672667

26682668
$this->assertTrue($definition->hasTag('proxy'));
26692669
$this->assertSame([
@@ -2997,7 +2997,7 @@ public function testRedisStoreWithCustomDistanceCanBeConfigured()
29972997
'my_redis_store' => [
29982998
'client' => 'foo',
29992999
'index_name' => 'my_vector_index',
3000-
'distance' => RedisDistance::L2,
3000+
'distance' => RedisDistance::L2->value,
30013001
],
30023002
],
30033003
],
@@ -7217,7 +7217,7 @@ private function getFullConfig(): array
72177217
'my_redis_store_with_custom_distance' => [
72187218
'client' => 'foo',
72197219
'index_name' => 'my_vector_index',
7220-
'distance' => RedisDistance::L2,
7220+
'distance' => RedisDistance::L2->value,
72217221
],
72227222
],
72237223
'supabase' => [

0 commit comments

Comments
 (0)