Llama Index与transformers集成Hugging Face模型时出现LocalEntryNotFoundError导入错误
我明白你在把Llama Index和transformers结合调用Hugging Face模型时遇到了这个烦人的导入错误,结合你给出的代码、报错信息和已经尝试的操作,我来帮你梳理更针对性的解决思路。
先明确你的问题场景:
你正在开发AI项目,核心代码片段如下:
from llama_index.core import Settings from llama_index.embeddings.huggingface import HuggingFaceInferenceAPIEmbedding import tiktoken from llama_index.core.callbacks import CallbackManager, TokenCountingHandler from llama_index.core.service_context import ServiceContext from llama_index.core.postprocessor import SentenceTransformerRerank from llama_index.llms.huggingface import HuggingFaceInferenceAPI
运行时触发了以下错误链:
ImportError: cannot import name 'LocalEntryNotFoundError' from 'huggingface_hub.errors' (/usr/local/lib/python3.10/dist-packages/huggingface_hub/errors.py)
During handling of the above exception, this occurred:
RuntimeError: Failed to import transformers.trainer because of the following error: cannot import name 'LocalEntryNotFoundError' from 'huggingface_hub.errors'
你已经尝试过验证包版本、检查API密钥与模型配置,还执行了升级命令:
%pip install --upgrade transformers huggingface_hub llama-index-llms-huggingface llama-index-llms-huggingface-api
问题根源
这个错误的核心是huggingface_hub与transformers的版本不兼容:LocalEntryNotFoundError这个类在较新的huggingface_hub版本中被重命名或移除了,但你当前安装的transformers版本仍在尝试导入它,导致了版本冲突。模糊的--upgrade命令可能拉取了最新但不兼容的版本组合,反而加剧了问题。
针对性解决方案
完全卸载后重新安装兼容版本组合
先清理现有可能冲突的包:pip uninstall -y transformers huggingface_hub llama-index-llms-huggingface llama-index-llms-huggingface-api然后安装经过验证的稳定兼容版本:
pip install transformers==4.35.2 huggingface_hub==0.17.3 llama-index-llms-huggingface==0.1.7 llama-index-llms-huggingface-api==0.1.3检查并确保环境隔离
- 如果使用了虚拟环境,确认已激活项目对应的虚拟环境,避免全局包和项目包发生冲突
- 运行
pip list查看当前环境下的包版本,确认huggingface_hub和transformers的版本与上面指定的一致
重启运行环境
如果你是在Colab、Jupyter Notebook这类交互式环境中,升级完包后一定要重启运行时,确保新安装的包生效。
备注:内容来源于stack exchange,提问作者Deepak Singh Rajput




