LLaMA-Factory 微调 DeepSeek-R1 模型实战指南
使用 LLaMA-Factory 进行大模型微调,优势在于流程简化与一站式服务。它支持全量调参、LoRA 等多种策略,涵盖量化处理与运行部署,无需在不同工具间切换。此外,内置的评测指标(如 BLEU)和 Loss 曲线监控,能帮助用户有效评估模型性能。
环境搭建
首先确保 Python 版本在 3.9 以上,推荐 3.10。创建虚拟环境后,进入项目目录安装依赖。
pip install -r requirements.txt
pip install -e ".[torch,metrics]"
GPU 用户需配置 CUDA。若使用 Windows 平台开启 QLoRA 量化,需安装预编译的 bitsandbytes 库。
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c nvidia
pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.41.2.post2-py3-none-win_amd64.whl
启动 WebUI:
llamafactory-cli webui
若遇到 localhost 访问问题,可修改源码中的 interface.py,将 share=False 改为 share=True 以生成分享链接,注意避免网络代理干扰。
数据集准备与清洗
本教程以微信聊天记录风格化为例。使用数据导出工具获取原始对话文件(txt/json)。合并多份记录时,编写脚本遍历文件夹下的所有 .json 文件并汇总。
import os, json
folder_path = './chat_logs'
json_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.json'):
json_files.append(os.path.join(root, file))
merged_data = []
for file in json_files:
with open(file, 'r', encoding='utf-8') as f:
try:
data = json.load(f)
merged_data.append(data)
except json.JSONDecodeError:
print(f"Error decoding {file}. Skipping.")
with open('merged_data.json', , encoding=) f:
json.dump(merged_data, f, indent=, ensure_ascii=)
()


