ChatGLM2安装
ChatGLM2安装
代码下载
下载指定commit的代码,当前教程基于当前commit编写。
1 | git clone https://github.com/THUDM/ChatGLM2-6B |
模型下载
1 | set GIT_LFS_SKIP_SMUDGE=1 |
安装环境
1 | cd ChatGLM-6B |
requirements-smile.txt
1 | accelerate==0.28.0 |
模型路径修改
将代码中的模型路径改为自己的模型路径
1 | tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True) |
改为
1 | tokenizer = AutoTokenizer.from_pretrained("I:\AI\chatglm2-6b", trust_remote_code=True) |
显存适配
根据自己的显存修改运行脚本
量化等级 | 编码 2048 长度的最小显存 | 生成 8192 长度的最小显存 |
---|---|---|
FP16 / BF16 | 13.1 GB | 12.8 GB |
INT8 | 8.2 GB | 8.1 GB |
INT4 | 5.5 GB | 5.1 GB |
- FP16 / BF16
1
2tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).cuda() - INT8
1
2tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).quantize(8).half().cuda() - INT4
1
2tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).quantize(4).half().cuda()使用
网页版
1
python web_demo.py
打开网址 http://127.0.0.1:7860
就可以进行聊天了
StreamLit网页
1 | streamlit run web_demo2.py |
API部署
1 | python api.py |
发送请求
1
2
3curl -X POST "http://127.0.0.1:8000" \
-H 'Content-Type: application/json' \
-d '{"prompt": "你好", "history": []}'回复结果
1
{"response":"你好👋!我是人工智能助手 ChatGLM2-6B,很高兴见到你,欢迎问我任何问题。","history":[["你好","你好👋!我是人工智能助手 ChatGLM2-6B,很高兴见到你,欢迎问我任何问题。"]],"status":200,"time":"2024-03-21 12:01:12"}
OpenAI API部署
1 | python openai_api.py |
- 发送请求
1
2
3
4
5
6
7
8
9
10
11
12
13import openai
if __name__ == "__main__":
openai.api_base = "http://localhost:8000/v1"
openai.api_key = "none"
for chunk in openai.ChatCompletion.create(
model="chatglm2-6b",
messages=[
{"role": "user", "content": "你好"}
],
stream=True
):
if hasattr(chunk.choices[0].delta, "content"):
print(chunk.choices[0].delta.content, end="", flush=True)
![]() |
![]() |
签名:Smile every day
名字:宏沉一笑
邮箱:whghcyx@outlook.com
个人网站:https://whg555.github.io
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 whghcyx@outlook.com
文章标题:ChatGLM2安装
文章字数:1.2k
本文作者:宏沉一笑
发布时间:2024-03-20, 23:26:48
最后更新:2024-03-21, 12:55:32
原始链接:https://whghcyx.gitee.io/2024/03/20/AI-%E6%A1%86%E6%9E%B6-2024-03-20-ChatGLM2%E5%AE%89%E8%A3%85/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。