心得
特别注意:
cell 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
!pip install mdtex2html
否则会报错
添加代码后正常。
打卡截图:
MindNLP ChatGLM-6B StreamChat
本案例基于MindNLP和ChatGLM-6B实现一个聊天应用。
1 环境配置
[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
!pip install mdtex2html
配置网络线路
[2]:
!export HF_ENDPOINT=https://hf-mirror.com
2 代码开发
下载权重大约需要10分钟
[3]:
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 ... Dumping model to file cache /tmp/jieba.cache Loading model cost 1.034 seconds. Prefix dict has been built successfully.
100%
773/773 [00:00<00:00, 53.8kB/s]
100%
32.6k/32.6k [00:00<00:00, 2.95MB/s]
Downloading shards: 100%
8/8 [16:37<00:00, 107.48s/it]
100%
1.62G/1.62G [02:07<00:00, 14.4MB/s]
100%
1.75G/1.75G [02:19<00:00, 10.2MB/s]
100%
1.84G/1.84G [02:27<00:00, 20.0MB/s]
100%
1.78G/1.78G [02:22<00:00, 10.4MB/s]
100%
1.75G/1.75G [02:23<00:00, 12.3MB/s]
100%
1.75G/1.75G [02:18<00:00, 14.5MB/s]
100%
1.00G/1.00G [01:18<00:00, 18.0MB/s]
100%
1.00G/1.00G [01:19<00:00, 12.8MB/s]
Loading checkpoint shards: 100%
8/8 [00:44<00:00, 4.84s/it]
100%
441/441 [00:00<00:00, 43.9kB/s]
100%
2.58M/2.58M [00:00<00:00, 20.4MB/s]
可以修改下列参数和prompt体验模型
[5]:
prompt = '你好'
history = []
response, _ = model.chat(tokenizer, prompt, history=history, max_length=20)
response
[5]:
'你好👋!我是人工智能助手 ChatGLM-6B'
[6]:
你好
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 你好 NameError: name '你好' is not defined
[7]:
prompt = '你好'
[8]:
prompt = '今天好热'
history = []
response, _ = model.chat(tokenizer, prompt, history=history, max_length=20)
response
-
[8]:
'是的,今天天气很热。夏季通常是气温较高、阳光强烈的季节'
[9]:
import time
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'guojun0718')
2024-07-18 08:44:17 guojun0718
[ ]: