refactor: make cosine similarity threshold a required config parameter

• Remove default threshold from env var
• Add validation for missing threshold
• Move default to lightrag.py config init
• Update all vector DB implementations
• Improve threshold validation consistency
This commit is contained in:
yangdx
2025-02-13 03:25:48 +08:00
parent 3308ecfa69
commit f01f57d0da
9 changed files with 59 additions and 30 deletions

View File

@@ -23,14 +23,15 @@ class FaissVectorDBStorage(BaseVectorStorage):
Uses cosine similarity by storing normalized vectors in a Faiss index with inner product search.
"""
cosine_better_than_threshold: float = float(os.getenv("COSINE_THRESHOLD", "0.2"))
cosine_better_than_threshold: float = None
def __post_init__(self):
# Grab config values if available
config = self.global_config.get("vector_db_storage_cls_kwargs", {})
self.cosine_better_than_threshold = config.get(
"cosine_better_than_threshold", self.cosine_better_than_threshold
)
cosine_threshold = config.get("cosine_better_than_threshold")
if cosine_threshold is None:
raise ValueError("cosine_better_than_threshold must be specified in vector_db_storage_cls_kwargs")
self.cosine_better_than_threshold = cosine_threshold
# Where to save index file if you want persistent storage
self._faiss_index_file = os.path.join(