feat: multi-collection support — each project uses its own isolated collection

This commit is contained in:
2026-07-05 02:07:02 +08:00
parent 8042cf288b
commit 0aee167085
2 changed files with 59 additions and 18 deletions
+39 -6
View File
@@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
class AppState:
"""应用级共享状态."""
"""应用级共享状态 — db 和 embedder 全局共享, searcher/ingestor 按 collection 懒创建."""
def __init__(self):
config_path = os.environ.get("MD_VECTOR_CONFIG", "config.yaml")
@@ -22,20 +22,48 @@ class AppState:
"MD_VECTOR_DB_DATA_DIR", self.config.chroma.persist_dir
)
self.db = VectorDB(persist_dir=data_dir)
self.embedder = create_embedder(self.config.embed)
collection = os.environ.get(
self.default_collection = os.environ.get(
"MD_VECTOR_DB_COLLECTION", self.config.chroma.collection_name
)
self.searcher = Searcher(self.db, self.embedder, collection)
self.ingestor = DocumentIngestor(self.db, self.embedder, collection)
# 按 collection 懒加载 searcher / ingestor
self._searchers: dict[str, Searcher] = {}
self._ingestors: dict[str, DocumentIngestor] = {}
def get_searcher(self, collection: str | None = None) -> Searcher:
name = collection or self.default_collection
if name not in self._searchers:
self._searchers[name] = Searcher(self.db, self.embedder, name)
return self._searchers[name]
def get_ingestor(self, collection: str | None = None) -> DocumentIngestor:
name = collection or self.default_collection
if name not in self._ingestors:
self._ingestors[name] = DocumentIngestor(self.db, self.embedder, name)
return self._ingestors[name]
def list_collections_with_stats(self) -> list[dict]:
"""列出所有 collection 及其统计."""
result = []
all_names = set(self._searchers.keys()) | set(self._ingestors.keys())
all_names.add(self.default_collection)
for name in sorted(all_names):
try:
coll = self.db.get_or_create_collection(name)
result.append({"name": name, "count": coll.count()})
except Exception:
result.append({"name": name, "count": 0})
return result
def is_healthy(self) -> dict:
"""真实健康检查: 验证 ChromaDB 和 Embedder 是否可用."""
status = {"status": "ok", "checks": {}}
try:
count = self.searcher.collection.count()
count = self.db.get_or_create_collection(
self.default_collection
).count()
status["checks"]["chromadb"] = {"status": "ok", "count": count}
except Exception as e:
status["checks"]["chromadb"] = {"status": "error", "detail": str(e)}
@@ -59,3 +87,8 @@ def get_state() -> AppState:
logger.info("初始化应用状态...")
_state = AppState()
return _state
def get_default_collection() -> str:
"""获取默认 collection 名, 供 CLI 等非 HTTP 场景使用."""
return get_state().default_collection