Load the StackExchange dataset from Huggingface, and return a dict of prompts and responses. See get_hh for the format. We strip the HTML tags from the responses (except for tags), and we add necessary newlines.
(split, silent=False, cache_dir: str = None)
| 44 | |
| 45 | |
| 46 | def get_se(split, silent=False, cache_dir: str = None) -> Dict[str, Dict[str, Union[List[Tuple[int, int]], List[str], str]]]: |
| 47 | """Load the StackExchange dataset from Huggingface, and return a dict of prompts and responses. See get_hh for the format. |
| 48 | |
| 49 | We strip the HTML tags from the responses (except for <code> tags), and we add necessary newlines. |
| 50 | """ |
| 51 | print(f'Loading SE dataset ({split} split) from Huggingface...') |
| 52 | dataset = datasets.load_dataset('HuggingFaceH4/stack-exchange-preferences', cache_dir=cache_dir)['train'] |
| 53 | print('done') |
| 54 | |
| 55 | # shuffle the dataset and select 1% for test |
| 56 | dataset = dataset.shuffle(seed=42) |
| 57 | dataset = dataset.select(range(int(len(dataset) * 0.01))) if split == 'test' else dataset.select( |
| 58 | range(int(len(dataset) * 0.01), len(dataset))) |
| 59 | |
| 60 | def strip_html(x): |
| 61 | x['question'] = strip_html_tags(x['question']) |
| 62 | for a in x['answers']: |
| 63 | a['text'] = strip_html_tags(a['text']) |
| 64 | return x |
| 65 | |
| 66 | dataset = dataset.map(strip_html, num_proc=64) |
| 67 | |
| 68 | data = defaultdict(dict) |
| 69 | for row in tqdm.tqdm(dataset, desc='Processing SE', disable=silent): |
| 70 | prompt = '\n\nHuman: ' + row['question'] + '\n\nAssistant:' |
| 71 | responses = [' ' + a['text'] for a in row['answers']] |
| 72 | scores = [a['pm_score'] for a in row['answers']] |
| 73 | |
| 74 | pairs = [] |
| 75 | for i in range(len(responses)): |
| 76 | for j in range(i + 1, len(responses)): |
| 77 | pairs.append((i, j) if scores[i] > scores[j] else (j, i)) |
| 78 | |
| 79 | data[prompt]['responses'] = responses |
| 80 | data[prompt]['pairs'] = pairs |
| 81 | data[prompt]['sft_target'] = max(responses, key=lambda x: scores[responses.index(x)]) |
| 82 | |
| 83 | return data |
| 84 | |
| 85 | def get_shp(split: str, silent: bool = False, cache_dir: str = None) -> Dict[str, Dict[str, Union[List[Tuple[int, int]], List[str], str]]]: |
| 86 | """Load the Stanford Human Preferences dataset from Huggingface and convert it to the necessary format. See hh for the format. |