Load the Anthropic Helpful-Harmless dataset from Huggingface and convert it to the necessary format. The dataset is converted to a dictionary with the following structure: { 'prompt1': { 'responses': List[str], 'pairs': List[Tuple[int, int]
(split: str, silent: bool = False, cache_dir: str = None)
| 118 | |
| 119 | |
| 120 | def get_hh(split: str, silent: bool = False, cache_dir: str = None) -> Dict[str, Dict[str, Union[List[Tuple[int, int]], List[str], str]]]: |
| 121 | """Load the Anthropic Helpful-Harmless dataset from Huggingface and convert it to the necessary format. |
| 122 | |
| 123 | The dataset is converted to a dictionary with the following structure: |
| 124 | { |
| 125 | 'prompt1': { |
| 126 | 'responses': List[str], |
| 127 | 'pairs': List[Tuple[int, int]], |
| 128 | 'sft_target': str |
| 129 | }, |
| 130 | 'prompt2': { |
| 131 | ... |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | Prompts should be structured as follows: |
| 136 | \n\nHuman: <prompt>\n\nAssistant: |
| 137 | Multiple turns are allowed, but the prompt should always start with \n\nHuman: and end with \n\nAssistant:. |
| 138 | |
| 139 | For this dataset, the sft_target is just the chosen response. |
| 140 | """ |
| 141 | print(f'Loading HH dataset ({split} split) from Huggingface...') |
| 142 | dataset = datasets.load_dataset('Anthropic/hh-rlhf', split=split, cache_dir=cache_dir) |
| 143 | print('done') |
| 144 | |
| 145 | def split_prompt_and_responses(ex): |
| 146 | prompt = extract_anthropic_prompt(ex['chosen']) |
| 147 | chosen_response = ex['chosen'][len(prompt):] |
| 148 | rejected_response = ex['rejected'][len(prompt):] |
| 149 | return prompt, chosen_response, rejected_response |
| 150 | |
| 151 | data = defaultdict(lambda: defaultdict(list)) |
| 152 | for row in tqdm.tqdm(dataset, desc='Processing HH', disable=silent): |
| 153 | prompt, chosen, rejected = split_prompt_and_responses(row) |
| 154 | responses = [chosen, rejected] |
| 155 | n_responses = len(data[prompt]['responses']) |
| 156 | data[prompt]['pairs'].append((n_responses, n_responses + 1)) |
| 157 | data[prompt]['responses'].extend(responses) |
| 158 | data[prompt]['sft_target'] = chosen |
| 159 | |
| 160 | return data |
| 161 | |
| 162 | |
| 163 | def get_dataset(name: str, split: str, silent: bool = False, cache_dir: str = None): |
no test coverage detected