Preprocess the data by tokenizing. Args: sources (List[str]): a list of source strings targets (List[str]): a list of target strings tokenizer (Tokenizer): a tokenizer object used for tokenization Returns: dict: a dictionary co
(self, examples)
| 122 | return self.gpt_convert_example_to_feature(raw_sample) |
| 123 | |
| 124 | def preprocess(self, examples): |
| 125 | """ |
| 126 | Preprocess the data by tokenizing. |
| 127 | Args: |
| 128 | sources (List[str]): a list of source strings |
| 129 | targets (List[str]): a list of target strings |
| 130 | tokenizer (Tokenizer): a tokenizer object used for tokenization |
| 131 | Returns: |
| 132 | dict: a dictionary containing the input_ids and labels for the examples |
| 133 | """ |
| 134 | |
| 135 | prompt_input, prompt_no_input = PROMPT_DICT[ |
| 136 | 'prompt_input'], PROMPT_DICT['prompt_no_input'] |
| 137 | |
| 138 | sources = [] |
| 139 | if 'input' not in examples: |
| 140 | if 'instruction' in examples: |
| 141 | for instruction in examples['instruction']: |
| 142 | sources.append(prompt_no_input.format_map({"instruction": instruction})) |
| 143 | elif 'query' in examples: |
| 144 | for query in examples['query']: |
| 145 | sources.append(prompt_no_input.format_map({"instruction": query})) |
| 146 | else: |
| 147 | if 'instruction' in examples: |
| 148 | for instruction, minput in zip(examples['instruction'], examples['input']): |
| 149 | sources.append(prompt_input.format_map({"instruction": instruction, "input": minput})) |
| 150 | elif 'query' in examples: |
| 151 | for query, minput in zip(examples['query'], examples['input']): |
| 152 | sources.append(prompt_input.format_map({"instruction": query, "input": minput})) |
| 153 | |
| 154 | if 'output' in examples: |
| 155 | key = 'output' |
| 156 | elif 'content' in examples: |
| 157 | key = 'content' |
| 158 | elif 'response' in examples: |
| 159 | key = 'response' |
| 160 | |
| 161 | targets = [ |
| 162 | example + self.tokenizer.eos_token |
| 163 | for example in examples[key] |
| 164 | ] |
| 165 | |
| 166 | examples_raw = [s + t for s, t in zip(sources, targets)] |
| 167 | examples_tokenized, sources_tokenized = [ |
| 168 | self.tokenize(strings, self.tokenizer) |
| 169 | for strings in (examples_raw, sources) |
| 170 | ] |
| 171 | input_ids = examples_tokenized['input_ids'] |
| 172 | labels = copy.deepcopy(input_ids) |
| 173 | for label, source_len in zip(labels, |
| 174 | sources_tokenized['input_ids_lens']): |
| 175 | label[:source_len] = self.IGNORE_INDEX |
| 176 | return dict(input_ids=input_ids, labels=labels) |
| 177 | |
| 178 | def tokenize(self, strings, tokenizer): |
| 179 | """ |