大模型实战营Day2 轻松玩转书生·浦语大模型趣味Demo:作业
基础作业(写在最前)
作业1
使用 InternLM-Chat-7B 模型生成 300 字的小故事(需截图)。
作业2
熟悉 hugging face 下载功能,使用 huggingface_hub python 包,下载 InternLM-20B 的 config.json 文件到本地(需截图下载过程)。
step1: 使用 Hugging Face 官方提供的 huggingface-cli 命令行工具。安装依赖:
pip install -U huggingface_hub
step2: 新建 python 文件,填入以下代码,运行即可。
import os
from huggingface_hub import hf_hub_download # Load model directly
hf_hub_download(repo_id="internlm/internlm-7b", filename="config.json")
进阶作业
完成浦语·灵笔的图文理解及创作部署
见详细作业。
完成 Lagent 工具调用 Demo 创作部署
见详细作业。
InternLM-Chat-7B智能对话Demo
1. 环境准备
在 InternStudio 平台中选择 A100(1/4) 的配置
打开刚刚租用服务器的进入开发机,并且打开其中的终端开始环境配置、模型下载和运行 demo。
进入开发机后,在页面的左上角可以切换 JupyterLab、终端和 VScode,并在终端输入 bash 命令,进入 conda 环境
进入 conda 环境之后,使用以下命令从本地克隆一个已有的 pytorch 2.0.1 的环境
bash # 请每次使用 jupyter lab 打开终端时务必先执行 bash 命令进入 bash 中
bash /root/share/install_conda_env_internlm_base.sh internlm-demo # 执行该脚本文件来安装项目实验环境
然后使用以下命令激活环境
conda activate internlm-demo
并在环境中安装运行 demo 所需要的依赖。
# 升级pip
python -m pip install --upgrade pip
pip install modelscope==1.9.5
pip install transformers==4.35.2
pip install streamlit==1.24.0
pip install sentencepiece==0.1.99
pip install accelerate==0.24.1
2. 模型下载
InternStudio 平台的 share 目录下已经为我们准备了全系列的 InternLM 模型,使用如下命令复制:
mkdir -p /root/model/Shanghai_AI_Laboratory
cp -r /root/share/temp/model_repos/internlm-chat-7b /root/model/Shanghai_AI_Laboratory
3. 代码准备
首先 clone 代码,在 /root
路径下新建 code
目录,然后切换路径, clone
代码。
cd /root/code
git clone https://gitee.com/internlm/InternLM.git
切换 commit 版本,与教程 commit 版本保持一致,可以让大家更好的复现。
cd InternLM
git checkout 3028f07cb79e5b1d7342f4ad8d11efad3fd13d17
将 /root/code/InternLM/web_demo.py
中 29 行和 33 行的模型更换为本地的 /root/model/Shanghai_AI_Laboratory/internlm-chat-7b
。
4. 终端运行
在 /root/code/InternLM
目录下新建一个 cli_demo.py
文件
在终端运行以下命令,即可体验 InternLM-Chat-7B
模型的对话能力。对话效果如下所示:
python /root/code/InternLM/cli_demo.py
5. web demo
切换到 VScode 中,运行 /root/code/InternLM
目录下的 web_demo.py
文件,输入以下命令后,将端口映射到本地。在本地浏览器输入 http://127.0.0.1:6006 即可。
bash
conda activate internlm-demo # 首次进入 vscode 会默认是 base 环境,所以首先切换环境
cd /root/code/InternLM
streamlit run web_demo.py --server.address 127.0.0.1 --server.port 6006
Lagent智能体工具调用Demo
1.环境准备
同InternLM-Chat-7B
2.模型下载
同InternLM-Chat-7B
3.Lagent 安装
首先切换路径到 /root/code
克隆 lagent
仓库,并通过 pip install -e .
源码安装 Lagent
cd /root/code
git clone https://gitee.com/internlm/lagent.git
cd /root/code/lagent
git checkout 511b03889010c4811b1701abb153e02b8e94fb5e # 尽量保证和教程commit版本一致
pip install -e . # 源码安装
4.修改代码
将 /root/code/lagent/examples/react_web_demo.py
内容替换
5.Demo 运行
streamlit run /root/code/lagent/examples/react_web_demo.py --server.address 127.0.0.1 --server.port 6006
浦语·灵笔图文创作理解 Demo
1. 环境准备
2. 模型下载
3. 代码准备
4. Demo运行
哈哈,效果还不错。