| 395 | |
| 396 | |
| 397 | class SidSFTDataset(CSVBaseDataset): |
| 398 | def __init__(self, train_file, tokenizer, max_len=2048, sample=-1, test=False, seed=0, category="", K=4, dedup=False): |
| 399 | super().__init__(train_file, sample, seed, max_len, category, dedup, tokenizer, test) |
| 400 | |
| 401 | self.get_inputs() |
| 402 | |
| 403 | def get_history(self, row): |
| 404 | row['history_item_sid'] = eval(row['history_item_sid']) |
| 405 | L = len(row['history_item_sid']) |
| 406 | history = "" |
| 407 | history_str = ", ".join(row["history_item_sid"]) |
| 408 | for i in range(L): |
| 409 | if i == 0: |
| 410 | history += row['history_item_sid'][i] |
| 411 | else: |
| 412 | history += ", " + row['history_item_sid'][i] |
| 413 | target_item = str(row['item_sid']) |
| 414 | target_item_sid = row["item_sid"] |
| 415 | last_history_item_sid = row['history_item_sid'][-1] if row['history_item_sid'] else None |
| 416 | 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?", |
| 417 | "output": target_item + "\n", |
| 418 | "history_str": history_str, |
| 419 | "dedup": target_item_sid == last_history_item_sid} |
| 420 | |
| 421 | def pre(self, idx): |
| 422 | 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. |
| 423 | |
| 424 | ### Instruction: |
| 425 | Can you predict the next possible item that the user may expect? |
| 426 | |
| 427 | """ |
| 428 | tokens = self.tokenizer.encode(instruction, bos=True, eos=False) |
| 429 | |
| 430 | history = self.get_history(self.data.iloc[idx]) |
| 431 | # print("**********************") |
| 432 | # print("history: ", history) |
| 433 | target_item = history['output'] |
| 434 | history['output'] = '' |
| 435 | negative_prompt_ids = copy.deepcopy(tokens) |
| 436 | |
| 437 | prompt = self.generate_prompt(history) |
| 438 | # print("prompt: ", prompt) |
| 439 | |
| 440 | tokens = tokens + self.tokenizer.encode(prompt, bos=False, eos=False) |
| 441 | # print("tokens: ", tokens) |
| 442 | # print("**********************") |
| 443 | history["input"] = "" |
| 444 | |
| 445 | attention_mask = [1] * len(tokens) |
| 446 | |
| 447 | if self.test: |
| 448 | return { |
| 449 | "input_ids": tokens, |
| 450 | "attention_mask": attention_mask, |
| 451 | } |
| 452 | |
| 453 | golden_tokens = self.tokenizer.encode(target_item, bos=False, eos=True) |
| 454 | input_prompt_len = len(tokens) |
no outgoing calls