一、介绍
- Baichuan 2 是百川智能推出的 新一代开源大语言模型 ,采用 2.6 万亿 Tokens 的高质量语料训练。
- Baichuan 2 在一系列权威的中文、英文和多语言的通用、领域 benchmark 上达到了同尺寸最佳的效果。
- 涵盖了 7B 、13B 的 Base 和 Chat 版本,并提供了 Chat 版本的 4bits 量化 。
更多详细信息见 Github仓库
二、容器构建说明
BaiChuan2 项目自带了 Web 交互界面,并在项目运行中自动下载所需的模型参数。因此,克隆项目后,安装好项目所需环境,然后直接运行即可,下面将以Baichuan2-13B-Chat模型进行部署,由于模型较大建议使用RTX3090及以上显卡。
1. 安装 miniconda
进入 Anaconda 官网:
找到快速命令行安装,选择 Linux 系统,复制代码进入终端进行安装。
等待安装完成。
2. 安装并更新基础命令,让后续使用更快捷: unzip、lsof、git、git-lfs
apt-get update
apt-get install -y unzip
apt-get install -y lsof
apt-get install -y git
apt-get install -y git-lfs
3. 克隆项目仓库,并打开文件
git clone https://github.com/baichuan-inc/Baichuan2.git
cd Baichuan2
4. 创建python为3.11版本的虚拟环境,名称为Baichuan2
conda create -n Baichuan2 python=3.11
5. 激活虚拟环境Baichuan2
conda activate Baichuan2
6. 通过pip安装所需的torch环境(国内镜像源快一些,以下使用了清华源)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121 -i https://pypi.tuna.tsinghua.edu.cn/simple
7. 安装项目依赖文件
为了安装速度更快可以将 pip 进行永久换源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt
8. 指定端口,运行py文件(运行项目,首次运行会自动下载模型参数,需要保证网络通畅)
streamlit run --server.address 0.0.0.0 --server.port 8080 web_demo.py
出现以下页面即为运行成功(使用了RTX3090)
若使用RTX3060则会出现以下运行不成功页面
9. 注意
RTX3090在运行Baichuan2-13B-Chat模型时仅仅只是运行成功,文字生成的速度大概是一秒钟两个字左右,若想要速度更快,则需要更换更加轻量化的模型,可以使用在线量化,以下用Baichuan2-13B-Chat模型的4bits量化模型作为示例
打开web_demo.py文件,替换相应代码
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(4).cuda()
重新运行后生成速度每秒可达到十多字!
三、网页演示
运行成功后获取访问链接,进入 webUI 界面后即可操作: