| 251 | |
| 252 | |
| 253 | class PromptWrapper(): |
| 254 | def __init__( |
| 255 | self, |
| 256 | tokenizer, |
| 257 | instruction_template, |
| 258 | conv_collater, |
| 259 | use_cot=False |
| 260 | ): |
| 261 | |
| 262 | self.instruction_template = instruction_template |
| 263 | |
| 264 | self.question_template = self.get_question_template(use_cot=use_cot) |
| 265 | |
| 266 | if '{fewshot_examples}' in self.instruction_template: |
| 267 | # use fewshot examples |
| 268 | # keep the fewshot placeholder, since examples are sample-specific |
| 269 | self.input_template = self.instruction_template.format(instruction=self.question_template, fewshot_examples='{fewshot_examples}') |
| 270 | else: |
| 271 | self.input_template = self.instruction_template.format(instruction=self.question_template) |
| 272 | |
| 273 | self.conv_collater = conv_collater # for multi-turn QA only, implemented for each model |
| 274 | self.tokenizer = tokenizer |
| 275 | |
| 276 | |
| 277 | def get_system_template(self, t): |
| 278 | if t.strip() == '': |
| 279 | return '{instruction}' |
| 280 | else: |
| 281 | try: |
| 282 | t.format(instruction='') |
| 283 | except: |
| 284 | raise Exception('there must be a {instruction} placeholder in the system template') |
| 285 | return t |
| 286 | |
| 287 | def get_question_template(self, use_cot): |
| 288 | if use_cot: |
| 289 | return "以下是中国{exam_type}中{exam_class}考试的一道{question_type},请分析每个选项,并最后给出答案。\n{question}\n{option_str}" |
| 290 | else: |
| 291 | return "以下是中国{exam_type}中{exam_class}考试的一道{question_type},不需要做任何分析和解释,直接输出答案选项。\n{question}\n{option_str}" |
| 292 | |
| 293 | def wrap(self, data: list[dict]): |
| 294 | ''' |
| 295 | data.keys(): ['id', 'exam_type', 'exam_class', 'question_type', 'question', 'option']. These are the raw data. |
| 296 | We still need 'option_str'. |
| 297 | ''' |
| 298 | res = [] |
| 299 | lines = [] |
| 300 | for line in data: |
| 301 | line["option_str"] = "\n".join( |
| 302 | [f"{k}. {v}" for k, v in line["option"].items() if len(v) > 1] |
| 303 | ) |
| 304 | query = self.input_template.format_map(line) |
| 305 | line['query'] = query |
| 306 | |
| 307 | res.append(query) |
| 308 | lines.append(line) |
| 309 | |
| 310 | return res, lines |