【实战】ChatChat0.3.1+DeepSeek+本地知识库部署使用(上)
一、介绍
🤖️ Langchain-Chatchat是一种利用 langchain 思想实现的基于本地知识库的问答应用,目标期望建立一套对中文场景与开源模型支持友好、可离线运行的知识库问答解决方案。
💡 受 GanymedeNil 的项目 document.ai 和 AlexZhangji 创建的 ChatGLM-6B Pull Request 启发,建立了全流程可使用开源模型实现的本地知识库问答应用。本项目的最新版本中可使用 Xinference、Ollama 等框架接入 GLM-4-Chat、 Qwen2-Instruct、 Llama3 等模型,依托于 langchain 框架支持通过基于 FastAPI 提供的 API 调用服务,或使用基于 Streamlit 的 WebUI 进行操作。
二、基本原理
本项目实现原理如下图所示,过程包括加载文件 -> 读取文本 -> 文本分割 -> 文本向量化 -> 问句向量化 -> 在文本向量中匹配出与问句向量最相似的 top k
个 -> 匹配出的文本作为上下文和问题一起添加到 prompt
中 -> 提交给 LLM
生成回答。
三、AutoDL快速使用
1.选择社区镜像
2.一键启动
cd /root
conda activate chatchat
bash startup.sh
3.使用ssh隧道
4.项目启动
人工智障对于经典问题的回答:
四、源码安装部署
1.选择基础环境
PyTorch:2.5.1
Python:3.12(ubuntu22.04)
Cuda:12.4
GPU:RTX 4090(24GB) * 1
CPU:16 vCPU Intel(R) Xeon(R) Gold 6430
2.源码拉取
git clone https://github.com/chatchat-space/Langchain-Chatchat.git
3.创建虚拟环境
##Langchain环境
conda create -n chat python=3.8
#conda初始化
conda init
source ~/.bashrc
conda activate chat
##安装库环境
cd Langchain-Chatchat/libs/chatchat-server/
pip install poetry
poetry config virtualenvs.prefer-active-python true
poetry install --with lint,test -E xinference
##xinference环境
conda create -n xinference python=3.9
conda activate xinference
pip install "xinference[all]" -i https://pypi.tuna.tsinghua.edu.cn/simple
4.配置xinference
##必须是同一行命令
XINFERENCE_HOME=/root/autodl-tmp/Langchain-Chatchat/xinference XINFERENCE_MODEL_SRC=modelscope xinference-local --host 0.0.0.0 --port 9997
5.LangChain初始化
##设置当前目录和项目数据目录
cd Langchain-Chatchat/libs/chatchat-server/chatchat
export CHATCHAT_ROOT=/root/autodl-tmp/Langchain-Chatchat/libs/chatchat-server/chatchat/path/to/chatchat_data
##执行以下命令初始化项目配置文件和数据目录:
cd libs/chatchat-server
python chatchat/cli.py init
##初始化知识库
cd libs/chatchat-server
python chatchat/cli.py kb --recreate-vs
##启动项目
cd libs/chatchat-server
python chatchat/cli.py start -a
五、遇到的坑
1.poetry config virtualenvs.prefer-active-python true失败
这一问题和虚拟环境的python版本有关,不同版本适配的poetry估计存在差异,导致命令已经出现更改,实测3.9/3.10均会出现报错,3.8版本下的poetry暂时可以正常设置
2.Unable to find installation candidates for onnxruntime (1.20.1)
这一问题是onnxruntime版本过高导致,实测1.19.0版本的onnxruntime可以正常安装,出现此类问题,可以针对性安装1.19.0版本,然后重新运行命令
poetry add onnxruntime==1.19.0
3.ERROR: Failed to build installable wheels for some pyproject.toml based projects (llama-cpp-python)
llama-cpp-python无法通过pip安装,建议直接使用whl方法安装,安装方法如下:
##加速资源
wget https://ghfast.top/https://github.com/abetlen/llama-cpp-python/releases/download/v0.3.4-cu122/llama_cpp_python-0.3.4-cp39-cp39-linux_x86_64.whl
pip install llama_cpp_python-0.3.4-cp39-cp39-linux_x86_64.whl
4.Langchain项目启动报错ModuleNotFoundError: No module named ‘chatchat’
一般就是portry安装包过程出现问题,按照以下步骤进行操作:
1.清理poetry的缓存pypoetry文件夹,请自行搜索不同操作系统下的路径。
2.检查Langchain-Chatchat\libs\chatchat-server文件夹下是否有poetry.lock文件,请删除。
3.再次执行poetry install --with lint,test -E xinference
4.检查安装过程有没有报错,没有报错再次使用pip list检查你是否成功安装前面所说的依赖包。
5.调用知识库或者RAG过程报错proxies参数类型错误
此错误与库版本有关httpx,应该将httpx软件包降级到版本 0.27.2,该proxies版本支持该参数。运行以下命令:
pip install httpx==0.27.2