ChromaDB插入数据报错:无法用神经网络模型计算预测
问题
运行以下代码向ChromaDB集合添加数据时出现CoreML执行错误:
import ollama, chromadb, time chroma_client = chromadb.Client() #chroma_client = chromadb.HttpClient(host="localhost", port=8000) collectionname = "my_collection" print(chroma_client.list_collections()) if any(collection.name == collectionname for collection in chroma_client.list_collections()): print("deleting collection") chroma_client.delete_collection("my_collection") collection = chroma_client.get_or_create_collection(name=collectionname, metadata={"hnsw:space": "cosine"}) collection.upsert( documents=[ "This is a document about machine learning", "This is another document about data science", "A third document about artificial intelligence" ], metadatas=[ {"source": "test1"}, {"source": "test2"}, {"source": "test3"} ], ids=[ "id1", "id2", "id3" ] )
错误信息:
2024-07-13 17:31:45.534848 [E:onnxruntime:, sequential_executor.cc:516 ExecuteKernel] Non-zero status code returned while running CoreML_16713284627214834184_1 node. Name:'CoreMLExecutionProvider_CoreML_16713284627214834184_1_1' Status Message: Error executing model: Unable to compute the prediction using a neural network model. It can be an invalid input data or broken/unsupported model (error code: -1). Traceback (most recent call last): File "/Users/joe/development/python/gettings_started_chromadb.py", line 15, in <module> collection.upsert( File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/chromadb/api/models/Collection.py", line 296, in upsert ) = self._validate_and_prepare_upsert_request( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/chromadb/api/models/CollectionCommon.py", line 531, in _validate_and_prepare_upsert_request embeddings = self._embed(input=documents) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/chromadb/api/models/CollectionCommon.py", line 568, in _embed return self._embedding_function(input=input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/chromadb/api/types.py", line 211, in __call__ result = call(self, input) ^^^^^^^^^^^^^^^^^ File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/chromadb/utils/embedding_functions/onnx_mini_lm_l6_v2.py", line 200, in __call__ return cast(Embeddings, self._forward(input).tolist()) ^^^^^^^^^^^^^^^^^^^^ File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/chromadb/utils/embedding_functions/onnx_mini_lm_l6_v2.py", line 143, in _forward model_output = self.model.run(None, onnx_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/joe/development/python/venv/dev/lib/python3.11/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 220, in run return self._sess.run(output_names, input_feed, run_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while running CoreML_16713284627214834184_1 node. Name:'CoreMLExecutionProvider_CoreML_16713284627214834184_1_1' Status Message: Error executing model: Unable to compute the prediction using a neural network model. It can be an invalid input data or broken/unsupported model (error code: -1).
已尝试清理Python虚拟环境、Docker部署,问题仍存在。
解决方案
禁用CoreML执行器
强制ONNXRuntime使用CPU执行,避免CoreML兼容性问题。在代码开头添加:import os os.environ["ONNXRUNTIME_EXECUTION_PROVIDERS"] = "CPUExecutionProvider"更换嵌入模型
改用Ollama提供的嵌入函数替代Chroma默认的ONNX模型,修改集合创建代码:from chromadb.utils.embedding_functions import OllamaEmbeddingFunction embedding_function = OllamaEmbeddingFunction( model_name="llama3", # 使用本地已拉取的Ollama模型 base_url="http://localhost:11434" ) collection = chroma_client.get_or_create_collection( name=collectionname, metadata={"hnsw:space": "cosine"}, embedding_function=embedding_function )注意:需确保Ollama已启动,且指定的模型已通过
ollama pull llama3拉取到本地。调整ONNXRuntime版本
当前版本的ONNXRuntime可能存在CoreML兼容bug,尝试安装稳定版:pip install onnxruntime==1.16.3 --force-reinstall检查系统CoreML支持
若使用旧版macOS(低于10.15 Catalina),CoreML对部分ONNX模型支持有限,需升级系统至符合要求的版本。
内容的提问来源于stack exchange,提问作者Joe Forrester
相关产品推荐
相关产品推荐

