Parse a prompt template, and wrap it with meta template if applicable. Args: prompt_template (List[PromptType]): A prompt template (potentially before being wrapped by meta template). mode (str): Parsing mode. Choices are 'ppl' and 'gen'.
(self, prompt_template: PromptType, mode: str)
| 298 | key] = self._encode_speical_tokens(value) |
| 299 | |
| 300 | def parse_template(self, prompt_template: PromptType, mode: str) -> str: |
| 301 | """Parse a prompt template, and wrap it with meta template if |
| 302 | applicable. |
| 303 | |
| 304 | Args: |
| 305 | prompt_template (List[PromptType]): A prompt |
| 306 | template (potentially before being wrapped by meta template). |
| 307 | mode (str): Parsing mode. Choices are 'ppl' and 'gen'. |
| 308 | |
| 309 | Returns: |
| 310 | str: The final string. |
| 311 | """ |
| 312 | assert isinstance(prompt_template, (str, list, PromptList, tuple)) |
| 313 | if not isinstance(prompt_template, (str, PromptList)): |
| 314 | return [self.parse_template(p, mode=mode) for p in prompt_template] |
| 315 | |
| 316 | assert mode in ['ppl', 'gen'] |
| 317 | if isinstance(prompt_template, str): |
| 318 | return prompt_template |
| 319 | if self.meta_template: |
| 320 | |
| 321 | prompt = '' |
| 322 | # Whether to keep generating the prompt |
| 323 | generate = True |
| 324 | |
| 325 | section_stack = [] # stores tuples: (section_name, start_idx) |
| 326 | |
| 327 | for i, item in enumerate(prompt_template): |
| 328 | if not generate: |
| 329 | break |
| 330 | if isinstance(item, str): |
| 331 | prompt += item |
| 332 | elif isinstance(item, dict) and 'section' in item: |
| 333 | if item['pos'] == 'end': |
| 334 | section_name, start_idx = section_stack.pop(-1) |
| 335 | assert section_name == item['section'] |
| 336 | if section_name in ['round', 'ice']: |
| 337 | dialogue = prompt_template[start_idx:i] |
| 338 | round_ranges = self._split_rounds( |
| 339 | dialogue, self.meta_template['round']) |
| 340 | # Consider inserting multiple round examples into |
| 341 | # template |
| 342 | for i in range(len(round_ranges) - 1): |
| 343 | start = round_ranges[i] |
| 344 | end = round_ranges[i + 1] |
| 345 | round_template = dialogue[start:end] |
| 346 | role_dict = self._update_role_dict( |
| 347 | round_template) |
| 348 | new_str, generate = self._prompt2str( |
| 349 | self.meta_template['round'], |
| 350 | role_dict, |
| 351 | # Start generating only when the mode is in |
| 352 | # generation and the template reaches the |
| 353 | # last round |
| 354 | for_gen=mode == 'gen' |
| 355 | and i == len(round_ranges) - 2 |
| 356 | and section_name == 'round') |
| 357 | prompt += new_str |