Preprocess input into messages format and calculate max output length. Args: input: Input prompt as string or PromptList max_out_len: Maximum output length max_seq_len: Maximum sequence length mode: The method of input truncation
(
self,
input: Union[str, PromptList],
max_out_len: int,
max_seq_len: int,
mode: str,
get_token_len_func,
)
| 478 | return prompt |
| 479 | |
| 480 | def _preprocess_messages( |
| 481 | self, |
| 482 | input: Union[str, PromptList], |
| 483 | max_out_len: int, |
| 484 | max_seq_len: int, |
| 485 | mode: str, |
| 486 | get_token_len_func, |
| 487 | ) -> tuple[List[Dict], int]: |
| 488 | """Preprocess input into messages format and calculate max output |
| 489 | length. |
| 490 | |
| 491 | Args: |
| 492 | input: Input prompt as string or PromptList |
| 493 | max_out_len: Maximum output length |
| 494 | max_seq_len: Maximum sequence length |
| 495 | mode: The method of input truncation |
| 496 | get_token_len_func: Function to calculate token length |
| 497 | |
| 498 | Returns: |
| 499 | tuple: (processed messages list, adjusted max_out_len) |
| 500 | """ |
| 501 | # Check input length when mode is 'none' |
| 502 | if mode == 'none': |
| 503 | input_len = (get_token_len_func(input) if isinstance( |
| 504 | input, str) else sum( |
| 505 | get_token_len_func(item['prompt']) for item in input)) |
| 506 | if input_len > max_seq_len: |
| 507 | raise ValueError( |
| 508 | f'Input length ({input_len}) exceeds max_seq_len ' |
| 509 | f'({max_seq_len}) and mode is set to "none". Please ' |
| 510 | f'either change the mode or increase the max_seq_len.') |
| 511 | |
| 512 | # Trim input if needed |
| 513 | def bin_trim_wrapper(text): |
| 514 | trim_length = max_seq_len - 100 |
| 515 | if max_out_len is not None: |
| 516 | trim_length -= max_out_len |
| 517 | return self._bin_trim(text, trim_length, mode) |
| 518 | |
| 519 | if isinstance(input, str) and mode != 'none': |
| 520 | input = bin_trim_wrapper(input) |
| 521 | # Convert input to messages format |
| 522 | if isinstance(input, str): |
| 523 | messages = [{'role': 'user', 'content': input}] |
| 524 | input_len = get_token_len_func(input) |
| 525 | else: |
| 526 | messages = [] |
| 527 | processed_prompts = [] |
| 528 | for item in input: |
| 529 | input_content = item['prompt'] |
| 530 | if mode != 'none': |
| 531 | input_content = bin_trim_wrapper(input_content) |
| 532 | processed_prompts.append(input_content) |
| 533 | msg = {'content': input_content} |
| 534 | if item['role'] == 'HUMAN': |
| 535 | msg['role'] = 'user' |
| 536 | elif item['role'] == 'BOT': |
| 537 | msg['role'] = 'assistant' |