利用text-generation-webui快速搭建chatGLM2/LLAMA2大模型运行环境

text-generation-webui  是一个基于Gradio的LLM Web UI开源项目,可以利用其快速搭建各种文本生成的大模型环境。

一、安装

text-generation-webui的readme其实已写得相当详细了,这里就不再重复,只说1个可能存在的坑:

安装 peft 安装卡住

requirements.txt 中有一些依赖项,需要访问github网站,国内经常会打不开,看看这个文件的内容:

aiofiles==23.1.0
fastapi==0.95.2
gradio_client==0.2.5
gradio==3.33.1

accelerate==0.21.0
colorama
datasets
einops
markdown
numpy
pandas
Pillow>=9.5.0
pyyaml
requests
safetensors==0.3.1
scipy
sentencepiece
tensorboard
tqdm
wandb

git+https://github.com/huggingface/peft@4b371b489b9850fd583f204cdf8b5471e098d4e4
git+https://github.com/huggingface/transformers@baf1daa58eb2960248fd9f7c3af0ed245b8ce4af

bitsandbytes==0.41.1; platform_system != "Windows"
https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.41.1-py3-none-win_amd64.whl; platform_system == "Windows"
https://github.com/PanQiWei/AutoGPTQ/releases/download/v0.4.1/auto_gptq-0.4.1+cu117-cp310-cp310-win_amd64.whl; platform_system == "Windows"
https://github.com/PanQiWei/AutoGPTQ/releases/download/v0.4.1/auto_gptq-0.4.1+cu117-cp310-cp310-linux_x86_64.whl; platform_system == "Linux" and platform_machine == "x86_64"
https://github.com/jllllll/exllama/releases/download/0.0.10/exllama-0.0.10+cu117-cp310-cp310-win_amd64.whl; platform_system == "Windows"
https://github.com/jllllll/exllama/releases/download/0.0.10/exllama-0.0.10+cu117-cp310-cp310-linux_x86_64.whl; platform_system == "Linux" and platform_machine == "x86_64"

# llama-cpp-python without GPU support
llama-cpp-python==0.1.78; platform_system != "Windows"
https://github.com/abetlen/llama-cpp-python/releases/download/v0.1.78/llama_cpp_python-0.1.78-cp310-cp310-win_amd64.whl; platform_system == "Windows"
# llama-cpp-python with CUDA support
https://github.com/jllllll/llama-cpp-python-cuBLAS-wheels/releases/download/textgen-webui/llama_cpp_python_cuda-0.1.78+cu117-cp310-cp310-win_amd64.whl; platform_system == "Windows"
https://github.com/jllllll/llama-cpp-python-cuBLAS-wheels/releases/download/textgen-webui/llama_cpp_python_cuda-0.1.78+cu117-cp310-cp310-linux_x86_64.whl; platform_system == "Linux" and platform_machine == "x86_64"

# GPTQ-for-LLaMa
https://github.com/jllllll/GPTQ-for-LLaMa-CUDA/releases/download/0.1.0/gptq_for_llama-0.1.0+cu117-cp310-cp310-win_amd64.whl; platform_system == "Windows"
https://github.com/jllllll/GPTQ-for-LLaMa-CUDA/releases/download/0.1.0/gptq_for_llama-0.1.0+cu117-cp310-cp310-linux_x86_64.whl; platform_system == "Linux" and platform_machine == "x86_64"

# ctransformers
https://github.com/jllllll/ctransformers-cuBLAS-wheels/releases/download/AVX2/ctransformers-0.2.22+cu117-py3-none-any.whl

如果安装peft时,项目无法下载,可以把 这行注释掉。然后再开1个终端,直接用

pip install peft -i  https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers -i  https://pypi.tuna.tsinghua.edu.cn/simple
pip install accelerate -i  https://pypi.tuna.tsinghua.edu.cn/simple

走国内镜像安装,然后再回到原来的终端 pip install -r requirements.txt, 其它依赖项,也是这个思路。

 

二、启动及model下载

python server.py 即可启动,刚启动时没有任何模型,需要手动下载。

huggingface.co的模型,只要允许下载的,text-generation-webui都能下载, 不过这个速度比较慢,而且容易中断,我个人更建议手动下载(比如 git LFS工具,或者国内有一些镜像或网盘上,有热心网友上传了副本),如果是手动下载,只要把模型放到 text-generation-webui/models 目录下即可。

 

三、加载模型

3.1 Llam2 模型加载

这里选择 TheBloke/Llama-2-7B-Chat-GGML · Hugging Face 这个Llama2模型测试,选择后发现自动默认用了llama.cpp 这个c++版本的loader(注:c++版本的推理速度更快)

 然后就可以 体验了,建议先设置成chat模式:

 测试一下:

 可以发现,Llama2对中文支持一般般,默认情况下,用中文提问,回答却是英文。除非手动加上指令"[用中文回答]",而且不会作诗(这一点比不上国产的chatGLM)

 

3.2 ChatGLM2-6B

 text-generate-webui 对chatGLM的支持还不太完善,虽然能跑,但有些坑:

坑1:bitsandbytes 这个库在我机器上(windows 10 + WSL2 + ubuntu,LLM模型跑在WSL2上),必须把它给卸载(pip uninstall bitsandbytes) ,否则模型加载会失败

坑2:不能选量化(不管是4还是8均报错),还必须勾上trust-remote-code

另外:WSL2的内存设置,建议至少24G,不然加载模型时,可以因为内存不足会自动Killed(加载完成后过一会儿,内存好象又会略降一些),下图是我机器的实际内存占用

bitsandbytes与WSL2中的cuda不能很好兼容,就算跑在GPU上,感觉也有点卡,不如chatGLM.cpp顺畅 ,但不太影响小白学习

另外,如果有网友遇到model加载成功,但是测试过程中,AI总是不回答,可以尝试下,修改chatGLM2-6B目录中tokenization_chatglm.py,78行后,加一行代码:

self._eos_token='<eos>'

 

四、API模式

启用时,还可以增加参数--api,以同时启动api 

api-examples目录下,有相应的api调用示例: (仍然以模型Llama-2-7B-Chat-GGML 为例)

 api方式,似乎不识别[用中文回答]这类指令

这点跟web-UI上的差别有些大:

 

参考文章:

chatglm2 support · Issue #2906 · oobabooga/text-generation-webui (github.com)

posted @ 2023-08-19 17:51  菩提树下的杨过  阅读(2468)  评论(0编辑  收藏  举报