本地部署 Langchain-Chatchat & ChatGLM

发布时间 2023-09-26 18:28:08作者: 七夜i

一、模型&环境介绍

1. ChatGLM

2. m3e

3. text2vec

4. Langchain-Chatchat

5. Python (建议Python 版本 3.8.1 - 3.10)

6. torch

7. conda

二、安装

作者本人电脑环境是:
  • Windows 10
  • Intel 处理器
  • N卡 3060Ti 12G

1. 建议先安装 conda (也可以不使用 conda )

2. 安装后为 conda 设置清华源

  • 长期切换通道,推荐生成.condarc文件后手动编辑文件内容,而不是通过命令行一个一个添加channel
  • .condarc文件默认不生成,运行一下命令就可以在用户目录下生成:conda config –set show_channel_urls yes
  • 在.condarc文件中复制以下内容(找不到该文件的可以conda info看看"user config file"的路径)
# This is a sample .condarc file.
# It adds mirror-channel(Tsinghua University) of anaconda and enables
# the show_channel_urls option.

# channel locations. These override conda defaults, i.e., conda will
# search *only* the channels listed here, in the order given.
# Use "defaults" to automatically include all default channels.
# Non-url channels will be interpreted as Anaconda.org usernames
# (this can be changed by modifying the channel_alias key; see below).
# The default is just 'defaults'.
channels:
  - defaults
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

# Show channel URLs when displaying what is going to be downloaded
# and in 'conda list'. The default is False.
show_channel_urls: true

# For more information about this file see:
# https://conda.io/docs/user-guide/configuration/use-condarc.html
  • 然后conda clean -i 清除索引缓存

3. git 和 git lfs 安装

  • 确保机器安装了 git 环境:https://git-scm.com/
  • 因为模型过大,需要安装 lfs 支持: git lfs install

4. 下载 ChatGLM2-6B 模型到本地,要根据自己的显卡显存下载合适的模型,比如作者本人需要下载 chatglm2-6b-32k-int4 或 int8(若显存小于13G)

git clone https://huggingface.co/THUDM/chatglm2-6b-32k /your_path/chatglm2-6b

注意:由于模型较大,下载经常会失败,需要根据 git 提示,重试多次。

5. 下载 m3e 或 text2vec 模型到本地

git clone https://huggingface.co/moka-ai/m3e-base /your_path/m3e

6. 下载并配置 Langchain-Chatchat

  • 下载 Langchain-Chatchat 到本地:https://github.com/chatchat-space/Langchain-Chatchat/tags ,建议下载稳定 tag 版本,要不然一堆坑。
  • git clone 或是下载 zip 解压到本地
  • cd 到 Langchain-Chatchat 目录下,使用清华源安装:pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
  • 默认安装的 torch 是 cpu 版本,我们需要安装 gpu 版本,否则模型在你 cpu 上跑,卡死。
    可以通过 pip list 查看 torch 的安装版本,或者在 python 环境通过以下命令判断:
import torch
print(torch.__version__)  # 2.0.1+cu117 ,带 +cu117 类似格式的,表示装的 gpu 版本 ,不带这种格式的默认是 cpu 版本
print(torch.cuda.is_available())  # True  表示支持 gpu ,False 的话将使用 cpu
  • 安装好显卡驱动, 安装 cuda 支持
  • 由于网络的关系,建议去 https://pytorch.org/ 官网下载 gpu 版本的 torch .whl,然后本地使用 pip install
    安装好通过上述的两种方式检查 torch 是否支持 gpu
  • 上述步骤搞定好,开始 Langchain-Chatchat 的配置
    a. 将chatglm 和 m3e 配置到 model_config.py
    b. 修改 server_config.py 配置
  • 在 Langchain-Chatchat 目录下,调用命令初始化本地数据库:python init_database.py --recreate-vs
  • 使用 python startup.py --all-webui 启动 web ui 页面

三、自定义知识库

。。。