MCPcopy Create free account
hub / github.com/AkaliKong/MiniOneRec / pre

Method pre

data.py:1584–1626  ·  view source on GitHub ↗
(self, idx)

Source from the content-addressed store, hash-verified

1582 return result
1583
1584 def pre(self, idx):
1585 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.
1586
1587### Instruction:
1588Analyze the user's interaction history, provide insights about their preferences, and predict the next item's semantic ID.
1589
1590"""
1591 tokens = self.tokenizer.encode(instruction, bos=True, eos=False)
1592
1593 row_data = self.data[idx]
1594 history_and_pref = self.get_history(row_data)
1595
1596 # Skip empty histories or missing targets
1597 if not history_and_pref['history_semantic_ids'] or not history_and_pref['target_sid']:
1598 return None
1599
1600 target_output = history_and_pref['output']
1601 history_and_pref['output'] = ''
1602
1603 prompt = self.generate_prompt(history_and_pref)
1604 tokens = tokens + self.tokenizer.encode(prompt, bos=False, eos=False)
1605 attention_mask = [1] * len(tokens)
1606
1607 if self.test:
1608 return {
1609 "input_ids": tokens,
1610 "attention_mask": attention_mask,
1611 }
1612
1613 golden_tokens = self.tokenizer.encode(target_output, bos=False, eos=True)
1614 input_prompt_len = len(tokens)
1615 tokens = tokens + golden_tokens
1616 attention_mask = [1] * len(tokens)
1617 labels = [-100] * input_prompt_len + tokens[input_prompt_len:]
1618
1619 if len(tokens) >= self.max_len:
1620 print(f"Sequence length {len(tokens)} exceeds max_len {self.max_len}")
1621
1622 return {
1623 "input_ids": tokens[-self.max_len:],
1624 "attention_mask": attention_mask[-self.max_len:],
1625 "labels": labels[-self.max_len:],
1626 }
1627
1628
1629class UserPreference2sidSFTDataset(BaseDataset):

Callers

nothing calls this directly

Calls 3

get_historyMethod · 0.95
encodeMethod · 0.45
generate_promptMethod · 0.45

Tested by

no test coverage detected