ollama , vllm,llama.cpp
等方式也可快速部署,但是无法微调和训练,只能推理使用(线上正式使用),下面使用另一种简单的方式部署,可训练,微调,推理
链接
- deepseek 模型:
https://www.modelscope.cn/models/deepseek-ai/DeepSeek-R1/summary
- 训练和微调:
https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/sft.html
环境
以下配置是最小配置,仅仅能运行起来小模型(<=8B),测试体验使用,无法并发
- GPU显存 >= 24G
- CUDA >= 11.8,建议 12.2
- python==3.10
- CPU >= 12核
- 运行内存 >= 64G
nvidia-smi
命令正确输出nvcc -V
命令正确输出,并且版本匹配
创建 python 3.10 环境
conda create -n py310 python=3.10
conda activate py310
下载框架
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]"
pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 transformers==4.43.2 vllm==0.6.1 openai==1.61.0 deepspeed==0.14.4 //可以不执行,但是防止与其他冲突
设置环境变量,将 HuggingFace设置为ModelScope
export USE_OPENMIND_HUB=1 // 1=ModelScope,0=HuggingFace
下载模型到本地(下载小模型测试比较快)
最大模型需要800G运行内存,这里使用小模型
pip install modelscope
modelscope download --model deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B --local_dir '/ossfs/workspace/DeepSeek-R1-Distill-Qwen-1.5B'
创建启动配置文件(也可以不创建,直接将参数拼接在启动命令后面)
- deepseek-base.yaml
model_name_or_path: deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B # 会自动下载也可以提前把模型下载到本地,填写本地路径
template: deepseek3 # 训练和推理时构造 prompt 的模板
infer_backend: huggingface # 部署框架: [huggingface, vllm]
部署
方式1:api 接口方式部署(对外暴露接口)
API_PORT=7007 CUDA_VISIBLE_DEVICES=0 llamafactory-cli api deepseek-base.yaml
方式2:web 聊天页面方式部署(需要开外网端口访问 web 页面)
API_PORT=7007 CUDA_VISIBLE_DEVICES=0 llamafactory-cli webchat deepseek-base.yaml
方式3:命令行模式启动(终端命令行聊天)
CUDA_VISIBLE_DEVICES=0 llamafactory-cli chat deepseek-base.yaml
测试
- 新建
chat.py
import os
from openai import OpenAI
from transformers.utils.versions import require_version
require_version("openai>=1.5.0", "To fix: pip install openai>=1.5.0")
if __name__ == '__main__':
port = 7007
client = OpenAI(
api_key="0",
base_url="http://localhost:{}/v1".format(os.environ.get("API_PORT", 7007)),
)
messages = []
messages.append({"role": "user", "content": "你好,什么是deepseek?"})
result = client.chat.completions.create(messages=messages, model="test")
print(result.choices[0].message)
python chat.py
- 或新建chat2.py
import os
from openai import OpenAI
from transformers.utils.versions import require_version
require_version("openai>=1.5.0", "To fix: pip install openai>=1.5.0")
if __name__ == '__main__':
port = 7007
client = OpenAI(
api_key="0",
base_url="http://localhost:{}/v1".format(os.environ.get("API_PORT", 7007)),
)
while True:
# 获取用户输入的问题
question = input("请输入你的问题(输入 'q' 退出):")
# 如果用户输入 'q',则退出循环
if question.lower() == 'q':
break
# 构建消息列表
messages = [{"role": "user", "content": question}]
try:
# 发送请求获取回复
result = client.chat.completions.create(messages=messages, model="test")
# 打印回复内容
print(result.choices[0].message.content)
print("-------------------------------")
except Exception as e:
print(f"请求出错:{e}")
命令行方式启动
备注
- 简单的问题不经过思考,复杂的问题才会 think
微调(SFT)
- 只讲微调,不讲全量训练,全量训练需要TB级别数据集和千台服务器
- 将自己的数据整理为标准的数据集格式:
https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/data_preparation.html#id7
注册数据集
- 追加
data/dataset_info.json
里的内容,注册自己的数据集
开始微调
- 创建训练配置文件:
llama3_lora_sft_ds3.yaml
,并修改参数:https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/sft.html
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_lora/llama3_lora_sft_ds3.yaml
测试结果
- 修改推理配置文件,增加
adapter_name_or_path
和finetuning_type
参数
model_name_or_path: ../DeepSeek-R1-Distill-Qwen-32B
adapter_name_or_path: saves/DeepSeek-R1-Distill-Qwen-32B/lora/sft
template: deepseek3
finetuning_type: lora
infer_backend: huggingface # 部署框架: [huggingface, vllm]
- 重新启动微调后的模型
llamafactory-cli chat examples/inference/deepseek-base-lora.yaml
- 如果微调没有效果,增大文件中的:
num_train_epochs
参数 - 不是机械回答,问类似的相关问题,也会根据微调数据集正确回答