昇思25天学习打卡营第28天 | MindNLP ChatGLM-6B StreamChat

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

配置网络线路

!export HF_ENDPOINT=https://hf-mirror.com

2 代码开发

下载权重

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")

可以修改下列参数和prompt体验模型

prompt = '你好'
history = []
response, _ = model.chat(tokenizer, prompt, history=history, max_length=20)
response
prompt = '我想要一篇跟月亮有关的作文'
history = []
response, _ = model.chat(tokenizer, prompt, history=history, max_length=20)
response

代码实现: