为了让模型能够有效学习,需要将原始数据转换为结构化形式。大型语言模型(LLM)的微调数据集不仅仅是文本的集合;它是一组精心构建的示例,每个示例都显示了特定的输入-输出行为。这个过程包括定义一个统一的结构,并将其应用于原始信息,以生成清晰、可用于学习的实例。从原始信息到训练实例大多数基于指令的微调数据集的基本单元是输入-输出对。您的目标是将非结构化内容(如文章、文档或日志文件)转换为一系列这样的对。每对都作为一个训练示例。考虑一段关于火星的原始短文:Mars, often called the Red Planet, is the fourth planet from the Sun. Its reddish appearance is due to iron oxide prevalent on its surface. Mars has a thin atmosphere and two small moons, Phobos and Deimos. It is a terrestrial planet with a cold, desert-like surface.从这段文字中,您可以生成多个训练实例,每个实例都针对不同的技能:总结:输入: "总结以下文本:火星,常被称为红色星球,是距离太阳第四远的行星..."输出: "火星是距离太阳第四远的行星,因其表面有氧化铁而被称为红色星球。它拥有稀薄的大气层和两颗卫星,火卫一和火卫二。"问答:输入: "为什么火星被称为红色星球?"输出: "火星被称为红色星球,因为其表面覆盖着氧化铁。"信息提取:输入: "从这句话中提取火星卫星的名称:火星拥有稀薄的大气层和两颗小卫星,火卫一和火卫二。"输出: "火卫一,火卫二"将一段文本转换为离散的、面向任务的示例,是构建定制数据集的要点。这些示例的质量和多样性将直接影响微调模型的最终能力。使用提示模板组织数据为保持一致性,尤其当您的数据包含指令、上下文和特定输入等多个部分时,您应该使用提示模板。模板是一种固定的字符串格式,可将输入的不同部分组织成一个单独的提示。这确保了模型始终以其训练时的相同结构来接收输入,这对实现稳定性能很有帮助。单个数据点的常见结构是包含 instruction、input 和 output 等键的字典。{ "instruction": "Identify the main scientific reason provided.", "input": "The document states that Mars appears red because its surface is rich in iron oxide.", "output": "The main scientific reason is the prevalence of iron oxide on its surface." }您可以使用提示模板将 instruction 和 input 字段组合成一个连贯的提示供模型使用。例如,您可以使用如下模板:下面是一条描述任务的指令,以及提供更多上下文的输入。请写出恰当完成请求的回复。 ### 指令: {instruction} ### 输入: {input} ### 回复:将此模板应用于上面的 JSON 示例,会生成模型在训练期间将处理的最终格式化提示。模型的任务是学习在看到以这种方式开头的提示时,生成 output 字段中的文本。这是一个简单的 Python 函数,用于演示这种格式化:def create_prompt(instruction, input_text): """将指令和输入格式化为标准化的提示。""" prompt_template = """下面是一条描述任务的指令,以及提供更多上下文的输入。请写出恰当完成请求的回复。 ### 指令: {instruction} ### 输入: {input_text} ### 回复:""" return prompt_template.format(instruction=instruction, input_text=input_text) # 示例用法: data_point = { "instruction": "Identify the main scientific reason provided.", "input": "The document states that Mars appears red because its surface is rich in iron oxide.", "output": "The main scientific reason is the prevalence of iron oxide on its surface." } # 模型看到这个组合提示并学习生成输出 formatted_prompt = create_prompt(data_point["instruction"], data_point["input"]) print(formatted_prompt)output 字段不属于输入提示的一部分;它是模型学习生成的目标内容。在训练过程中,formatted_prompt 和 output 的组合构成一个完整的训练示例。数据转换流程整个过程可以看作是一个流程,它将原始的、非结构化信息转换为最终的、可供模型使用的数据集文件。这包括提取有意义的对,并以一致的方式组织它们。digraph G { rankdir=TB; splines=ortho; node [shape=box, style="rounded,filled", fontname="Arial", margin=0.2]; edge [fontname="Arial"]; subgraph cluster_0 { label = "1. 原始数据源"; style=dashed; bgcolor="#e9ecef"; raw_text [label="非结构化文本\n(例如:文章、文档)", shape=note, fillcolor="#f8f9fa"]; } subgraph cluster_1 { label = "2. 提取与结构化"; style=dashed; bgcolor="#e9ecef"; process [label="手动或自动\n提取", shape=cylinder, fillcolor="#d0bfff"]; } subgraph cluster_2 { label = "3. 结构化实例"; style=dashed; bgcolor="#e9ecef"; pair1 [label="{ \"instruction\": \"Summarize...\", \n \"output\": \"...\" }", shape=record, fillcolor="#a5d8ff"]; pair2 [label="{ \"instruction\": \"Extract names...\", \n \"output\": \"...\" }", shape=record, fillcolor="#a5d8ff"]; pair3 [label="{ \"instruction\": \"Why is...\", \n \"output\": \"...\" }", shape=record, fillcolor="#a5d8ff"]; } subgraph cluster_3 { label = "4. 最终数据集文件"; style=dashed; bgcolor="#e9ecef"; jsonl [label="dataset.jsonl", shape=folder, fillcolor="#96f2d7"]; } raw_text -> process; process -> pair1 [label="实例 1"]; process -> pair2 [label="实例 2"]; process -> pair3 [label="实例 n"]; {pair1, pair2, pair3} -> jsonl; }创建定制数据集的流程。原始文本被处理成结构化的指令和输出对,然后保存到最终的数据集文件,例如 JSON Lines 格式。使用 JSON Lines 序列化数据集有了结构化数据点列表(例如 Python 字典列表)后,您需要将它们保存到文件中。虽然包含单个大列表的标准 JSON 文件是一种选择,但它不适合大型数据集。JSON Lines(或 .jsonl)是一种更好的格式。在 JSON Lines 文件中,每一行都是一个独立的、自包含的 JSON 对象。标准 JSON (data.json):[ { "instruction": "Summarize the text.", "input": "Mars is a cold, desert-like planet...", "output": "Mars is a cold planet with a desert environment." }, { "instruction": "What are the moons of Mars?", "input": "The two moons of Mars are Phobos and Deimos.", "output": "Phobos and Deimos." } ]JSON Lines (data.jsonl):{"instruction": "Summarize the text.", "input": "Mars is a cold, desert-like planet...", "output": "Mars is a cold planet with a desert environment."} {"instruction": "What are the moons of Mars?", "input": "The two moons of Mars are Phobos and Deimos.", "output": "Phobos and Deimos."}JSON Lines 为机器学习流程提供多项优势:流式处理: 您可以逐行读取和处理文件,而无需将整个数据集加载到内存中。这对于几 GB 大小的数据集来说效率很高。可追加: 您可以通过在文件末尾追加新行来轻松添加新示例。可靠性: 如果文件写入过程被中断,您不会损坏整个文件,只会可能损坏最后写入的行。以下是在 Python 中将结构化数据写入 .jsonl 文件的方法:import json # 假设 'structured_data' 是一个字典列表 structured_data = [ { "instruction": "Summarize the text.", "input": "Mars is a cold, desert-like planet...", "output": "Mars is a cold planet with a desert environment." }, { "instruction": "What are the moons of Mars?", "input": "The two moons of Mars are Phobos and Deimos.", "output": "Phobos and Deimos." } ] file_path = "my_dataset.jsonl" with open(file_path, 'w') as f: for entry in structured_data: json_record = json.dumps(entry) f.write(json_record + '\n') print(f"数据集已保存到 {file_path}")通过遵循这些步骤,您可以将最初收集的原始信息转换为一个整洁、结构化的数据集。此文件现已准备好进行最后的准备步骤——分词,在此步骤中文本将被转换为模型可以直接处理的数字格式。