Langchain-Chatchat大语言模型本地知识库的踩坑、部署、使用

Langchain-Chatchat

概述

Langchain-Chatchat是一个基于ChatGLM大语言模型与Langchain应用框架实现,开源、可离线部署的检索增强生成(RAG)大模型的本地知识库问答应用项目。

GitHub:https://github.com/chatchat-space/Langchain-Chatchat

实现原理

本项目实现原理如下图所示,过程包括加载文件 -> 读取文本 -> 文本分割 -> 文本向量化 -> 问句向量化 -> 在文本向量中匹配出与问句向量最相似的 top k个 -> 匹配出的文本作为上下文和问题一起添加到 prompt中 -> 提交给 LLM生成回答。

开发环境准备

软件要求

使用Ubuntu系统,其他系统可能出现系统兼容性问题

python 复制代码
Linux Ubuntu 22.04.5 kernel version 6.7

最低要求

该要求仅针对标准模式,轻量模式使用在线模型,不需要安装torch等库,也不需要显卡即可运行。

Python 版本: >= 3.8(很不稳定), < 3.11
CUDA 版本: >= 12.1

推荐要求

开发者在以下环境下进行代码调试,在该环境下能够避免最多环境问题。

Python 版本 == 3.10.12
CUDA 版本: == 12.3

硬件要求

如果为了体验使用该项目,可以使用线上模型,不需要太高的硬件要求。
如果想要顺利在GPU运行本地模型的 int4 量化版本,至少需要以下的硬件配置

ChatGLM2-6B & LLaMA-7B

makefile 复制代码
最低显存要求: 7GB

推荐显卡: RTX 3060, RTX 2060

LLaMA-13B

yaml 复制代码
最低显存要求: 11GB

推荐显卡: RTX 2060 12GB, RTX 3060 12GB, RTX 3080, RTX A2000

Qwen-14B-Chat

makefile 复制代码
最低显存要求: 13GB

推荐显卡: RTX 3090

LLaMA-30B

yaml 复制代码
最低显存要求: 22GB

推荐显卡: RTX A5000, RTX 3090, RTX 4090, RTX 6000, Tesla V100, RTX Tesla P40

LLaMA-65B

makefile 复制代码
最低显存要求: 40GB

推荐显卡: A100, A40, A6000

注意:

go 复制代码
若使用 int8 推理,则显存大致为 int4 推理要求的 1.5 倍

若使用 fp16 推理,则显存大致为 int4 推理要求的 2.5 倍

数据仅为估算,实际情况以 nvidia-smi 占用为准。

同时,Embedding 模型将会占用 1-2G 的显存,历史记录最多会占用数 G 显存,因此,需要多冗余一些显存。

内存最低要求: 内存要求至少应该比模型运行的显存大。

部署

拉取仓库

python 复制代码
git clone https://github.com/chatchat-space/Langchain-Chatchat.git

# 进入目录
cd Langchain-Chatchat

创建虚拟环境

创建一个虚拟环境,使项目与项目间进行软件版本隔离

python 复制代码
conda create -n chat python=3.10

# 激活使用虚拟环境
conda activate chat 

安装全部依赖

在虚拟环境内安装项目的依赖

python 复制代码
pip install -r requirements.txt 
pip install -r requirements_api.txt
pip install -r requirements_webui.txt  

默认依赖包括基本运行环境(FAISS向量库)。如果要使用 milvus/pg_vector 等向量库,请将 requirements.txt 中相应依赖取消注释再安装。

初始化配置文件

python 复制代码
python copy_config_example.py

脚本将会将所有config目录下的配置文件样例复制一份到config目录下,方便开发者进行配置。 接着,开发者可以根据自己的需求,对配置文件进行修改。

basic_config.py:基础配置项:配置记录日志的格式和储存路径,通常不需要修改。

kb_config.py:数据库配置:配置分词器、知识库、向量数据库等信息

model_config.py:模型配置项:包含本地LLM模型、本地Embeddings模型、在线LLM模型API的相关配置

prompt_config.py:提示词配置项:提示词配置分为三个板块,分别对应三种聊天类型:基础的对话提示词、与知识库对话的提示词、与Agent对话的提示词。

server_config.py:服务和端口配置项:不需要进行大量的修改,仅需确保对应的端口打开,并不互相冲突即可。server_config.py中的配置优先于startup.py中的默认值,注意避免配置文件覆盖

初始化知识库

第一次运行本项目,知识库尚未建立,或者配置文件中的知识库类型、嵌入模型发生变化,需要以下命令初始化或重建知识库:

python 复制代码
python init_database.py --recreate-vs

如果已经有创建过知识库,可以先执行以下命令创建或更新数据库表:

python 复制代码
$ python init_database.py --create-tables

第一次使用时,会自动下载BAAI/bge-large-zh模型,用于知识库的初始化构建

python 复制代码
recreating all vector stores
2023-12-06 11:31:27,108 - faiss_cache.py[line:80] - INFO: loading vector store in 'samples/vector_store/bge-large-zh' from disk.
2023-12-06 11:31:28,013 - SentenceTransformer.py[line:66] - INFO: Load pretrained SentenceTransformer: BAAI/bge-large-zh
.gitattributes: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.52k/1.52k [00:00<?, ?B/s]
1_Pooling/config.json: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 191/191 [00:00<?, ?B/s]
README.md: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 27.9k/27.9k [00:00<00:00, 1.78MB/s]
config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 941/941 [00:00<?, ?B/s]
config_sentence_transformers.json: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 124/124 [00:00<?, ?B/s]
model.safetensors: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.30G/1.30G [03:50<00:00, 5.66MB/s]
pytorch_model.bin: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.30G/1.30G [04:09<00:00, 5.22MB/s]
sentence_bert_config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 52.0/52.0 [00:00<?, ?B/s]
special_tokens_map.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 125/125 [00:00<00:00, 223kB/s]
tokenizer.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 439k/439k [00:00<00:00, 463kB/s]
tokenizer_config.json: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 366/366 [00:00<?, ?B/s]
vocab.txt: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 110k/110k [00:00<00:00, 331kB/s]
modules.json: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 349/349 [00:00<?, ?B/s]
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:02<00:00,  2.13s/it]
2023-12-06 11:42:08,536 - loader.py[line:54] - INFO: Loading faiss with AVX2 support.
2023-12-06 11:42:08,537 - loader.py[line:58] - INFO: Could not load library with AVX2 support due to:
ModuleNotFoundError("No module named 'faiss.swigfaiss_avx2'")
2023-12-06 11:42:08,537 - loader.py[line:64] - INFO: Loading faiss.
2023-12-06 11:42:08,946 - loader.py[line:66] - INFO: Successfully loaded faiss.
2023-12-06 11:42:09,301 - faiss_cache.py[line:80] - INFO: loading vector store in 'samples/vector_store/bge-large-zh' from disk.
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  6.13it/s]

正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\llm\img\大模型指令对齐训练原理-幕布图片-350029-666381.jpg 添加到向量库,共包含2条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:02<00:00,  2.01s/it]
正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\llm\大模型应用技术原理.md 添加到向量库,共包含7条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:07<00:00,  7.84s/it]
正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\llm\分布式训练技术原理.md 添加到向量库,共包含11条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:18<00:00, 18.49s/it]
正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\llm\img\大模型推理优化策略-幕布图片-699343-219844.jpg 添加到向量库,共包含1条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  5.38it/s]
正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\llm\大模型技术栈-实战与应用.md 添加到向量库,共包含2条文档

正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\test_files\langchain-ChatGLM_open.csv 添加到向量库,共包含323条文档
Batches:  45%|███████████████████████████████████████████████████████████████████████████████████▋                                                                                                    | 5/11 [02:00<02:08, 21.40s/it]

正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\test_files\langchain-ChatGLM_closed.xlsx 添加到向量库,共包含86条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [01:35<00:00, 31.86s/it]
正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\test_files\langchain-ChatGLM_open.xlsx 添加到向量库,共包含126条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [02:14<00:00, 33.64s/it]
正在将 samples/F:\GitHub\Langchain-Chatchat\knowledge_base\samples\content\test_files\langchain.pdf 添加到向量库,共包含52条文档
Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:56<00:00, 28.44s/it]
2023-12-06 11:55:42,220 - faiss_cache.py[line:24] - INFO: 已将向量库 ('samples', 'bge-large-zh') 保存到磁盘
总计用时: 0:24:15.137100

注意:

Windows环境下,会默认自动将该模型下载到C:\Users\Admin\.cache\torch\sentence_transformers目录下
也可以提前下载THUDM/chatglm2-6b与moka-ai/m3e-base到本地,然后在model_config.py中配置,具体参考异常3中的处理办法

启动项目

启动项目整个过程中,坑也比较多,参考异常2与异常3中的处理办法。

python 复制代码
python startup.py -a   
python 复制代码
2023-12-06 12:20:03,661 - startup.py[line:652] - INFO: 正在启动服务:
2023-12-06 12:20:03,661 - startup.py[line:653] - INFO: 如需查看 llm_api 日志,请前往 F:\GitHub\Langchain-Chatchat\logs
2023-12-06 12:20:16 | INFO | model_worker | Register to controller
2023-12-06 12:20:16 | ERROR | stderr | INFO:     Started server process [7000]
2023-12-06 12:20:16 | ERROR | stderr | INFO:     Waiting for application startup.
2023-12-06 12:20:16 | ERROR | stderr | INFO:     Application startup complete.
2023-12-06 12:20:16 | ERROR | stderr | INFO:     Uvicorn running on http://127.0.0.1:20000 (Press CTRL+C to quit)
2023-12-06 12:20:18 | INFO | model_worker | Loading the model ['chatglm3-6b'] on worker 33b56f2a ...
Loading checkpoint shards:   0%|                                                                                                                                                                               | 0/7 [00:00<?, ?it/s]
Loading checkpoint shards:  14%|███████████████████████▊                                                                                                                                               | 1/7 [00:20<02:02, 20.42s/it]
Loading checkpoint shards:  29%|███████████████████████████████████████████████▋                                                                                                                       | 2/7 [00:44<01:51, 22.35s/it]
Loading checkpoint shards:  43%|███████████████████████████████████████████████████████████████████████▌                                                                                               | 3/7 [01:08<01:33, 23.26s/it]
Loading checkpoint shards:  57%|███████████████████████████████████████████████████████████████████████████████████████████████▍                                                                       | 4/7 [01:41<01:21, 27.07s/it]
Loading checkpoint shards:  71%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎                                               | 5/7 [02:10<00:55, 27.80s/it]
Loading checkpoint shards:  86%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏                       | 6/7 [02:34<00:26, 26.42s/it]
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [02:46<00:00, 21.66s/it]
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [02:46<00:00, 23.73s/it]

启动API服务

创建新终端,进入虚拟环境,启动API服务:

python 复制代码
python server/api.py

访问:http://0.0.0.0:7861/docs

启动Web UI服务

最后启动Web UI服务,创建新终端,进入虚拟环境,

python 复制代码
streamlit run webui.py

访问:http://localhost:8501/

使用

对话功能

可以选择本地模型,线上模型,然后进行对话

知识库管理

可以新建知识库,上传相关资料信息 上传后会建立向量数据库 进行提问,回答基于知识库进行回答

文件对话

上传一个报销制度文件,然后提问

搜索引擎问答

异常集合

异常1

场景:

初始化配置文件

python 复制代码
python init_database.py --recreate-vs

问题:

python 复制代码
 cannot import name 'Doc' from 'typing_extensions'

解决:

因为安装的typing_extensions版本不正确,需要重新安装

python 复制代码
pip install typing_extensions==4.8.0

异常2

场景:

启动项目

python 复制代码
python startup.py -a

问题:

python 复制代码
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a singl
e OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE t
o allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

解决1:

这里使用Anaconda创建虚拟环境,其中有ibiomp5md.dll文件,重命名以备份该文件,如:libiomp5md.dll.back

解决2:

startup.py文件上方设置环境变量,保证前后顺序

python 复制代码
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'

异常3

场景:

启动项目过程中

python 复制代码
python startup.py -a

问题:

python 复制代码
| OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like THUDM/chatglm3-6b is not the path to a directory containing 
a file named config.json.

解决:

默认使用的LLM 模型 THUDM/ChatGLM3-6B 与 Embedding 模型BAAI/bge-large-zh,会远程连接模型网站。这里使用魔法也不得行,不知为啥,具体模型网站能访问的。

下载LLM 模型THUDM/ChatGLM3-6B与Embedding模型BAAI/bge-large-zh

1.访问https://huggingface.co/BAAI/bge-large-zh下载 2.访问https://huggingface.co/THUDM/chatglm3-6b下载

然后修改configs/model_config.py文件,指定模型存放位置与使用模型名称,需保证存放模型目录下的模型文件名与model_config.py文件使用的名称一致。

python 复制代码
# 统一存放模型位置
MODEL_ROOT_PATH = "../../../models"

# 选用的 Embedding 名称
EMBEDDING_MODEL = "bge-large-zh"

# 要运行的LLM名称,可以包括本地模型和在线模型
LLM_MODELS = ["chatglm3-6b", "zhipu-api", "openai-api"]

MODEL_PATH = {
    "embed_model": {
        "ernie-tiny": "nghuyong/ernie-3.0-nano-zh",
        "ernie-base": "nghuyong/ernie-3.0-base-zh",
        "text2vec-base": "shibing624/text2vec-base-chinese",
        "text2vec": "GanymedeNil/text2vec-large-chinese",
        "text2vec-paraphrase": "shibing624/text2vec-base-chinese-paraphrase",
        "text2vec-sentence": "shibing624/text2vec-base-chinese-sentence",
        "text2vec-multilingual": "shibing624/text2vec-base-multilingual",
        "text2vec-bge-large-chinese": "shibing624/text2vec-bge-large-chinese",
        "m3e-small": "moka-ai/m3e-small",
        "m3e-base": "moka-ai/m3e-base",
        "m3e-large": "moka-ai/m3e-large",
        "bge-small-zh": "BAAI/bge-small-zh",
        "bge-base-zh": "BAAI/bge-base-zh",
         # "bge-large-zh": "BAAI/bge-large-zh",
         # 如果模型目录名称和 MODEL_PATH 中的 key 或 value 相同,程序会自动检测加载,无需修改 MODEL_PATH 中的路径。
        "bge-large-zh": "bge-large-zh",
        "bge-large-zh-noinstruct": "BAAI/bge-large-zh-noinstruct",
        "bge-base-zh-v1.5": "BAAI/bge-base-zh-v1.5",
        "bge-large-zh-v1.5": "BAAI/bge-large-zh-v1.5",
        "piccolo-base-zh": "sensenova/piccolo-base-zh",
        "piccolo-large-zh": "sensenova/piccolo-large-zh",
        "nlp_gte_sentence-embedding_chinese-large": "damo/nlp_gte_sentence-embedding_chinese-large",
        "text-embedding-ada-002": "your OPENAI_API_KEY",
    },

    "llm_model": {
        # 以下部分模型并未完全测试,仅根据fastchat和vllm模型的模型列表推定支持 
        # "chatglm2-6b": "THUDM/chatglm2-6b",
        # 如果模型目录名称和 MODEL_PATH 中的 key 或 value 相同,程序会自动检测加载,无需修改 MODEL_PATH 中的路径。
        "chatglm2-6b": "chatglm2-6b",
        "chatglm2-6b-32k": "THUDM/chatglm2-6b-32k",

        "chatglm3-6b": "THUDM/chatglm3-6b",
        "chatglm3-6b-32k": "THUDM/chatglm3-6b-32k",
        "chatglm3-6b-base": "THUDM/chatglm3-6b-base",
    },
相关推荐
huanxiangcoco11 分钟前
207. 课程表
python·leetcode·广度优先
matrixlzp13 分钟前
Python Selenium 自动化爬虫 + Charles Proxy 抓包
爬虫·python·selenium·自动化
子午15 分钟前
水果识别系统Python+卷积神经网络算法+人工智能+深度学习+计算机课设项目+TensorFlow+模型训练
人工智能·python·深度学习
卑微的Coder17 分钟前
python画正方形、平行四边形、六边形、五角星、风车(四个半圆)
开发语言·python
一只会敲代码的小灰灰24 分钟前
python学习第十节:爬虫基于requests库的方法
爬虫·python·学习
Adolf_199329 分钟前
Flask-JWT-Extended登录验证
后端·python·flask
William数据分析1 小时前
[Python]案例驱动最佳入门:Python数据可视化在气候研究中的应用
python·信息可视化·数据
唤醒手腕1 小时前
2024年最新 Python 大数据网络爬虫技术基础案例详细教程(更新中)
开发语言·爬虫·python
zhangbin_2371 小时前
【Python机器学习】NLP信息提取——正则模式
开发语言·人工智能·python·深度学习·机器学习·自然语言处理
小李飞刀李寻欢1 小时前
mongoDB 读取数据python版本实现
数据库·python·mongodb·数据