mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-09-08 09:06:40 +08:00
fix(chat): 修复 ChromaDB 1.x 兼容性问题导致索引失败
- ChromaDB 1.x delete/get 不存在的 collection 抛 NotFoundError 而非 ValueError,统一改为 except Exception - 简化 _collection_name,UUID 格式本身就是合法的 collection name - requirements.txt 放宽 chromadb 版本约束 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
3cd4c749c1
commit
fdc888512a
@@ -66,13 +66,8 @@ class VectorStoreManager:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def _collection_name(self, task_id: str) -> str:
|
def _collection_name(self, task_id: str) -> str:
|
||||||
"""ChromaDB collection 名称需满足限制:3-63字符,字母数字开头结尾。"""
|
"""ChromaDB collection 名称:直接使用 task_id(UUID 格式合法)。"""
|
||||||
safe = re.sub(r'[^a-zA-Z0-9_-]', '_', task_id)[:60]
|
return task_id
|
||||||
if not safe or not safe[0].isalnum():
|
|
||||||
safe = "t" + safe
|
|
||||||
if not safe[-1].isalnum():
|
|
||||||
safe = safe + "0"
|
|
||||||
return safe
|
|
||||||
|
|
||||||
def index_task(self, task_id: str) -> None:
|
def index_task(self, task_id: str) -> None:
|
||||||
"""读取笔记结果并建立向量索引。"""
|
"""读取笔记结果并建立向量索引。"""
|
||||||
@@ -101,7 +96,7 @@ class VectorStoreManager:
|
|||||||
# 删除旧 collection(幂等)
|
# 删除旧 collection(幂等)
|
||||||
try:
|
try:
|
||||||
self._client.delete_collection(col_name)
|
self._client.delete_collection(col_name)
|
||||||
except ValueError:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
collection = self._client.create_collection(
|
collection = self._client.create_collection(
|
||||||
@@ -121,7 +116,7 @@ class VectorStoreManager:
|
|||||||
col_name = self._collection_name(task_id)
|
col_name = self._collection_name(task_id)
|
||||||
try:
|
try:
|
||||||
collection = self._client.get_collection(col_name)
|
collection = self._client.get_collection(col_name)
|
||||||
except ValueError:
|
except Exception:
|
||||||
logger.warning(f"Collection 不存在: {col_name}")
|
logger.warning(f"Collection 不存在: {col_name}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -142,7 +137,7 @@ class VectorStoreManager:
|
|||||||
try:
|
try:
|
||||||
self._client.delete_collection(col_name)
|
self._client.delete_collection(col_name)
|
||||||
logger.info(f"已删除向量索引: {task_id}")
|
logger.info(f"已删除向量索引: {task_id}")
|
||||||
except ValueError:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def is_indexed(self, task_id: str) -> bool:
|
def is_indexed(self, task_id: str) -> bool:
|
||||||
@@ -151,5 +146,5 @@ class VectorStoreManager:
|
|||||||
try:
|
try:
|
||||||
col = self._client.get_collection(col_name)
|
col = self._client.get_collection(col_name)
|
||||||
return col.count() > 0
|
return col.count() > 0
|
||||||
except ValueError:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ celery==5.5.1
|
|||||||
certifi==2025.1.31
|
certifi==2025.1.31
|
||||||
cffi==1.17.1
|
cffi==1.17.1
|
||||||
charset-normalizer==3.4.1
|
charset-normalizer==3.4.1
|
||||||
chromadb==0.6.3
|
chromadb>=0.5.0
|
||||||
click==8.1.8
|
click==8.1.8
|
||||||
click-didyoumean==0.3.1
|
click-didyoumean==0.3.1
|
||||||
click-plugins==1.1.1
|
click-plugins==1.1.1
|
||||||
|
|||||||
Reference in New Issue
Block a user