趋近智
为了让模型能够有效学习,需要将原始数据转换为结构化形式。大型语言模型(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 的组合构成一个完整的训练示例。
整个过程可以看作是一个流程,它将原始的、非结构化信息转换为最终的、可供模型使用的数据集文件。这包括提取有意义的对,并以一致的方式组织它们。
创建定制数据集的流程。原始文本被处理成结构化的指令和输出对,然后保存到最终的数据集文件,例如 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 为机器学习流程提供多项优势:
以下是在 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}")
通过遵循这些步骤,您可以将最初收集的原始信息转换为一个整洁、结构化的数据集。此文件现已准备好进行最后的准备步骤——分词,在此步骤中文本将被转换为模型可以直接处理的数字格式。
简洁的语法。内置调试功能。从第一天起就可投入生产。
为 ApX 背后的 AI 系统而构建
这部分内容有帮助吗?
© 2026 ApX Machine Learning用心打造