前言
LLM模型微調 能讓大模型掌握特定行業的深度知識,能夠實現AI虛擬主播,AI醫生,AI程序員,AI網絡安全工程師等特定領域的延展。更重要的是,當有本地部署的硬件條件限制時,能夠讓微調後小的大語言模型等效百億級的大語言模型
測試環境:windows11,RTX4070顯卡
下面將手把手帶你跑通無代碼模型微調的全過程
環境安裝
必要的工具:
- git: https://git-scm.cn/ (方便拉取資源)
- python: https://www.python.org/ (微調和運行必要環境)
流程:
- 創建文件夾,並拉取 llama-factory項目
mkdir D:/LLM-Tuning
cd D:/LLM-Tuning
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
- 安裝LLaMA-Factory需要的環境
pip install -e ".[torch,metrics]"
pip install modelscope
- 驗證環境
python -c "import torch; print(torch.cuda.is_available()); print(torch.cuda.get_device_name(0))"
正常輸出如下:

錯誤:正常來説安裝完後驗證環境會顯示顯卡型號,但是我在安裝時,會出現報錯,原因是它安裝了錯誤的cuda版本,需要重新安裝
torch
解決方法如下:pip uninstall torch torchvision torchaudio pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126如果其他版本請參考官網: https://pytorch.org/get-started/locally/
微調
這裏用於演示,只對模型做一個自我認知的微調
準備數據集
拉取數據集
git clone https://www.modelscope.cn/datasets/DanKe123abc/yuki_identity_sft.git
修改數據集
下載完後,目錄結構如下:

我們需要關注的是yuki_identity_sft.jsonl文件,用編輯器將下列文字全局替換:
Yuki => 陳千語
DanKe => 管理員
效果圖如下:

準備本地模型
這裏使用的是qwen2.5_1.5B用於演示
下載模型
from modelscope import snapshot_download
download_dir = "D:\\Models\\Qwen2.5-1.5B-Instruct"
model_dir = snapshot_download(
'qwen/Qwen2.5-1.5B-Instruct',
cache_dir=download_dir,
revision='master'
)
print(f"下載完成!模型路徑為: {model_dir}")
微調
配置數據集信息

- 打開
D:\LLM-Tuning\LLaMA-Factory\data文件,將剛剛修改好的數據集yuki_identity_sft.jsonl文件拖入文件夾中 - 打開
dataset_info.json文件,添加新配置:

"MytestData": {
"file_name":"yuki_identity_sft.jsonl",
"columns": {
"messages": "conversations"
},
"tags": {
"role_tag": "role",
"content_tag": "content",
"user_tag": "user",
"assistant_tag": "assistant"
},
"formatting": "sharegpt"
},
打開LLamaFactory微調面板
python -m llamafactory.cli webui
設置參數如圖,其他的默認就行:

設置完後直接點擊開始,模型就開始訓練了,訓練完後會出現下面提示:

驗證模型
加載訓練完後的lora模型

訓練前後的大模型對比
訓練前

訓練後

觀察圖片可以發現,微調後qwen2.5認為自己是陳千語,自己由管理員開發的
大模型部署
下面不是新手向
如果只是希望學習微調的在這裏已經結束了,下面是本系列教程的後續,如何用langchain部署本地的LLM微調大語言模型
環境配置
安裝需要的環境
pip install peft langchain langchain-huggingface
下面是樣例代碼
代碼流程如下:
加載基座模型->加載 LoRA 權重->正在合併權重->構建Langchain通道->調用模型
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from peft import PeftModel
from langchain_huggingface import HuggingFacePipeline
from langchain_core.prompts import PromptTemplate
BASE_MODEL_PATH = r'D:\Models\Qwen2.5-1.5B-Instruct\qwen\Qwen2___5-1___5B-Instruct'
LORA_PATH = r'D:\D_MyProject\LLM-Tuning\LLaMA-Factory\saves\Qwen2.5-1.5B\lora\train_2026-02-13-23-16-50\checkpoint-260'
print("1. 正在加載基座模型...")
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_PATH, trust_remote_code=True)
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL_PATH,
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=True
)
print("2. 正在加載 LoRA 權重 ...")
model = PeftModel.from_pretrained(base_model, LORA_PATH)
print("3. 正在合併權重 ...")
model = model.merge_and_unload()
print("4. 構建 LangChain 管道...")
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=200,
do_sample=True,
temperature=0.7,
repetition_penalty=1.1
)
llm = HuggingFacePipeline(pipeline=pipe)
print("\n=== 陳千語上線 ===\n")
respone = llm.invoke('你好,你是誰?')
print(f"{respone}")
演示效果

至此,我們成功的實現了大模型LLM從微調到部署,把之前的langchain串起來...
如果❤喜歡❤本系列教程,就點個關注吧,後續不定期更新~