| 36 | return self.tokenizer.decode(t) |
| 37 | |
| 38 | class SidSFTDataset(Dataset): |
| 39 | def __init__(self, train_file, tokenizer, max_len=2048, sample=-1, test=False, seed=0, category="", K=4, dedup=False): |
| 40 | self.data = pd.read_csv(train_file) |
| 41 | random.seed(seed) |
| 42 | |
| 43 | if sample > 0: |
| 44 | self.data = self.data.sample(sample, random_state=seed) |
| 45 | self.tokenizer = Tokenizer(tokenizer) |
| 46 | self.test = test |
| 47 | self.max_len = max_len |
| 48 | self.category = category |
| 49 | self.dedup = dedup |
| 50 | self.get_inputs() |
| 51 | |
| 52 | def __len__(self): |
| 53 | return len(self.data) |
| 54 | |
| 55 | def generate_prompt(self, data_point): |
| 56 | return f"""### User Input: |
| 57 | {data_point["input"]} |
| 58 | |
| 59 | ### Response:\n{data_point["output"]}""" |
| 60 | |
| 61 | def get_history(self, row): |
| 62 | row['history_item_sid'] = eval(row['history_item_sid']) |
| 63 | L = len(row['history_item_sid']) |
| 64 | history = "" |
| 65 | history_str = ", ".join(row["history_item_sid"]) |
| 66 | for i in range(L): |
| 67 | if i == 0: |
| 68 | history += row['history_item_sid'][i] |
| 69 | else: |
| 70 | history += ", " + row['history_item_sid'][i] |
| 71 | target_item = str(row['item_sid']) |
| 72 | target_item_sid = row["item_sid"] |
| 73 | last_history_item_sid = row['history_item_sid'][-1] if row['history_item_sid'] else None |
| 74 | return {"input": f"The user has interacted with items {history} in chronological order. Can you predict the next possible item that the user may expect?", |
| 75 | "output": target_item + "\n", |
| 76 | "history_str": history_str, |
| 77 | "dedup": target_item_sid == last_history_item_sid} |
| 78 | |
| 79 | def pre(self, idx): |
| 80 | instruction = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. |
| 81 | |
| 82 | ### Instruction: |
| 83 | Can you predict the next possible item that the user may expect? |
| 84 | |
| 85 | """ |
| 86 | tokens = self.tokenizer.encode(instruction, bos=True, eos=False) |
| 87 | |
| 88 | history = self.get_history(self.data.iloc[idx]) |
| 89 | # print("**********************") |
| 90 | # print("history: ", history) |
| 91 | target_item = history['output'] |
| 92 | history['output'] = '' |
| 93 | negative_prompt_ids = copy.deepcopy(tokens) |
| 94 | |
| 95 | prompt = self.generate_prompt(history) |