利用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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章