| import random |
| import torch |
| from torch.utils.data import Dataset |
| from datasets import load_dataset |
|
|
| from dataset.common import pre_processing_chat |
|
|
|
|
| class RLAIFDataset(Dataset): |
| def __init__(self, jsonl_path, tokenizer, max_length=1024, thinking_ratio=0.5): |
| super().__init__() |
| self.tokenizer = tokenizer |
| self.max_length = max_length |
| self.thinking_ratio = thinking_ratio |
| self.samples = load_dataset('json', data_files=jsonl_path, split='train') |
| self.bos_id = tokenizer(f'{tokenizer.bos_token}assistant', add_special_tokens=False).input_ids |
| self.eos_id = tokenizer(f'{tokenizer.eos_token}', add_special_tokens=False).input_ids |
|
|
| def __len__(self): |
| return len(self.samples) |
|
|
| def create_chat_prompt(self, conversations): |
| conversations = pre_processing_chat(conversations) |
| use_thinking = random.random() < self.thinking_ratio |
| return self.tokenizer.apply_chat_template( |
| conversations[:-1], |
| tokenize=False, |
| open_thinking=use_thinking, |
| add_generation_prompt=True |
| ) |
|
|
| def __getitem__(self, index): |
| sample = self.samples[index] |
| prompt = self.create_chat_prompt(sample['conversations']) |
|
|
| return { |
| 'prompt': prompt, |
| 'answer': "" |
| } |
|
|