清华ChatGLM语言模型开源项目地址:https://github.com/THUDM/ChatGLM-6B
运行环境:Windows + Virtualbox 虚拟机 + Ubuntu 20.04 LTS
Python版本:Python 3.10+, 我测试在低版本的3.7下面安装依赖库会报错,然后3.10 和3.11都正常
硬件要求:虚拟机的闲置内存在6G左右,然后把虚拟机的CPU核数拉满
注意:以上的Ubuntu 20.04 LTS和Python 3.10+的安装我就省略了哈~
不废话了,直接上部署步骤:
安装一下系统依赖:
1 2 |
sudo apt install cmake |
安装python虚拟环境并激活虚拟环境:
1 2 |
python3 -m venv ChatGLM && source ChatGLM/bin/activate |
安装python依赖库:
1 2 |
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 'protobuf>=3.19.5,<3.20.1' 'transformers==4.26.1' icetk cpm_kernels 'torch>=1.10' streamlit streamlit-chat |
随便找个剩余空间大于5个G左右的磁盘的任意目录下建立ChatGLM的运行目录,以及量化好的语言模型的目录并下载模型和模型的配置文件代码等等,完整命令如下:
1 2 |
mkdir -p ChatGLM/model && cd ChatGLM/model && wget --no-check-certificate 'https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/pytorch_model.bin' && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/config.json && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/configuration_chatglm.py && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/ice_text.model && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/modeling_chatglm.py && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/quantization.py && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/quantization_kernels.c && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/quantization_kernels_parallel.c && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/tokenization_chatglm.py && wget --no-check-certificate https://huggingface.co/THUDM/chatglm-6b-int4/resolve/main/tokenizer_config.json && cd .. |
以上文件中最核心的就是量化好的模型文件pytorch_model.bin
然后就是将以下的代码复制粘贴到一个叫ChatGLM-cli.py的代码文件中,并放置在上面建立好的ChatGLM目录下面,不要放到model下面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import os import platform from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("./model", trust_remote_code=True) model = AutoModel.from_pretrained("./model", trust_remote_code=True).float() model = model.eval() os_name = platform.system() clear_command = 'cls' if os_name == 'Windows' else 'clear' def build_prompt(history): prompt = "欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序" for query, response in history: prompt += f"\n\n用户:{query}" prompt += f"\n\nChatGLM-6B:{response}" return prompt def main(): history = [] print("欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序") while True: query = input("\n用户:") if query == "stop": break if query == "clear": history = [] os.system(clear_command) print("欢迎使用 ChatGLM-6B 模型,输入内容即可进行对话,clear 清空对话历史,stop 终止程序") continue count = 0 for response, history in model.stream_chat(tokenizer, query, history=history): count += 1 if count % 8 == 0: os.system(clear_command) print(build_prompt(history), flush=True) os.system(clear_command) print(build_prompt(history), flush=True) if __name__ == "__main__": main() |
声明:以上代码非本人原创而是来自于官方项目中的https://raw.githubusercontent.com/THUDM/ChatGLM-6B/main/cli_demo.py ,我根据本文的实际情况以及标题中的最小成本部署参考官方文档进行了小修改,方便大家一次运行成功。
再然后就是将以下的代码复制粘贴到一个叫ChatGLM-web.py的代码文件中,并放置在上面建立好的ChatGLM目录下面,不要放到model下面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
from transformers import AutoModel, AutoTokenizer import streamlit as st from streamlit_chat import message st.set_page_config( page_title="ChatGLM-6b 演示", page_icon=":robot:" ) @st.cache_resource def get_model(): tokenizer = AutoTokenizer.from_pretrained("./model", trust_remote_code=True) model = AutoModel.from_pretrained("./model", trust_remote_code=True).float() model = model.eval() return tokenizer, model MAX_TURNS = 20 MAX_BOXES = MAX_TURNS * 2 def predict(input, history=None): tokenizer, model = get_model() if history is None: history = [] with container: if len(history) > 0: for i, (query, response) in enumerate(history): message(query, avatar_style="big-smile", key=str(i) + "_user") message(response, avatar_style="bottts", key=str(i)) message(input, avatar_style="big-smile", key=str(len(history)) + "_user") st.write("AI正在回复:") with st.empty(): for response, history in model.stream_chat(tokenizer, input, history): query, response = history[-1] st.write(response) return history container = st.container() # create a prompt text for the text generation prompt_text = st.text_area(label="用户命令输入", height = 100, placeholder="请在这儿输入您的命令") if 'state' not in st.session_state: st.session_state['state'] = [] if st.button("发送", key="predict"): with st.spinner("AI正在思考,请稍等........"): # text generation st.session_state["state"] = predict(prompt_text, st.session_state["state"]) |
声明:以上代码非本人原创而是来自于官方项目中的https://raw.githubusercontent.com/THUDM/ChatGLM-6B/main/web_demo2.py ,我根据本文的实际情况以及标题中的最小成本部署参考官方文档进行了小修改,方便大家一次运行成功。
最后大家参考下面的截图中的目录树,要保证和我的一致就行了,如下:
最后就是开始运行我们的cli命令行和web图形界面的demo展示了:
cli命令行demo的运行命令如下:
1 2 |
python ChatGLM-cli.py |
效果如下:
web图形界面的demo的运行命令如下:
1 2 |
streamlit run ChatGLM-web.py.py --server.port 8080 |
最后打开浏览器输入你的虚拟机的ip地址加8080端口组成的url就可以看到实际效果了,比如我的url是:http://192.168.56.2:8080
,最终效果如下:
声明:因为本文旨在让大家以最小的成本感受一下清华ChatGLM语言模型的魅力,但是也导致了以上无论是cli还是web的demo在回答你的问题的时候返回结果的速度极其缓慢,如需追求更快的回答速度请参考官方项目中的文档修改相应的代码并加大你的帶寬! 加大你的內存! 加大你的顯示器!
我会评论啥啊,我就是个打酱油的
@大佬您好,我是机器人 卧槽大佬你这是真人还是机器人呀,难道我的cf验证码没生效么~
@World 这种纯cpu方案大家都可以试着部署一下哈~
大佬👍👍👍👍👍👍
@领导,我小江 不敢当哈~