2023年5月17日 可用,后续未知。
我们学了论语,学了唐诗,可以吟诗作赋,那样AI怎么学?这是一个可行方案。
本文也是这几个项目的详细过程,供后人参考。
https://huggingface.co/TMZN/ChatGLM-wyw https://github.com/tmzncty/ChatGLM-wyw https://huggingface.co/datasets/TMZN/lunyu
文件结构

首先,我们准备一本想要的炼的书籍,尽可能以对话体为主,所以我选择了最经典的论语。

全文很容易找到,这里也就不放出了。
然后呢,论语很多时候是一个人说(子曰),所以我们需要手动找出完整对话场景用于数据集。使用
https://github.com/huang1332/finetune_dataset_maker
作为数据集生成器。(其实还有直接转换的方案,但是不完整的对话还是别塞进去比较好。)

然后可以得出类似下图的数据集(后面我发现不对劲。)

这个格式即使是能参加后续的转换变成炼丹可用数据集,也是会丢失信息的。(只有问题没有回答)
最终,我调整了一下生成的格式,变成

[
{
"instruction": "问题:曰:贫而无谄富而无骄何如?",
"input": "",
"output": "可也、未若贫而乐富而好礼者也、"
},
{
"instruction": "问题:曰:夫子至于是邦也必闻其政求之与?抑与之与?",
"input": "",
"output": "夫子温、良、恭、俭、让以得之、夫子之求之也其诸异乎人之求之与?"
},
{
"instruction": "问题:何谓四恶?",
"input": "",
"output": "不教而杀谓之虐;不戒视成谓之暴;慢令致期谓之贼;犹之与人也出纳之吝谓之有司、"
}
] 这里贴出可以转换的格式,给大家作为参考。(实际上我应该把问题这两个字去掉的。)
然后运行转换数据集.bat或者
import argparse
import json
from tqdm import tqdm
def format_example(example: dict) -> dict:
context = f"Instruction: {example['instruction']}\n"
if example.get("input"):
context += f"Input: {example['input']}\n"
context += "Answer: "
target = example["output"]
return {"context": context, "target": target}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data_path", type=str, default="./answers-lunyu.json")
parser.add_argument("--save_path", type=str, default="./answers-lunyu.jsonl")
args = parser.parse_args()
with open(args.data_path,encoding='utf-8') as f:
examples = json.load(f)
with open(args.save_path, 'w', encoding='utf-8') as f:
for example in tqdm(examples, desc="formatting.."):
f.write(json.dumps(format_example(example)) + '\n')
if __name__ == "__main__":
main()
进行转换,路径自己改一下就行。
接着改个文件名变成ansower.jsonl
标记化数据集后直接开始训练即可。
等待跑完,再打开web即可。
训练和推理都需要15-17G显存,如果不够,请采用量化或者各种加载方式。

尽可能学会正则,用GPT3.5辅助,不然后续数据很难处理。(你肯定是不愿意手敲的。)
至于MDX怎么塞进去,也是类似的思路,但是涉及版权。
测试结果(莫名喜感)


