Skip to content

Commit aa1ad5d

Browse files
committed
feature #733 [AI Bundle][Postgres] Add store config (Jarosław Lach)
This PR was squashed before being merged into the main branch. Discussion ---------- [AI Bundle][Postgres] Add store config | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | License | MIT Added postgresql configuration for ai-bundle with. Configuration requires DSN for PDO instantiation with optional username and password, table name, vector field name or/and distance function Commits ------- 346d352 [AI Bundle][Postgres] Add store config
2 parents 8842d68 + 346d352 commit aa1ad5d

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/ai-bundle/config/options.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,21 @@
668668
->end()
669669
->end()
670670
->end()
671+
->arrayNode('postgres')
672+
->useAttributeAsKey('name')
673+
->arrayPrototype()
674+
->children()
675+
->stringNode('dsn')->cannotBeEmpty()->end()
676+
->stringNode('username')->end()
677+
->stringNode('password')->end()
678+
->stringNode('table_name')->isRequired()->end()
679+
->stringNode('vector_field')->end()
680+
->enumNode('distance')
681+
->values(['cosine', 'inner_product', 'l1', 'l2'])
682+
->end()
683+
->end()
684+
->end()
685+
->end()
671686
->end()
672687
->end()
673688
->arrayNode('vectorizer')

src/ai-bundle/src/AiBundle.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
use Symfony\AI\Store\Bridge\MongoDb\Store as MongoDbStore;
6969
use Symfony\AI\Store\Bridge\Neo4j\Store as Neo4jStore;
7070
use Symfony\AI\Store\Bridge\Pinecone\Store as PineconeStore;
71+
use Symfony\AI\Store\Bridge\Postgres\Store;
7172
use Symfony\AI\Store\Bridge\Qdrant\Store as QdrantStore;
7273
use Symfony\AI\Store\Bridge\Redis\Store as RedisStore;
7374
use Symfony\AI\Store\Bridge\SurrealDb\Store as SurrealDbStore;
@@ -1189,6 +1190,39 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
11891190
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $type.'_'.$name);
11901191
}
11911192
}
1193+
1194+
if ('postgres' === $type) {
1195+
foreach ($stores as $name => $store) {
1196+
$pdo = new Definition(\PDO::class);
1197+
$pdo->setArguments([
1198+
$store['dsn'],
1199+
$store['username'] ?? null,
1200+
$store['password'] ?? null],
1201+
);
1202+
1203+
$arguments = [
1204+
$pdo,
1205+
$store['table_name'],
1206+
];
1207+
1208+
if (\array_key_exists('vector_field', $store)) {
1209+
$arguments[2] = $store['vector_field'];
1210+
}
1211+
1212+
if (\array_key_exists('distance', $store)) {
1213+
$arguments[3] = $store['distance'];
1214+
}
1215+
1216+
$definition = new Definition(Store::class);
1217+
$definition
1218+
->addTag('ai.store')
1219+
->setArguments($arguments);
1220+
1221+
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1222+
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
1223+
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $type.'_'.$name);
1224+
}
1225+
}
11921226
}
11931227

11941228
/**

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,15 @@ private function getFullConfig(): array
28942894
'collection' => 'my_weaviate_collection',
28952895
],
28962896
],
2897+
'postgres' => [
2898+
'my_postgres_store' => [
2899+
'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=postgresql_db',
2900+
'username' => 'postgres',
2901+
'password' => 'pass',
2902+
'table_name' => 'my_table',
2903+
'vector_field' => 'my_embedding',
2904+
],
2905+
],
28972906
],
28982907
'vectorizer' => [
28992908
'test_vectorizer' => [

0 commit comments

Comments
 (0)