当前位置:   article > 正文

昇思MindSpore学习笔记2-05LLM原理和实践--MindNLP ChatGLM-6B StreamChat_chatglm2 mindnlp

chatglm2 mindnlp

摘要:

        记录基于MindNLP和ChatGLM-6B实现聊天应用实验的过程和步骤。

一、环境配置

安装要用的包和模块mindspore mindnlp mdtex2html

  1. %%capture captured_output
  2. # 实验环境已经预装了mindspore==2.2.14,如需更换mindspore版本,可更改下面mindspore的版本号
  3. !pip uninstall mindspore -y
  4. !pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindspore==2.2.14
  5. !export HF_ENDPOINT=https://hf-mirror.com
  6. !pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindnlp mdtex2html

输出:

  1. Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple
  2. Requirement already satisfied: mindnlp in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (0.3.1)
  3. Collecting mdtex2html
  4. Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/ff/e8/c5fab9aa5d9254ad7c7e37d33a3c32fd49d82b4c6b54da337bbca378eb5c/mdtex2html-1.3.0-py3-none-any.whl (13 kB)
  5. Requirement already satisfied: mindspore in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (2.2.14)
  6. Requirement already satisfied: tqdm in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (4.66.4)
  7. ......
  8. Collecting markdown (from mdtex2html)
  9. Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/fc/b3/0c0c994fe49cd661084f8d5dc06562af53818cc0abefaca35bdc894577c3/Markdown-3.6-py3-none-any.whl (105 kB)
  10. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 105.4/105.4 kB 11.4 MB/s eta 0:00:00
  11. Collecting latex2mathml (from mdtex2html)
  12. Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/f2/0a/181ed55562ce90179aedf33b09fcd79db31c868a5d480f3cb71a31d19692/latex2mathml-3.77.0-py3-none-any.whl (73 kB)
  13. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 73.7/73.7 kB 24.0 MB/s eta 0:00:00
  14. Requirement already satisfied: filelock in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (3.15.3)
  15. ......
  16. Requirement already satisfied: tzdata>=2022.7 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pandas->datasets->mindnlp) (2024.1)
  17. Installing collected packages: latex2mathml, markdown, mdtex2html
  18. Successfully installed latex2mathml-3.77.0 markdown-3.6 mdtex2html-1.3.0
  19. [notice] A new release of pip is available: 24.1 -> 24.1.2[notice] To update, run: python -m pip install --upgrade pip
  20. Note: you may need to restart the kernel to use updated packages.

二、配置网络

1.下载权重模型

  1. from mindnlp.transformers import AutoModelForSeq2SeqLM, AutoTokenizer
  2. import gradio as gr
  3. import mdtex2html
  4. model = AutoModelForSeq2SeqLM.from_pretrained('ZhipuAI/ChatGLM-6B', mirror="modelscope").half()
  5. model.set_train(False)
  6. tokenizer = AutoTokenizer.from_pretrained('ZhipuAI/ChatGLM-6B', mirror="modelscope")

输出:

  1. 100%━━━━━━━━━━ 773/773 [00:00<00:00, 51.7kB/s]
  2. 100%━━━━━━━━━━ 32.6k/32.6k [00:00<00:00, 2.75MB/s]
  3. Downloading shards: 100%
  4. 8/8 [16:38<00:00, 106.84s/it]
  5. 100%━━━━━━━━━━ 1.62G/1.62G [02:09<00:00, 13.6MB/s]
  6. 100%━━━━━━━━━━ 1.75G/1.75G [02:25<00:00, 7.59MB/s]
  7. 100%━━━━━━━━━━ 1.84G/1.84G [02:26<00:00, 16.6MB/s]
  8. 100%━━━━━━━━━━ 1.78G/1.78G [02:20<00:00, 15.5MB/s]
  9. 100%━━━━━━━━━━ 1.75G/1.75G [02:20<00:00, 10.8MB/s]
  10. 100%━━━━━━━━━━ 1.75G/1.75G [02:18<00:00, 12.0MB/s]
  11. 100%━━━━━━━━━━ 1.00G/1.00G [01:18<00:00, 21.7MB/s]
  12. 100%━━━━━━━━━━ 1.00G/1.00G [01:17<00:00, 14.1MB/s]
  13. Loading checkpoint shards: 100%
  14. 8/8 [00:46<00:00,  5.13s/it]
  15. 100%━━━━━━━━━━ 441/441 [00:00<00:00, 21.6kB/s]
  16. 100%━━━━━━━━━━ 2.58M/2.58M [00:00<00:00, 21.2MB/s]

2.体验模型

  1. prompt = '你好'
  2. history = []
  3. response, _ = model.chat(tokenizer, prompt, history=history, max_length=20)
  4. response

输出:

'你好
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/852531
推荐阅读
相关标签