生成偏好数据集¶
- 目标:为 DPO/ORPO 生成合成偏好数据集。
- 库:argilla, hf-inference-endpoints
- 组件:LoadDataFromHub, TextGeneration, UltraFeedback, GroupColumns, FormatTextGenerationDPO, PreferenceToArgilla, InferenceEndpointsLLM
开始使用¶
安装依赖项¶
要完成本教程,您需要通过 pip 安装 distilabel SDK 和一些第三方库。在本教程中,我们将使用免费但速率受限的 Hugging Face 无服务器推理 API,因此我们需要将其作为额外的 distilabel 依赖项安装。您可以通过运行以下命令来安装它们
让我们进行所需的导入
您需要一个 HF_TOKEN
才能使用 HF Inference Endpoints。登录以在本笔记本中直接使用它。
定义 Pipeline¶
为了生成我们的偏好数据集,我们将需要定义一个包含所有必要步骤的 Pipeline
。下面,我们将详细介绍每个步骤。
加载数据集¶
我们将使用来自 Hugging Face Hub 的 argilla/10Kprompts-mini
数据集作为源数据。
- 组件:
LoadDataFromHub
- 输入列:
instruction
和topic
,与加载的数据集中的相同 - 输出列:
instruction
和topic
生成响应¶
我们需要为给定的指令生成响应。我们将使用 Hugging Face Hub 上通过 Serverless Inference API 提供的两个不同的模型:meta-llama/Meta-Llama-3-8B-Instruct
和 mistralai/Mixtral-8x7B-Instruct-v0.1
。我们还将指示每个模型的生成参数。
- 组件:使用
InferenceEndpointsLLM
的 LLM 的TextGeneration
任务 - 输入列:
instruction
- 输出列:每个模型的
generation
、distilabel_metadata
、model_name
对于您的用例并为了改进结果,您可以使用您选择的任何其他 LLM。
generate_responses = [
TextGeneration(
llm=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3-8B-Instruct",
tokenizer_id="meta-llama/Meta-Llama-3-8B-Instruct",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
),
pipeline=Pipeline(name="showcase-pipeline"),
),
TextGeneration(
llm=InferenceEndpointsLLM(
model_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
tokenizer_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
),
pipeline=Pipeline(name="showcase-pipeline"),
),
]
for task in generate_responses:
task.load()
print(next(task.process([{"instruction": "Which are the top cities in Spain?"}])))
分组响应¶
评估响应的任务需要将生成列表作为输入。但是,每个模型响应都保存在子集 text_generation_0
和 text_generation_1
的 generation 列中。我们将把这两列合并为单列和 default
子集。
- 组件:
GroupColumns
- 输入列:来自
text_generation_0
和text_generation_1
的generation
和model_name
- 输出列:
generations
和model_names
group_responses = GroupColumns(
columns=["generation", "model_name"],
output_columns=["generations", "model_names"],
pipeline=Pipeline(name="showcase-pipeline"),
)
next(
group_responses.process(
[
{
"generation": "Madrid",
"model_name": "meta-llama/Meta-Llama-3-8B-Instruct",
},
],
[
{
"generation": "Barcelona",
"model_name": "mistralai/Mixtral-8x7B-Instruct-v0.1",
}
],
)
)
评估响应¶
为了构建我们的偏好数据集,我们需要评估模型生成的响应。我们将使用 meta-llama/Meta-Llama-3-70B-Instruct
来执行此操作,应用 UltraFeedback
任务,该任务根据不同的维度(帮助性、诚实性、指令遵循、真实性)判断响应。
- 组件:使用
InferenceEndpointsLLM
的 LLM 的UltraFeedback
任务 - 输入列:
instruction
、generations
- 输出列:
ratings
、rationales
、distilabel_metadata
、model_name
对于您的用例并为了改进结果,您可以使用您选择的任何其他 LLM。
evaluate_responses = UltraFeedback(
aspect="overall-rating",
llm=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3-70B-Instruct",
tokenizer_id="meta-llama/Meta-Llama-3-70B-Instruct",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
),
pipeline=Pipeline(name="showcase-pipeline"),
)
evaluate_responses.load()
next(
evaluate_responses.process(
[
{
"instruction": "What's the capital of Spain?",
"generations": ["Madrid", "Barcelona"],
}
]
)
)
转换为偏好数据集¶
- 您可以使用
chosen
和rejected
列自动将其转换为偏好数据集。- 组件:
FormatTextGenerationDPO
步骤 - 输入列:
instruction
、generations
、generation_models
、ratings
- 输出列:
prompt
、prompt_id
、chosen
、chosen_model
、chosen_rating
、rejected
、rejected_model
、rejected_rating
- 组件:
format_dpo = FormatTextGenerationDPO(pipeline=Pipeline(name="showcase-pipeline"))
format_dpo.load()
next(
format_dpo.process(
[
{
"instruction": "What's the capital of Spain?",
"generations": ["Madrid", "Barcelona"],
"generation_models": [
"Meta-Llama-3-8B-Instruct",
"Mixtral-8x7B-Instruct-v0.1",
],
"ratings": [5, 1],
}
]
)
)
- 或者,您可以使用 Argilla 手动标记数据并将其转换为偏好数据集。
- 组件:
PreferenceToArgilla
步骤 - 输入列:
instruction
、generations
、generation_models
、ratings
- 输出列:
instruction
、generations
、generation_models
、ratings
- 组件:
运行 Pipeline¶
下面,您可以看到完整的 Pipeline 定义
with Pipeline(name="generate-dataset") as pipeline:
load_dataset = LoadDataFromHub(repo_id="argilla/10Kprompts-mini")
generate_responses = [
TextGeneration(
llm=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3-8B-Instruct",
tokenizer_id="meta-llama/Meta-Llama-3-8B-Instruct",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
)
),
TextGeneration(
llm=InferenceEndpointsLLM(
model_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
tokenizer_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
)
),
]
group_responses = GroupColumns(
columns=["generation", "model_name"],
output_columns=["generations", "model_names"],
)
evaluate_responses = UltraFeedback(
aspect="overall-rating",
llm=InferenceEndpointsLLM(
model_id="meta-llama/Meta-Llama-3-70B-Instruct",
tokenizer_id="meta-llama/Meta-Llama-3-70B-Instruct",
generation_kwargs={"max_new_tokens": 512, "temperature": 0.7},
)
)
format_dpo = FormatTextGenerationDPO()
to_argilla = PreferenceToArgilla(
dataset_name="preference-dataset",
dataset_workspace="argilla",
api_url="https://[your-owner-name]-[your-space-name].hf.space",
api_key="[your-api-key]",
num_generations=2
)
for task in generate_responses:
load_dataset.connect(task)
task.connect(group_responses)
group_responses.connect(evaluate_responses)
evaluate_responses.connect(format_dpo, to_argilla)
现在让我们运行 Pipeline 并生成偏好数据集。
让我们检查偏好数据集!如果您已将数据加载到 Argilla,则可以在 Argilla UI 中开始注释。
您可以将数据集推送到 Hub 以与社区共享,并嵌入它以探索数据。
结论¶
在本教程中,我们展示了使用 distilabel 构建用于生成偏好数据集的 Pipeline 的详细步骤。您可以为自己的用例自定义此 Pipeline,并通过 Hugging Face Hub 与社区共享您的数据集,或使用它们来训练 DPO 或 ORPO 模型。
我们使用包含提示的数据集,通过无服务器 Hugging Face Inference API 使用两个不同的模型生成响应。接下来,我们使用第三个模型评估响应,遵循 UltraFeedback 标准。最后,我们将数据转换为偏好数据集,并使用 Argilla 进行进一步的策展。