0
点赞
收藏
分享

微信扫一扫

D2-InternLM-Chat-7B 智能对话 Demo

使用InternStudio中的 A100(1/4) 机器和InternLM-Chat-7B模型部署一个智能对话 Demo。

一、环境准备

D2-InternLM-Chat-7B 智能对话 Demo_bash

进入 conda 环境:

bash # 请每次使用 jupyter lab 打开终端时务必先执行 bash 命令进入 bash 中

使用以下命令从本地克隆一个已有的 pytorch 2.0.1 的环境

conda create --name internlm-demo --clone=/root/share/conda_envs/internlm-base

D2-InternLM-Chat-7B 智能对话 Demo_bash_02

使用以下命令激活环境:

conda activate internlm-demo

D2-InternLM-Chat-7B 智能对话 Demo_大模型_03

在环境中安装运行 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

D2-InternLM-Chat-7B 智能对话 Demo_大模型_04

二、模型下载

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

  • -r 选项表示递归地复制目录及其内容

D2-InternLM-Chat-7B 智能对话 Demo_bash_05

------------------------------------------------------------------------------

也可以使用 modelscope 中的 snapshot_download 函数下载模型,第一个参数为 模型名称,参数 cache_dir 为模型的 下载路径

在 /root 路径下新建目录model,在目录下新建 download.py 文件并在其中输入以下内容,粘贴代码后记得保存文件,如下图所示。并运行 python /root/model/download.py 执行下载,模型大小为 14 GB,下载模型大概需要 10~20 分钟。

import torch
from modelscope import snapshot_download, AutoModel, AutoTokenizer
import os
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm-chat-7b', cache_dir='/root/model', revision='v1.0.3')

三、代码准备

先 clone 代码。在 /root 路径下新建 code 目录,然后切换路径, clone 代码:

cd /root/code

git clone https://gitee.com/internlm/InternLM.git

切换 commit 版本,与教程 commit 版本保持一致,可以让大家更好的复现。

cd InternLM

git checkout 3028f07cb79e5b1d7342f4ad8d11efad3fd13d17

D2-InternLM-Chat-7B 智能对话 Demo_部署_06

将 /root/code/InternLM/web_demo.py中 29 行和 33 行的模型更换为本地的 /root/model/Shanghai_AI_Laboratory/internlm-chat-7b

D2-InternLM-Chat-7B 智能对话 Demo_开源_07

D2-InternLM-Chat-7B 智能对话 Demo_大模型_08

四、终端运行

在 /root/code/InternLM 目录下新建一个 cli_demo.py 文件,将以下代码填入其中:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM


model_name_or_path = "/root/model/Shanghai_AI_Laboratory/internlm-chat-7b"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='auto')
model = model.eval()

messages = []

print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")

while True:
    input_text = input("User  >>> ")
    if input_text == "exit":
        break
    response, history = model.chat(tokenizer, input_text, history=messages)
    messages.append((input_text, response))
    print(f"robot >>> {response}")

然后在终端运行以下命令,即可体验 InternLM-Chat-7B 模型的对话能力:

python /root/code/InternLM/cli_demo.py

对话效果如下所示:

D2-InternLM-Chat-7B 智能对话 Demo_开源_09

五、web demo 运行

本地配置端口配置:

在本地配置端口,可以通过 ssh 连接到服务器,然后将服务器的端口映射到本地,这样就可以在本地浏览器中访问服务器的端口了。

ssh-keygen -t rsa

D2-InternLM-Chat-7B 智能对话 Demo_bash_10

提示选择密钥文件的保存位置,默认情况下是在 ~/.ssh/ 目录中。按 Enter 键接受默认值或输入自定义路径。

公钥默认存储在 ~/.ssh/id_rsa.pub,可以通过系统自带的 cat 工具查看文件内容:

cat ~\.ssh\id_rsa.pub

将公钥复制到剪贴板中,然后回到 InternStudio 控制台,点击配置 SSH Key。如下图所示:

D2-InternLM-Chat-7B 智能对话 Demo_bash_11

D2-InternLM-Chat-7B 智能对话 Demo_bash_12

在本地终端输入以下指令 .6006 是在服务器中打开的端口,而 33799 是根据开发机的端口进行更改:

ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p 33799

切换到开发机 VScode 中,运行 /root/code/InternLM 目录下的 web_demo.py 文件,输入以下命令:

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

D2-InternLM-Chat-7B 智能对话 Demo_bash_13

在本地浏览器输入 http://127.0.0.1:6006 ,下面是加载界面:

D2-InternLM-Chat-7B 智能对话 Demo_大模型_14

在加载完模型之后,就可以与 InternLM-Chat-7B 进行对话:

D2-InternLM-Chat-7B 智能对话 Demo_bash_15

D2-InternLM-Chat-7B 智能对话 Demo_开源_16


举报

相关推荐

0 条评论