You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Unsloth SFTTrainer微调LLM:单列UltraChat数据集适配问题求助

解决UltraChat单列数据适配Unsloth双列格式的方案

1. 先明确数据转换逻辑

Unsloth SFTTrainer需要的核心格式是**指令(instruction)和响应(response)**的键值对结构,而UltraChat的单列数据本质是对话文本,只需拆解出用户输入作为指令、助手输出作为响应即可。

2. 数据转换脚本示例

用Python编写脚本完成格式转换与数据集合并:

from datasets import load_dataset

# 加载UltraChat数据集
ultrachat = load_dataset("openbmb/UltraChat", split="train")

# 转换UltraChat为双列格式
def format_ultrachat(example):
    # 适配UltraChat常见的"User: ... Assistant: ..."格式
    if "data" in example:
        split_content = example["data"].split("Assistant: ", 1)
        if len(split_content) == 2:
            instruction = split_content[0].replace("User: ", "").strip()
            response = split_content[1].strip()
            return {"instruction": instruction, "response": response}
    # 适配多轮对话结构(若数据集用conversations字段存储)
    elif "conversations" in example:
        convs = example["conversations"]
        if len(convs) >= 2 and convs[-1]["role"] == "assistant":
            instruction = convs[-2]["content"]
            response = convs[-1]["content"]
            return {"instruction": instruction, "response": response}
    return None

# 应用转换并过滤无效数据
formatted_ultrachat = ultrachat.map(format_ultrachat).filter(lambda x: x is not None)

# 处理SynthCypher数据集,统一字段名
synthcypher = load_dataset("ServiceNow-AI/SynthCypher", split="train")
def rename_synth_fields(example):
    return {"instruction": example["question"], "response": example["query"]}
synthcypher = synthcypher.map(rename_synth_fields)

# 合并两个数据集
combined_dataset = formatted_ultrachat.concat(synthcypher)

# 保存转换后的数据集(可选,方便重复使用)
combined_dataset.save_to_disk("./combined_cypher_ultrachat")

3. 训练适配注意事项

  • 确保合并后的数据集仅保留instruction和response两个核心字段,Unsloth的默认Prompt模板会自动调用这两个字段构建训练样本。
  • 训练时直接将combined_dataset传入Unsloth SFTTrainer即可,无需修改原有训练代码框架。

4. 格式验证

转换完成后可打印样本确认格式:

print(combined_dataset[0])
# 预期输出:{"instruction": "用户的问题/指令内容", "response": "模型的目标输出内容"}

内容的提问来源于stack exchange,提问作者Calciumm502

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 03:32:27