ChatGLM-6B LLM大模型使用

ChatGLM-6B 是一个开源的、支持中英双语问答的对话语言模型,基于 General Language Model (GLM) 架构,具有 62 亿参数。

本机显卡只有6G(GTX 1660 Ti),所以刚好可以使用,用户可以在消费级的显卡上进行本地部署(INT4 量化级别下最低只需 6GB 显存),可以去下面参考链接下载对应模型

参考:https://huggingface.co/THUDM/chatglm-6b-int4
https://github.com/THUDM/ChatGLM-6B

安装环境

1、安装对应cuda 版本的torch包

ChatGLM-6B LLM大模型使用_第1张图片
** CUDA 11.3

pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113

2、安装transformers等包

pip install protobuf transformers==4.27.1 cpm_kernels sentencepiece

使用

运算推理还是比较慢,长的内容需要3分钟多

from transformers import AutoTokenizer, AutoModel

## 在线加载下载模型很慢,所以这边离线下载本地加载
##模型下载地址:https://cloud.tsinghua.edu.cn/d/674208019e314311ab5c/


tokenizer = AutoTokenizer.from_pretrained(r"C:\Users\lonng\Downloads\chatglm-6b-int4", trust_remote_code=True)
model = AutoModel.from_pretrained(r"C:\Users\lonng\Downloads\chatglm-6b-int4", trust_remote_code=True).half().cuda()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
history

response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

chatglm-6b-int4 模型bin文件3个多G
ChatGLM-6B LLM大模型使用_第2张图片

ChatGLM-6B LLM大模型使用_第3张图片

response, history = model.chat(tokenizer, "单细胞测序分析方法", history=history)

ChatGLM-6B LLM大模型使用_第4张图片
ChatGLM-6B LLM大模型使用_第5张图片

你可能感兴趣的:(深度学习,人工智能)