介绍
新一代大语言模型 InternLM2(书生·浦语)在推理、对话体验及工具调用能力上均有显著提升,支持 20 万字超长上下文,能够处理长文档摘要及复杂的多轮对话任务。
InternLM2 是新一代开源大语言模型,支持 20 万字超长上下文和工具调用。本文介绍 InternLM2-Chat-7B 模型的本地化部署流程,涵盖环境准备、Gradio 与 Streamlit 页面交互部署方式,以及基于 Transformers、ModelScope 和 LMDeploy 的代码调用方法。内容包含显存需求分析、启动参数说明及常见错误排查,帮助开发者快速搭建私有化大模型服务。

新一代大语言模型 InternLM2(书生·浦语)在推理、对话体验及工具调用能力上均有显著提升,支持 20 万字超长上下文,能够处理长文档摘要及复杂的多轮对话任务。
InternLM2 包含 InternLM2-7B 和 InternLM2-20B 两种规格。根据应用场景不同,分为 InternLM2-Base、InternLM2、InternLM2-Chat-SFT 和 InternLM2-Chat。其中 InternLM2 为基础模型,InternLM2-Chat 为官方推荐的对话模型。本文以 InternLM2-Chat-7B 为例,详细介绍其本地化部署与使用流程。
| 模型 | HuggingFace | ModelScope |
|---|---|---|
| InternLM2-Chat-7B | internlm/internlm2-chat-7b | Shanghai_AI_Laboratory/internlm2-chat-7b |
| InternLM2-Chat-20B | internlm/internlm2-chat-20b | Shanghai_AI_Laboratory/internlm2-chat-20b |
InternLM2-Chat-7B 模型在量化或半精度模式下运行,通常消耗约 20GB 显存。建议使用配备 RTX 3090 或 RTX 4090 等高性能 GPU 的服务器。若显存不足,可考虑使用量化版本或降低 batch size。
transformers:用于加载模型。lmdeploy:用于高性能推理服务。gradio / streamlit:用于构建 Web 交互界面。LMDeploy 封装了 Gradio 接口,适合快速搭建演示服务。
启动参数说明:
tp (tensor_parallel_size):张量并行度,表示使用几张 GPU 运行一个模型。max_batch_size:批处理大小,值越大吞吐量越高,但显存占用增加。cache_max_entry_count:K/V 缓存大小设置。小数表示显存百分比,整数表示 block 数量。server_name / server_port:服务监听地址和端口。部署脚本示例:
cd ~
# 安装运行环境
echo "Installing Python dependencies"
pip install lmdeploy socksio gradio==3.50.2
# 安装 Git LFS 扩展包
echo "Installing git-lfs extension"
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install -y git-lfs
git lfs install
# 拉取模型库
echo "Download repo"
git clone https://huggingface.co/internlm/internlm2-chat-7b
# 启动模型
python3 -m lmdeploy.serve.gradio.app \
--tp=1 \
--max_batch_size=64 \
--cache_max_entry_count=0.1 \
--server_name=0.0.0.0 \
--server_port=8888 \
./internlm2-chat-7b
执行脚本后,需确保服务器防火墙开放对应端口(如 8888),以便外部访问。
官方提供了基于 Streamlit 的示例代码,适合轻量级展示。
部署脚本示例:
cd ~
# 安装环境
pip install streamlit==1.24.0
pip install transformers==4.37.0
# 克隆代码
git clone https://github.com/InternLM/InternLM.git
# 运行
streamlit run ./InternLM/chat/web_demo.py
默认启动端口为 8501,同样需要配置网络访问权限。
适用于直接加载模型进行推理。
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
if __name__ == '__main__':
# 本地路径请根据实际情况调整
model_path = "./internlm2-chat-7b"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.float16
)
model = model.eval()
response, history = model.chat(tokenizer, "你好 我是 Cleaner", history=[])
print(response)
适用于从魔搭社区下载模型。
import torch
from modelscope import snapshot_download, AutoTokenizer, AutoModelForCausalLM
if __name__ == '__main__':
# 下载模型到本地目录
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm2-chat-7b')
tokenizer = AutoTokenizer.from_pretrained(model_dir, device_map="auto", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_dir,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.float16
)
model = model.eval()
response, history = model.chat(tokenizer, "你好 我是 Cleaner", history=[])
print(response)
适用于高性能推理场景,支持 TensorRT-LLM 后端。
from lmdeploy import pipeline, TurbomindEngineConfig
if __name__ == '__main__':
backend_config = TurbomindEngineConfig(
tp=1,
max_batch_size=64,
cache_max_entry_count=0.1
)
# 替换为实际模型路径
pipe = pipeline("./internlm2-chat-7b", backend_config=backend_config)
response = pipe(["你好 我是 Cleaner"])
print(response)
如果部署过程中出现 OOM 错误,可尝试以下方案:
max_batch_size。cache_max_entry_count 的值。tp 参数,利用多 GPU 加速计算。max_batch_size,提高吞吐量。InternLM2 具备开源免费、可商用、超长上下文支持及工具调用能力强等特点。通过上述步骤,开发者可以快速在本地搭建私有化大模型服务,结合业务需求进行微调或应用开发,打造专属的智能助手。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online