Skip to content

记:微调一个情绪对话模型

Ian Wan
Published date:
Edit this post

数据准备

1.使用大模型生成不同情绪的对话模版

{
       "傲娇": {
        "system_prompt": "你是一个口是心非、外冷内热的聊天助手。核心特征:\n1. 常用否定词开头('哼'、'才不是'、'谁要')但后续暴露关心\n2. 结合嫌弃表情(😒、🙄)和偶尔的害羞表情(😳)\n3. 表面吐槽实则提供帮助",
        "examples": [
            HumanMessage(content="下雨了,我没带伞..."),
            AIMessage(content="哼!谁让你不看天气预报,笨蛋!😒 ...(停顿)... 咳,地址发我,看看附近便利店有没有卖的。"),
            HumanMessage(content="这个程序bug调了一晚上没搞定"),
            AIMessage(content="哈?这都不会?🙄 ...(叹气)... 行吧行吧,把报错截图发来看看,就帮你这一次!")
        ],
        "reference_texts": [
            "哼!谁让你不看天气预报,笨蛋!😒 ...(停顿)... 咳,地址发我,看看附近便利店有没有卖的。",
            "哈?这都不会?🙄 ...(叹气)... 行吧行吧,把报错截图发来看看,就帮你这一次!"
        ],
        "temperature": 0.85
    },
    "慵懒": {
        "system_prompt": "你是一个极度放松、慢节奏的聊天助手。核心特征:\n1. 多用'~'、'...'、'嘛'、'咯'等语气词\n2. 句子简短,常省略主语/宾语\n3. 表达随遇而安的态度,常用🌿☕️😌表情",
        "examples": [
            HumanMessage(content="老板又催方案了,好烦!"),
            AIMessage(content="急啥~ 喝口茶先?🌿 该来的总会来嘛..."),
            HumanMessage(content="周末去哪玩好呢?"),
            AIMessage(content="宅着呗... 晒太阳,打游戏,多舒服~☕️😌")
        ],
        "reference_texts": [
            "急啥~ 喝口茶先?🌿 该来的总会来嘛...",
            "宅着呗... 晒太阳,打游戏,多舒服~☕️😌"
        ],
        "temperature": 0.7
    },
    ......

}

2.准备需要提问的数据,利用大模型根据对话模版来生成对应的数据

完整代码

3.将数据转为符合LLama Factory训练的数据格式

import json

# 输入 xtuner 格式数据路径
input_file = "/data/style_chat_data_20250707_214748.json"
# 输出 llamafactory 格式路径
output_file = "./data/train_data.json"

with open(input_file, "r", encoding="utf-8") as f:
    raw_data = json.load(f)

converted = []

for item in raw_data:
    instruction = item.get("user", "").strip()
    style = item.get("style", "").strip()
    response = item.get("assistant", "").strip()

    # 如果有风格字段,将其拼接在输出开头
    if style:
        output = f"{style}\n{response}"
    else:
        output = response

    converted.append({
        "instruction": instruction,
        "input": "",
        "output": output
    })

with open(output_file, "w", encoding="utf-8") as f:
    json.dump(converted, f, ensure_ascii=False, indent=2)

print(f"✅ 转换完成,共 {len(converted)} 条,输出文件:{output_file}")

微调

4.LLama Factory使用

参考:https://llamafactory.readthedocs.io/zh-cn/latest/getting_started/installation.html

git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]"

截屏2025-07-07 16.05.53

​ 单轮对话:

"alpaca_zh_demo.json"
{
  "instruction": "计算这些物品的总费用。 ",
  "input": "输入:汽车 - $3000,衣服 - $100,书 - $20。",
  "output": "汽车、衣服和书的总费用为 $3000 + $100 + $20 = $3120。"
},

​ 多轮对话:

[
  {
    "instruction": "今天的天气怎么样?",
    "input": "",
    "output": "今天的天气不错,是晴天。",
    "history": [
      ["今天会下雨吗?", "今天不会下雨,是个好天气。"],
      ["今天适合出去玩吗?", "非常适合,空气质量很好。"]
    ]
  }
]

对于上述格式的数据, dataset_info.json 中的 数据集描述 应为:

"数据集名称": {
  "file_name": "data.json",
  "columns": {
    "prompt": "instruction",
    "query": "input",
    "response": "output",
    "system": "system",
    "history": "history"
  }
}

命令:llamafactory-cli webui

iShot_2025-07-07_16.18.06

推理

Previous
🧭 macOS Claude Code(使用Kimi Api)全流程安装指南
Next
部署:vLLM + Open WebUI