最近在学习基于本地知识库的大语言模型应用 langchain-ChatGLM ,和清华开源大模型 chatGLM2-6b 模型的本地化部署。在本地化部署过程中遇到了paddleocr包的导入问题,并报错 ModuleNotFoundError: No module named 'tools.infer'; 'tools' is not a package 本文将介绍整个问题的排查过程及最终的解决方案。

1、问题描述及排查

工程启动命令:

python cli_demo.py --no-remote-model --model-name chatglm-6b

当我们执行工程的demo启动脚本时,返回如下异常:

INFO  2023-06-28 11:33:18,008-1d:
loading model config
llm device: cuda
embedding device: cuda
dir: /ChatGLM/langchain-ChatGLM
flagging username: de2259dd2a9446c691c097527bdce7c1
INFO  2023-06-28 11:33:19,658-1d: Note: NumExpr detected 32 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
INFO  2023-06-28 11:33:19,659-1d: NumExpr defaulting to 8 threads.
/opt/conda/envs/chatglm/lib/python3.9/site-packages/pkg_resources/__init__.py:121: DeprecationWarning: pkg_resources is deprecated as an API
  warnings.warn("pkg_resources is deprecated as an API", DeprecationWarning)
/opt/conda/envs/chatglm/lib/python3.9/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to`pkg_resources.declare_namespace('mpl_toolkits')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/opt/conda/envs/chatglm/lib/python3.9/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to`pkg_resources.declare_namespace('google')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/opt/conda/envs/chatglm/lib/python3.9/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to`pkg_resources.declare_namespace('zope')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
/opt/conda/envs/chatglm/lib/python3.9/site-packages/pkg_resources/__init__.py:2870: DeprecationWarning: Deprecated call to`pkg_resources.declare_namespace('zope')`.
Implementing implicit namespace packages (as specified in PEP 420) is preferred to `pkg_resources.declare_namespace`. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
  declare_namespace(pkg)
Traceback (most recent call last):
  File "/ChatGLM/langchain-ChatGLM/cli_demo.py", line 2, in <module>
    from chains.local_doc_qa import LocalDocQA
  File "/ChatGLM/langchain-ChatGLM/chains/local_doc_qa.py", line 11, in <module>
    from loader import UnstructuredPaddleImageLoader, UnstructuredPaddlePDFLoader
  File "/ChatGLM/langchain-ChatGLM/loader/__init__.py", line 1, in <module>
    from .image_loader import UnstructuredPaddleImageLoader
  File "/ChatGLM/langchain-ChatGLM/loader/image_loader.py", line 5, in <module>
    from paddleocr import PaddleOCR
  File "/opt/conda/envs/chatglm/lib/python3.9/site-packages/paddleocr/__init__.py", line 14, in <module>
    from .paddleocr import *
  File "/opt/conda/envs/chatglm/lib/python3.9/site-packages/paddleocr/paddleocr.py", line 37, in <module>
    from tools.infer import predict_system
ModuleNotFoundError: No module named 'tools.infer'; 'tools' is not a package

根据错误日打印的堆栈信息,我们可以判断,该问题是由于 paddleocr 包中 paddleocr.py 在引入 tools.infer 时没有使用绝对路径产生的歧义导致的。因此我们后续的解决方案的重点是解决 import 导致的歧义。具体解决过程如下。

2、问题解决

(1)、删除会导致依赖冲突的其他pip包

在 langchain-chatGLM 的项目依赖中我们看到,项目中 pdf 加载由先前的 detectron2 替换为使用 paddleocr,如果之前有安装过 detectron2 需要先完成卸载避免引发 tools 冲突。

pip uninstall detectron2

如果问题没有解决,请继续看方案2。

(2)、手动升级paddleocr包

为了彻底解决包引入歧义的问题,需要将 paddleocr.py 的引用路径改为绝对路径,然后重新安装 paddleocr包解决。 我们首先下载 PaddleOCR 源码。

git clone https://github.com/PaddlePaddle/PaddleOCR.git

通过阅读源码可以发现 PaddlerOCR项目的某次提交 github.com/PaddlePaddl… ,已经修复了这个问题。

结合 PyPI 中的版本记录 最新版本 v2.6.1.3 发布日期是 2023-02-08,但是Improve package import compatibility 提交合并于 2023-03-29 可以猜测,是由于 pip 包最新版本没有包含该提交记录导致的。

因此需要我们通过手动安装 paddleocr 包的方式解决,具体流程如下:

卸载原有 paddleocr 包

pip uninstall paddleocr
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
pip install -e .

最终问题圆满解决。

  • ChatGPT保姆级教程,一分钟学会使用ChatGPT!
  • OpenAI 推出超神 ChatGPT 注册攻略来了
  • 手把手教你注册和使用ChatGPT
  • 基于 ChatGPT 和 React 搭建 JSON 转 TS 的 Web 应用
  •