LLaMA Factory 官方文档在 data/README.md
中明确声明:
Currently we support datasets in alpaca and sharegpt format. Allowed file types include json, jsonl, csv, parquet, arrow.
这表明 JSONL 格式是 LLaMA Factory 原生支持的文件格式之一。
在 src/llamafactory/extras/constants.py
中,LLaMA Factory 定义了完整的文件类型映射:
python展开代码FILEEXT2TYPE = {
"arrow": "arrow",
"csv": "csv",
"json": "json",
"jsonl": "json", # JSONL 被映射为 "json" 类型
"parquet": "parquet",
"txt": "text",
}
这个映射确保了 .jsonl
文件能够被正确识别和处理。
在 src/llamafactory/data/loader.py
中,数据加载器会根据文件扩展名自动选择处理方式:
python展开代码data_path = FILEEXT2TYPE.get(os.path.splitext(data_files[0])[-1][1:], None)
if data_path is None:
raise ValueError("Allowed file types: {}.".format(",".join(FILEEXT2TYPE.keys())))
在 src/llamafactory/data/data_utils.py
中,LLaMA Factory 提供了专门的 JSONL 读取函数:
python展开代码def _read_json_with_fs(fs: "fsspec.AbstractFileSystem", path: str) -> list[Any]:
r"""Helper function to read JSON/JSONL files using fsspec."""
with fs.open(path, "r") as f:
if path.endswith(".jsonl"):
return [json.loads(line) for line in f if line.strip()] # 逐行读取 JSONL
else:
return json.load(f) # 读取 JSON
在 Web 界面中,LLaMA Factory 也提供了对 JSONL 格式的完整支持:
python展开代码def _load_data_file(file_path: str) -> list[Any]:
with open(file_path, encoding="utf-8") as f:
if file_path.endswith(".json"):
return json.load(f)
elif file_path.endswith(".jsonl"):
return [json.loads(line) for line in f] # 逐行解析 JSONL
else:
return list(f)
对于多模态 VLM 训练,您可以使用以下配置:
json展开代码{
"my_vlm_dataset": {
"file_name": "data.jsonl",
"formatting": "sharegpt",
"columns": {
"messages": "conversations",
"images": "images"
},
"tags": {
"role_tag": "from",
"content_tag": "value",
"user_tag": "human",
"assistant_tag": "gpt"
}
}
}
您的 data.jsonl
文件应该采用以下格式(每行一个完整的 JSON 对象):
jsonl展开代码{"conversations": [{"from": "human", "value": "<image>请描述这张图片..."}, {"from": "gpt", "value": "这是一对银耳环..."}], "images": ["000443568.jpg"]} {"conversations": [{"from": "human", "value": "<image>这是什么?"}, {"from": "gpt", "value": "这是一只猫..."}], "images": ["cat.jpg"]} {"conversations": [{"from": "human", "value": "<image>分析这张图片"}, {"from": "gpt", "value": "这是一张风景照..."}], "images": ["landscape.jpg"]}
LLaMA Factory 使用灵活的字段映射机制,支持自定义字段名和值:
tags
配置自定义字段名本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!