From 16c9a81f4c03d060973a3d4dc71ca53e5089db36 Mon Sep 17 00:00:00 2001 From: yangdx Date: Fri, 8 Aug 2025 02:55:49 +0800 Subject: [PATCH] feat: support config.ini for PostgreSQL vector index settings - Add support for reading vector_index_type, hnsw_m, hnsw_ef, and ivfflat_lists from config.ini - Maintain backward compatibility with environment variables - Update config.ini.example with new PostgreSQL vector index options - Follow existing configuration priority: env vars > config.ini > defaults --- config.ini.example | 6 +++++- lightrag/kg/postgres_impl.py | 31 +++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/config.ini.example b/config.ini.example index 26e6ad97..d3fff063 100644 --- a/config.ini.example +++ b/config.ini.example @@ -26,8 +26,12 @@ port = 5432 user = your_username password = your_password database = your_database -workspace = default # 可选,默认为default +# workspace = default max_connections = 12 +vector_index_type = HNSW # HNSW or IVFFLAT +hnsw_m = 16 +hnsw_ef = 64 +ivfflat_lists = 100 [memgraph] uri = bolt://localhost:7687 diff --git a/lightrag/kg/postgres_impl.py b/lightrag/kg/postgres_impl.py index af2a4686..c974f385 100644 --- a/lightrag/kg/postgres_impl.py +++ b/lightrag/kg/postgres_impl.py @@ -68,6 +68,9 @@ class PostgreSQLDB: # Vector configuration self.vector_index_type = config.get("vector_index_type") + self.hnsw_m = config.get("hnsw_m") + self.hnsw_ef = config.get("hnsw_ef") + self.ivfflat_lists = config.get("ivfflat_lists") if self.user is None or self.password is None or self.database is None: raise ValueError("Missing database user, password, or database") @@ -1155,7 +1158,7 @@ class PostgreSQLDB: create_vector_index_sql = f""" CREATE INDEX {vector_index_name} ON {k} USING hnsw (content_vector vector_cosine_ops) - WITH (m = {os.getenv("POSTGRES_HNSW_M", 16)}, ef_construction = {os.getenv("POSTGRES_HNSW_EF", 200)}) + WITH (m = {self.hnsw_m}, ef_construction = {self.hnsw_ef}) """ logger.info(f"Creating hnsw index {vector_index_name} on table {k}") await self.execute(create_vector_index_sql) @@ -1195,7 +1198,7 @@ class PostgreSQLDB: create_sql = f""" CREATE INDEX {index_name} ON {k} USING ivfflat (content_vector vector_cosine_ops) - WITH (lists = {os.getenv("POSTGRES_IVFFLAT_LISTS", 100)}) + WITH (lists = {self.ivfflat_lists}) """ logger.info(f"Creating ivfflat index {index_name} on table {k}") await self.execute(create_sql) @@ -1209,7 +1212,6 @@ class PostgreSQLDB: except Exception as e: logger.error(f"Failed to create ivfflat index on {k}: {e}") - async def query( self, sql: str, @@ -1350,7 +1352,28 @@ class ClientManager: "POSTGRES_SSL_CRL", config.get("postgres", "ssl_crl", fallback=None), ), - "vector_index_type": os.environ.get("POSTGRES_VECTOR_INDEX_TYPE", "HNSW"), + "vector_index_type": os.environ.get( + "POSTGRES_VECTOR_INDEX_TYPE", + config.get("postgres", "vector_index_type", fallback="HNSW"), + ), + "hnsw_m": int( + os.environ.get( + "POSTGRES_HNSW_M", + config.get("postgres", "hnsw_m", fallback="16"), + ) + ), + "hnsw_ef": int( + os.environ.get( + "POSTGRES_HNSW_EF", + config.get("postgres", "hnsw_ef", fallback="64"), + ) + ), + "ivfflat_lists": int( + os.environ.get( + "POSTGRES_IVFFLAT_LISTS", + config.get("postgres", "ivfflat_lists", fallback="100"), + ) + ), } @classmethod