From 346d352077fa275e00e499d9d615c253b09b0ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Lach?= Date: Mon, 6 Oct 2025 16:40:14 +0200 Subject: [PATCH] [AI Bundle][Postgres] Add store config --- src/ai-bundle/config/options.php | 15 ++++++++ src/ai-bundle/src/AiBundle.php | 34 +++++++++++++++++++ .../DependencyInjection/AiBundleTest.php | 9 +++++ 3 files changed, 58 insertions(+) diff --git a/src/ai-bundle/config/options.php b/src/ai-bundle/config/options.php index 02fb316d7..730ee6f87 100644 --- a/src/ai-bundle/config/options.php +++ b/src/ai-bundle/config/options.php @@ -668,6 +668,21 @@ ->end() ->end() ->end() + ->arrayNode('postgres') + ->useAttributeAsKey('name') + ->arrayPrototype() + ->children() + ->stringNode('dsn')->cannotBeEmpty()->end() + ->stringNode('username')->end() + ->stringNode('password')->end() + ->stringNode('table_name')->isRequired()->end() + ->stringNode('vector_field')->end() + ->enumNode('distance') + ->values(['cosine', 'inner_product', 'l1', 'l2']) + ->end() + ->end() + ->end() + ->end() ->end() ->end() ->arrayNode('vectorizer') diff --git a/src/ai-bundle/src/AiBundle.php b/src/ai-bundle/src/AiBundle.php index 7ca6a6637..3a92e5d6b 100644 --- a/src/ai-bundle/src/AiBundle.php +++ b/src/ai-bundle/src/AiBundle.php @@ -68,6 +68,7 @@ use Symfony\AI\Store\Bridge\MongoDb\Store as MongoDbStore; use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore; use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore; +use Symfony\AI\Store\Bridge\Postgres\Store; use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore; use Symfony\AI\Store\Bridge\Redis\Store as RedisStore; use Symfony\AI\Store\Bridge\SurrealDb\Store as SurrealDbStore; @@ -1189,6 +1190,39 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde $container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $type.'_'.$name); } } + + if ('postgres' === $type) { + foreach ($stores as $name => $store) { + $pdo = new Definition(\PDO::class); + $pdo->setArguments([ + $store['dsn'], + $store['username'] ?? null, + $store['password'] ?? null], + ); + + $arguments = [ + $pdo, + $store['table_name'], + ]; + + if (\array_key_exists('vector_field', $store)) { + $arguments[2] = $store['vector_field']; + } + + if (\array_key_exists('distance', $store)) { + $arguments[3] = $store['distance']; + } + + $definition = new Definition(Store::class); + $definition + ->addTag('ai.store') + ->setArguments($arguments); + + $container->setDefinition('ai.store.'.$type.'.'.$name, $definition); + $container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name); + $container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $type.'_'.$name); + } + } } /** diff --git a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php index 927b6038a..1660d1125 100644 --- a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php +++ b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php @@ -2894,6 +2894,15 @@ private function getFullConfig(): array 'collection' => 'my_weaviate_collection', ], ], + 'postgres' => [ + 'my_postgres_store' => [ + 'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=postgresql_db', + 'username' => 'postgres', + 'password' => 'pass', + 'table_name' => 'my_table', + 'vector_field' => 'my_embedding', + ], + ], ], 'vectorizer' => [ 'test_vectorizer' => [