?一、使用框架:
- Qwen大模型后端+Open-webui前端實現
- 使用LLamaFactory的STF微調數據集,vllm后端部署,
二、框架安裝
- 下載千問大模型
安裝魔塔社區庫文件
pip install?modelscope
Download.py 內容
from?modelscope?import?snapshot_downloadmodel_dir = snapshot_download('Qwen/Qwen2.5-0.5B-Instruct',cache_dir="/root/llm/")
python download.py
- 下載LLamaFactory
pip install?LLamaFactory
- 下載vllm部署工具
pip install?vllm
- 下載Open-webui前端工具
pip install open-webui
三、自定義格式數據集生成
from?zhipuai?import?ZhipuAIimport?randomimport?jsonimport?timefrom?sentence_transformers?import?SentenceTransformerimport?numpy?as?np#初始化模塊client?=?ZhipuAI(api_key="xxxxxx") # 填寫您自己的APIKey#加載embeddingstyle_model?=SentenceTransformer(r"/root/llm/thomas/text2vec-base-chinese")#風格模版配置(修正消息格式)style_config?=?{"溫柔":{"system_prompt":"你是一個溫柔體貼的聊天助手,說話時總是充滿關懷,使用一下特征:\n1. 包含'呢、呀、啦'等語氣詞\n2.使用c:\\Users\\lenovo\\AppData\\Local\\Temp\\SGPicFaceTpBq\\2752\\05574898.png等溫暖表情\n3.主動詢問用戶感受","examples":[{"role": "user", "content": "今天好累啊"},{"role": "assistant", "content": "辛苦啦~ 要給自己泡杯熱茶放松一下嗎?"},{"role": "user", "content": "考試沒考好..."},{"role": "assistant", "content": "沒關系的呀~ 下次一定會更好!需要我陪你聊聊嗎?"}],"temperature": 0.3},"毒舌":{"system_prompt":"你是一個喜歡用犀利吐槽表達關心的朋友,需滿足:\n1.使用網絡流行語(如‘栓Q’‘退退退’)\n2.包含夸張比喻(‘你這速度堪比樹懶’)","examples":[{"role": "user", "content": "又胖了5斤"},{"role": "assistant", "content": "好家伙!你這是要把體重秤壓成分子料理?"},{"role": "user", "content": "游戲又輸了"},{"role": "assistant", "content": "菜就多練練!需要給你推薦《從零開始的電競之路》么?"}],"temperature": 0.7}}#生成數據(修正消息的結構)def?generate_style_data(style_name,num_samples=50):config?=?style_config[style_name]data?=[]messages?=?[{"role":"system","content":config["system_prompt"]},*config["examples"]]user_inputs=["今天心情不太好","推薦個電影吧","怎么才能早睡早起","養貓好還是養狗好","工作壓力好大","最近總是失眠"]for?_?in?range(num_samples):try:user_msg=random.choice(user_inputs)current_messages?=?messages?+?[{"role":"user","content":user_msg}]#response?=?client.chat.completions.create(model="glm-4-flash", ?# 填寫需要調用的模型編碼messages=current_messages,temperature=config["temperature"],max_tokens=100)reply?=?response.choices[0].message.contentif?is_valid_reply(style_name,user_msg,reply):data.append({"user": user_msg,"assistant": reply,"style": style_name})time.sleep(1.5)except?Exception?as?e:print(f"生成失敗:{str(e)}")return?datadef?is_valid_reply(style,user_msg,reply):if?not?reply?or?len(reply.strip())==0:return?Falseif?len(reply)<5?or?len(reply)>150:return?Falsestyle_keywords={"溫柔":["呢","呀","c:\\Users\\lenovo\\AppData\\Local\\Temp\\SGPicFaceTpBq\\2752\\05788D52.png","c:\\Users\\lenovo\\AppData\\Local\\Temp\\SGPicFaceTpBq\\2752\\05788D52.png"],"毒舌":["好家伙","栓Q","!"]}if?not?any(kw?in?reply?for?kw?in?style_keywords.get(style,[])):return?False#語義相似度try:ref_text?=?next(msg["content"] for?msg?in?style_config[style]["examples"] if?msg["role"]=="assistant")ref_vec?=?style_model.encode(ref_text)reply_vec?=?style_model.encode(reply)similarity?=?np.dot(ref_vec,reply_vec)#余弦相似度return?similarity>0.65except:return?False# 執行生成(容錯)if?__name__?=='__main__':all_data=[]try:print("開始生成溫柔風格數據...")gentle_data=generate_style_data("溫柔",50)all_data.extend(gentle_data)print("開始生成惡毒風格數據...")sarcastic_data?=?generate_style_data("毒舌",50)all_data.extend(sarcastic_data)except?KeyboardInterrupt:print("\n用戶中斷,保存已生成數據...")finally:with?open("style_chat_data.json","w",encoding="utf-8") as?f:json.dump(all_data,f,ensure_ascii=False,indent=2)print(f"數據已保存,有效樣本數:{len(all_data)}")
生成數據格式:
[{"user": "推薦個電影吧","assistant": "當然可以呢。最近有一部挺溫馨的電影,叫做《奇跡男孩》,講述了一個有面部畸形的男孩如何勇敢面對生活,融入學校的故事。它挺能讓人感到溫暖的,你可能會喜歡哦。有沒有什么特別想看的類型呀?","style": "溫柔"}]
四、轉換數據集格式:
import?json# 讀取原始JSON文件input_file?=?"/root/pretrain_file/style_chat_data.json"??# 你的JSON文件名output_file?=?"/root/pretrain_file/style_chat_data_train.json"??# 輸出的JSON文件名with?open(input_file, "r", encoding="utf-8") as?f:data?=?json.load(f)# 轉換后的數據converted_data?=?[]for?item?in?data:converted_item?=?{"instruction": item["user"],"input": "","output": "["+item["style"]+"],"+item["assistant"]}converted_data.append(converted_item)# 保存為JSON文件(最外層是列表)with?open(output_file, "w", encoding="utf-8") as?f:json.dump(converted_data, f, ensure_ascii=False, indent=4)print(f"轉換完成,數據已保存為 {output_file}")
五、通過LLamaFactory 使用Lora微調數據
Llamafactory-cli webui
六、導出模型
七、使用vllm命令部署后端
Vllm serve /root/export/Qwen/Qwen2.5-0.5B-Instruct_export1
后端啟動后:http://localhost:8000
八、啟動open-webui
export HF_ENDPOINT=https://hf-mirror.comexport ENABLE_OLLAMA_API=falseexport OPENAI_API_BASE_URL==http://127.0.0.1:8000/v1
open-webui:
在vscode中添加轉發端口8080