MindNLP ChatGLM-6B StreamChat
本案例基于MindNLP和ChatGLM-6B实现一个聊天应用。
1 环境配置
%%capture captured_output
# 实验环境已经预装了mindspore==2.2.14,如需更换mindspore版本,可更改下面mindspore的版本号
!pip uninstall mindspore -y
!pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindspore==2.2.14
!pip install mindnlp mdtex2html
配置网络线路
!export HF_ENDPOINT=https://hf-mirror.com
2 代码开发
下载权重大约需要10分钟
from mindnlp.transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import gradio as gr
import mdtex2html
model = AutoModelForSeq2SeqLM.from_pretrained('ZhipuAI/ChatGLM-6B', mirror="modelscope").half()
model.set_train(False)
tokenizer = AutoTokenizer.from_pretrained('ZhipuAI/ChatGLM-6B', mirror="modelscope")
Building prefix dict from the default dictionary ...
Loading model from cache /tmp/jieba.cache
Loading model cost 1.029 seconds.
Prefix dict has been built successfully.
Loading checkpoint shards: 0%| | 0/8 [00:00<?, ?it/s]
0%| | 0.00/441 [00:00<?, ?B/s]
0%| | 0.00/2.58M [00:00<?, ?B/s]
可以修改下列参数和prompt体验模型
prompt = '你好'
history = []
response, _ = model.chat(tokenizer, prompt, history=history, max_length=20)
response
-
The dtype of attention mask (Float32) is not bool
|
'你好👋!我是人工智能助手 ChatGLM-6B'