用于RLHF的人类偏好收集
该项目将帮助您通过收集对比数据来建立人类对监督模型生成响应的偏好,从而将您的LLM提升至ChatGPT质量水平。
通过根据质量对多个响应进行排序,您可以训练一个能有效捕捉人类偏好的奖励模型。该奖励模型在强化学习中扮演关键角色,可优化微调后的基础模型的性能。
延伸阅读与资源
- Gathering Human Feedback Tutorial 一个Jupyter Notebook教程,将引导您逐步完成收集对比数据、建立人类偏好并将这些反馈整合到奖励模型训练中的过程。
- RLHF 资源: 收集了关于如何收集数据并构建端到端的人类反馈强化学习(RLHF)系统来微调生成式AI模型的相关链接、教程和最佳实践。
- Awesome-Human-in-the-Loop List: 一个关于人机协同模型再训练的优质资源与参考文献列表。
- Talk: Improving Machine Learning from Human Feedback : 来自PyData柏林会议的一场演讲,探讨如何利用RLHF技术通过人类反馈改进机器学习。
- Workshop: Getting Started with Reinforcement Learning : 关于如何开始强化学习的研讨会。
- 指南:五大可立即微调的大型语言模型
如何收集数据集
RLHF的数据集包含两个部分:
- 输入提示
- 每个提示对应的备选生成回复。
为了简化人工标注者的任务,建议每个提示提供2个响应选项以供选择。
从一组初始提示和响应开始,其中每个项目都是具有以下结构的JSON对象:
[{
"prompt": "The quick brown fox...",
"answer1": "jumps over the lazy dog.",
"answer2": "bags few lynx."
}, ...]
收集示例可以通过手动生成,或者使用您的基线模型生成多个备选假设。
当您的数据集开始收集到dataset.json文件后,创建一个项目并将数据集上传到Label Studio。
启动您的标注项目
- 在Label Studio中创建新项目
- 前往
Settings > Labeling Interface > Browse Templates > Generative AI > Human Preference collection for RLHF - 保存项目
导入数据集
使用Python SDK,您可以将带有输入提示的数据集导入Label Studio。使用您刚创建的项目的PROJECT_ID,运行以下代码:
from label_studio_sdk import Client
ls = Client(url='<YOUR-LABEL-STUDIO-URL>', api_key='<YOUR-API_KEY>')
project = ls.get_project(id=PROJECT_ID)
project.import_tasks('dataset.json')
然后您可以通过创建响应来开始标注数据集。
如何配置标注界面
Human Preference collection for RLHF 模板包含以下XML格式的标注界面:
<View className="root">
<Style>
.root {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Roboto',
sans-serif;
line-height: 1.6;
background-color: #f0f0f0;
}
.container {
margin: 0 auto;
padding: 20px;
background-color: #ffffff;
border-radius: 5px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.1), 0 6px 20px 0 rgba(0, 0, 0, 0.1);
}
.prompt {
padding: 20px;
background-color: #0084ff;
color: #ffffff;
border-radius: 5px;
margin-bottom: 20px;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1), 0 3px 10px 0 rgba(0, 0, 0, 0.1);
}
.answers {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
gap: 20px;
}
.answer-box {
flex-basis: 49%;
padding: 20px;
background-color: rgba(44, 62, 80, 0.9);
color: #ffffff;
border-radius: 5px;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1), 0 3px 10px 0 rgba(0, 0, 0, 0.1);
}
.answer-box p {
word-wrap: break-word;
}
.answer-box:hover {
background-color: rgba(52, 73, 94, 0.9);
cursor: pointer;
transition: all 0.3s ease;
}
.lsf-richtext__line:hover {
background: unset;
}
.answer-box .lsf-object {
padding: 20px
}
</Style>
<View className="container">
<View className="prompt">
<Text name="prompt" value="$prompt" />
</View>
<View className="answers">
<Pairwise name="comparison" toName="answer1,answer2"
selectionStyle="background-color: #27ae60; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.2); border: 2px solid #2ecc71; cursor: pointer; transition: all 0.3s ease;" />
<View className="answer-box">
<Text name="answer1" value="$answer1" />
</View>
<View className="answer-box">
<Text name="answer2" value="$answer2" />
</View>
</View>
</View>
</View>
<!--{ "data" : {
"prompt": "What are the key benefits of using Reinforcement Learning from Human Feedback (RLHF) for dataset collection in the context of Large Language Model (LLM) generation?",
"answer1": "Reinforcement Learning from Human Feedback (RLHF) for dataset collection in Large Language Model (LLM) generation provides key benefits such as improved model performance through direct optimization, better alignment with human values by incorporating human feedback, and the ability to iteratively refine the model based on user interactions, resulting in a more user-friendly and efficient language model.",
"answer2": "Using Reinforcement Learning from Human Feedback (RLHF) for dataset collection in Large Language Model (LLM) generation offers advantages such as enhanced model capabilities by optimizing for desired outcomes, greater adaptability to human preferences through the inclusion of human feedback, and the opportunity to continuously improve the model based on user experiences, ultimately leading to a more effective and responsive language model."
}}
-->
此外,您可以在XML注释部分修改"prompt"、$answer1和$answer2,查看它们与您的数据配合的效果。
导出数据集
根据问题陈述的复杂程度,通常需要标注数百到数千个任务才能完成对LLM的微调。
在标注足够多的任务后,您可以将数据集导出为以下原始Label Studio JSON格式:
[
{
"id": 1,
"data": {
"prompt": "Generate a Python function that takes a list of integers as input and returns the sum of all even numbers in the list."
},
"annotations": [
{
"id": 1,
"created_at": "2021-03-03T14:00:00.000000Z",
"result": [
{
"from_name": "instruction",
"to_name": "prompt",
"type": "textarea",
"value": {
"text": [
"def sum_even_numbers(numbers):\n return sum([n for n in numbers if n % 2 == 0])"
]
}
}
],
// other fields
以上显示的是带有标注的任务列表。每个任务都有一个包含输入提示的data.prompt字段,每个"annotations"条目下都包含一个位于result.value.text字段中的响应结果。
您可以为每个任务创建多个标注。
或者,您可以下载相同数据的CSV格式:
prompt,instruction
"Generate...","def sum..."
如何微调模型
您生成的示例可用于微调开源LLM模型,如GPT-2、T5、Falcon、LLaMa等。您可以在HuggingFace LLM排行榜上查看完整的模型列表,并从Model Hub下载和微调模型。 另外,还有一些可用的微调服务:OpenAI、CoHere、(AI21 Studio)[https://www.ai21.com/studio/foundation-models]、MosaicML、Google Cloud AI Platform、AzureML等。