From b65eb6dc81ce15cdf901b8e836a20f4576ea2ef3 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Sat, 30 Aug 2025 11:26:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(db):=20=E4=BC=98=E5=8C=96ChromaDB?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8F=82=E6=95=B0=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将查询参数动态构建,仅在`where`条件存在时才将其添加到查询中。此举增强了代码的健壮性和灵活性,避免了传入空的`where`字典可能引发的潜在问题。 --- src/common/vector_db/chromadb_impl.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/common/vector_db/chromadb_impl.py b/src/common/vector_db/chromadb_impl.py index 8e9313b3b..363f5fcbe 100644 --- a/src/common/vector_db/chromadb_impl.py +++ b/src/common/vector_db/chromadb_impl.py @@ -92,12 +92,14 @@ class ChromaDBImpl(VectorDBBase): collection = self.get_or_create_collection(collection_name) if collection: try: - return collection.query( - query_embeddings=query_embeddings, - n_results=n_results, - where=where or {}, + query_params = { + "query_embeddings": query_embeddings, + "n_results": n_results, **kwargs, - ) + } + if where: + query_params["where"] = where + return collection.query(**query_params) except Exception as e: logger.error(f"查询集合 '{collection_name}' 失败: {e}") return {}