samuel-z-chen commited on
Commit
08fc5c9
·
1 Parent(s): 552eb31

Add support to namespace_prefix

Browse files
Files changed (1) hide show
  1. lightrag/kg/postgres_impl.py +11 -5
lightrag/kg/postgres_impl.py CHANGED
@@ -254,6 +254,8 @@ class PGKVStorage(BaseKVStorage):
254
  db: PostgreSQLDB = field(default=None)
255
 
256
  def __post_init__(self):
 
 
257
  self._max_batch_size = self.global_config["embedding_batch_num"]
258
 
259
  async def initialize(self):
@@ -269,7 +271,7 @@ class PGKVStorage(BaseKVStorage):
269
 
270
  async def get_by_id(self, id: str) -> dict[str, Any] | None:
271
  """Get doc_full data by id."""
272
- sql = SQL_TEMPLATES["get_by_id_" + self.namespace]
273
  params = {"workspace": self.db.workspace, "id": id}
274
  if is_namespace(self.namespace, NameSpace.KV_STORE_LLM_RESPONSE_CACHE):
275
  array_res = await self.db.query(sql, params, multirows=True)
@@ -283,7 +285,7 @@ class PGKVStorage(BaseKVStorage):
283
 
284
  async def get_by_mode_and_id(self, mode: str, id: str) -> Union[dict, None]:
285
  """Specifically for llm_response_cache."""
286
- sql = SQL_TEMPLATES["get_by_mode_id_" + self.namespace]
287
  params = {"workspace": self.db.workspace, mode: mode, "id": id}
288
  if is_namespace(self.namespace, NameSpace.KV_STORE_LLM_RESPONSE_CACHE):
289
  array_res = await self.db.query(sql, params, multirows=True)
@@ -297,7 +299,7 @@ class PGKVStorage(BaseKVStorage):
297
  # Query by id
298
  async def get_by_ids(self, ids: list[str]) -> list[dict[str, Any]]:
299
  """Get doc_chunks data by id"""
300
- sql = SQL_TEMPLATES["get_by_ids_" + self.namespace].format(
301
  ids=",".join([f"'{id}'" for id in ids])
302
  )
303
  params = {"workspace": self.db.workspace}
@@ -318,7 +320,7 @@ class PGKVStorage(BaseKVStorage):
318
 
319
  async def get_by_status(self, status: str) -> Union[list[dict[str, Any]], None]:
320
  """Specifically for llm_response_cache."""
321
- SQL = SQL_TEMPLATES["get_by_status_" + self.namespace]
322
  params = {"workspace": self.db.workspace, "status": status}
323
  return await self.db.query(SQL, params, multirows=True)
324
 
@@ -391,6 +393,8 @@ class PGVectorStorage(BaseVectorStorage):
391
 
392
  def __post_init__(self):
393
  self._max_batch_size = self.global_config["embedding_batch_num"]
 
 
394
  config = self.global_config.get("vector_db_storage_cls_kwargs", {})
395
  cosine_threshold = config.get("cosine_better_than_threshold")
396
  if cosine_threshold is None:
@@ -493,7 +497,9 @@ class PGVectorStorage(BaseVectorStorage):
493
  embedding = embeddings[0]
494
  embedding_string = ",".join(map(str, embedding))
495
 
496
- sql = SQL_TEMPLATES[self.namespace].format(embedding_string=embedding_string)
 
 
497
  params = {
498
  "workspace": self.db.workspace,
499
  "better_than_threshold": self.cosine_better_than_threshold,
 
254
  db: PostgreSQLDB = field(default=None)
255
 
256
  def __post_init__(self):
257
+ namespace_prefix = self.global_config.get("namespace_prefix")
258
+ self.base_namespace = self.namespace.replace(namespace_prefix, "")
259
  self._max_batch_size = self.global_config["embedding_batch_num"]
260
 
261
  async def initialize(self):
 
271
 
272
  async def get_by_id(self, id: str) -> dict[str, Any] | None:
273
  """Get doc_full data by id."""
274
+ sql = SQL_TEMPLATES["get_by_id_" + self.base_namespace]
275
  params = {"workspace": self.db.workspace, "id": id}
276
  if is_namespace(self.namespace, NameSpace.KV_STORE_LLM_RESPONSE_CACHE):
277
  array_res = await self.db.query(sql, params, multirows=True)
 
285
 
286
  async def get_by_mode_and_id(self, mode: str, id: str) -> Union[dict, None]:
287
  """Specifically for llm_response_cache."""
288
+ sql = SQL_TEMPLATES["get_by_mode_id_" + self.base_namespace]
289
  params = {"workspace": self.db.workspace, mode: mode, "id": id}
290
  if is_namespace(self.namespace, NameSpace.KV_STORE_LLM_RESPONSE_CACHE):
291
  array_res = await self.db.query(sql, params, multirows=True)
 
299
  # Query by id
300
  async def get_by_ids(self, ids: list[str]) -> list[dict[str, Any]]:
301
  """Get doc_chunks data by id"""
302
+ sql = SQL_TEMPLATES["get_by_ids_" + self.base_namespace].format(
303
  ids=",".join([f"'{id}'" for id in ids])
304
  )
305
  params = {"workspace": self.db.workspace}
 
320
 
321
  async def get_by_status(self, status: str) -> Union[list[dict[str, Any]], None]:
322
  """Specifically for llm_response_cache."""
323
+ SQL = SQL_TEMPLATES["get_by_status_" + self.base_namespace]
324
  params = {"workspace": self.db.workspace, "status": status}
325
  return await self.db.query(SQL, params, multirows=True)
326
 
 
393
 
394
  def __post_init__(self):
395
  self._max_batch_size = self.global_config["embedding_batch_num"]
396
+ namespace_prefix = self.global_config.get("namespace_prefix")
397
+ self.base_namespace = self.namespace.replace(namespace_prefix, "")
398
  config = self.global_config.get("vector_db_storage_cls_kwargs", {})
399
  cosine_threshold = config.get("cosine_better_than_threshold")
400
  if cosine_threshold is None:
 
497
  embedding = embeddings[0]
498
  embedding_string = ",".join(map(str, embedding))
499
 
500
+ sql = SQL_TEMPLATES[self.base_namespace].format(
501
+ embedding_string=embedding_string
502
+ )
503
  params = {
504
  "workspace": self.db.workspace,
505
  "better_than_threshold": self.cosine_better_than_threshold,