本节课可以让同学们实践 4 个主要内容,分别是:
1、部署 InternLM2-Chat-1.8B
模型进行智能对话
1.1安装依赖库:
pip install huggingface-hub==0.17.3
pip install transformers==4.34
pip install psutil==5.9.8
pip install accelerate==0.24.1
pip install streamlit==1.32.2
pip install matplotlib==3.8.3
pip install modelscope==1.9.5
pip install sentencepiece==0.1.99
1.2下载 InternLM2-Chat-1.8B
模型
import os
from modelscope.hub.snapshot_download import snapshot_download
# 创建保存模型目录
os.system("mkdir /root/models")
# save_dir是模型保存到本地的目录
save_dir="/root/models"
snapshot_download("Shanghai_AI_Laboratory/internlm2-chat-1_8b",
cache_dir=save_dir,
revision='v1.1.0')
1.3运行 cli_demo
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name_or_path = "/root/models/Shanghai_AI_Laboratory/internlm2-chat-1_8b"
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, trust_remote_code=True, device_map='cuda:0')
model = AutoModelForCausalLM.from_pretrained(model_name_or_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map='cuda:0')
model = model.eval()
system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
"""
messages = [(system_prompt, '')]
print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")
while True:
input_text = input("\nUser >>> ")
input_text = input_text.replace(' ', '')
if input_text == "exit":
break
length = 0
for response, _ in model.stream_chat(tokenizer, input_text, messages):
if response is not None:
print(response[length:], flush=True, end="")
length = len(response)
2、部署实战营优秀作品 八戒-Chat-1.8B
模型
- 八戒-Chat-1.8B:魔搭社区
- 聊天-嬛嬛-1.8B:OpenXLab浦源 - 模型中心
- Mini-Horo-巧耳:OpenXLab浦源 - 模型中心
git clone https://gitee.com/InternLM/Tutorial -b camp2
执行下载模型:
python /root/Tutorial/helloworld/bajie_download.py
待程序下载完成后,输入运行命令:
streamlit run /root/Tutorial/helloworld/bajie_chat.py --server.address 127.0.0.1 --server.port 6006
3、通过 InternLM2-Chat-7B
运行 Lagent
智能体 Demo
Lagent 的特性总结如下:
- 流式输出:提供 stream_chat 接口作流式输出,本地就能演示酷炫的流式 Demo。
- 接口统一,设计全面升级,提升拓展性,包括:
- 型号 : 不论是 OpenAI API, Transformers 还是推理加速框架 LMDeploy 一网打尽,模型切换可以游刃有余;
- Action: 简单的继承和装饰,即可打造自己个人的工具集,不论 InternLM 还是 GPT 均可适配;
- Agent:与 Model 的输入接口保持一致,模型到智能体的蜕变只需一步,便捷各种 agent 的探索实现;
- 文档全面升级,API 文档全覆盖。
下载模型:
git clone https://gitee.com/internlm/lagent.git
# git clone https://github.com/internlm/lagent.git
cd /root/demo/lagent
git checkout 581d9fb8987a5d9b72bb9ebd37a95efd47d479ac
pip install -e . # 源码安装
在 terminal 中输入指令,构造软链接快捷访问方式:
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-7b /root/models/internlm2-chat-7b
打开 路径下 文件,并修改对应位置 (71行左右) 代码 :internlm2_agent_web_demo_hf.py
修改模型地址:
运行前端代码:
streamlit run /root/demo/lagent/examples/internlm2_agent_web_demo_hf.py --server.address 127.0.0.1 --server.port 6006
4、实践部署 浦语·灵笔2
模型
补充环境包,选用 进行开发:50% A100
pip install timm==0.4.12 sentencepiece==0.1.99 markdown2==2.4.10 xlsxwriter==3.1.2 gradio==4.13.0 modelscope==1.9.5
下载 InternLM-XComposer 仓库 相关的代码资源:
cd /root/demo
git clone https://gitee.com/internlm/InternLM-XComposer.git
# git clone https://github.com/internlm/InternLM-XComposer.git
cd /root/demo/InternLM-XComposer
git checkout f31220eddca2cf6246ee2ddf8e375a40457ff626
在 中输入指令,构造软链接快捷访问方式:terminal
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm-xcomposer2-7b /group_share/01/models/internlm-xcomposer2-7b
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm-xcomposer2-vl-7b /group_share/01/models/internlm-xcomposer2-vl-7b
图文写作实战(开启 50% A100 权限后才可开启此章节)
cd /root/demo/InternLM-XComposer
python /root/demo/InternLM-XComposer/examples/gradio_demo_composition.py \
--code_path /root/models/internlm-xcomposer2-7b \
--private \
--num_gpus 1 \
--port 6006
图片理解实战(开启 50% A100 权限后才可开启此章节)
根据附录 6.4 的方法,关闭并重新启动一个新的 ,继续输入指令,启动 :terminal
InternLM-XComposer2-vl
conda activate demo
cd /root/demo/InternLM-XComposer
python gradio_demo_chat.py \
--code_path /group_share/01/models/internlm-xcomposer2-vl-7b \
--private \
--num_gpus 1 \
--port 6006