conda环境(可以在自己的环境中配置,记下来方便自己本地部署)
建立环境
# conda create -n demo python==3.10 -y
# conda activate demo
# conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia
激活环境
conda activate demo
配置环境
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
模型下载
这利用的是 modelscope.,感觉和huggingfac下载大同小异
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')
运行模型
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)
主要包括
模型加载:
model_name_or_path:指定预训练模型的路径或名称。
tokenizer:使用AutoTokenizer.from_pretrained方法加载internlm2-chat-1_8b的标记器。
model:使用AutoModelForCausalLM.from_pretrained方法加载internlm2-chat-1_8b。
system_prompt:这是对话系统的初始提示或介绍。
messages:这是一个列表,包含用户输入和机器人响应的元组。初始时,它只包含系统提示。
对话循环:
进入一个while循环,直到用户输入 “exit”。
在循环中,获取用户输入。
使用模型的stream_chat方法处理输入。该方法可能根据用户输入和对话历史(messages)生成响应。
作业
300字故事
已经远超300字,模型本身可能训练了太多书生浦语的知识,固定化输出指定文字,不具备变通性。
huggingfac下载
注意配置镜像
export HF_ENDPOINT=https://hf-mirror.com
import os
from huggingface_hub import hf_hub_download # Load model directly
hf_hub_download(repo_id="internlm/internlm2-7b", filename="config.json")
Lagent
大模型使用Lagent和人用工具差不多
这里教程少了端口配置
ssh-keygen -t rsa
cat ~.ssh\id_rsa.pub
添加公钥
能正常登录
这里没出现密码
超显存了
做不了了。。。。