使用ChromaDB添加文档时内核重启的原因及解决方法
ChromaDB添加文档时内核崩溃的原因及修复方案
可能的原因
- 嵌入模型加载失败或内存资源不足:ChromaDB默认依赖sentence-transformers的嵌入模型,若模型未正确下载、或机器内存不足以支撑模型运行,会触发内核崩溃。
- 依赖版本冲突:ChromaDB与torch、sentence-transformers等依赖版本不兼容,引发底层运行错误。
- 代码拼写错误:示例代码中
"This document os about Tunisia"的os应为is,虽大概率不是直接崩溃原因,但可能触发隐性解析异常。 - 本地存储权限不足:ChromaDB的默认存储目录无读写权限,写入文档时触发错误导致内核重启。
修复步骤
- 修正代码拼写错误:将错误的
os改为is,修正后代码:collection.add( documents=[ "This document is about New York", "This document is about Tunisia" ], ids = ['id1','id2'] ) - 调整嵌入模型配置:
- 若内存不足,改用轻量型嵌入模型,手动指定模型并初始化集合:
from chromadb.utils import embedding_functions embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="all-MiniLM-L6-v2") collection = client.create_collection(name="your_collection_name", embedding_function=embedding_func) - 确保模型已成功下载,若网络问题可手动下载模型文件后指定路径。
- 若内存不足,改用轻量型嵌入模型,手动指定模型并初始化集合:
- 重新安装兼容版本的依赖:
卸载现有冲突依赖,安装稳定兼容版本:pip uninstall chromadb sentence-transformers torch -y pip install chromadb==0.4.24 sentence-transformers==2.2.2 torch==2.1.0 - 检查并设置存储目录权限:
确保ChromaDB的存储目录有读写权限,或手动指定有权限的路径:import chromadb client = chromadb.PersistentClient(path="/path/to/writable-directory") - 优化内存使用:关闭其他占用内存的程序,若在Jupyter环境中,调整内核的内存分配限制。
内容的提问来源于stack exchange,提问作者Farah Bouali
相关产品推荐
相关产品推荐

