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

Method pre

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

Source from the content-addressed store, hash-verified

1763 }
1764
1765 def pre(self, idx):
1766 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.
1767
1768### Instruction:
1769Based on the user interaction history and preference analysis, predict the next item's semantic ID.
1770
1771"""
1772 tokens = self.tokenizer.encode(instruction, bos=True, eos=False)
1773
1774 row_data = self.data[idx]
1775 input_and_target = self.get_input_and_target(row_data)
1776
1777 # Skip empty histories or missing targets
1778 if not input_and_target['history_semantic_ids'] or not input_and_target['target_sid']:
1779 return None
1780
1781 target_output = input_and_target['output']
1782 input_and_target['output'] = ''
1783
1784 prompt = self.generate_prompt(input_and_target)
1785 tokens = tokens + self.tokenizer.encode(prompt, bos=False, eos=False)
1786 attention_mask = [1] * len(tokens)
1787
1788 if self.test:
1789 return {
1790 "input_ids": tokens,
1791 "attention_mask": attention_mask,
1792 }
1793
1794 golden_tokens = self.tokenizer.encode(target_output + '\n', bos=False, eos=True)
1795 input_prompt_len = len(tokens)
1796 tokens = tokens + golden_tokens
1797 attention_mask = [1] * len(tokens)
1798 labels = [-100] * input_prompt_len + tokens[input_prompt_len:]
1799
1800 if len(tokens) >= self.max_len:
1801 print(f"Sequence length {len(tokens)} exceeds max_len {self.max_len}")
1802
1803 return {
1804 "input_ids": tokens[-self.max_len:],
1805 "attention_mask": attention_mask[-self.max_len:],
1806 "labels": labels[-self.max_len:],
1807 }

Callers

nothing calls this directly

Calls 3

get_input_and_targetMethod · 0.95
encodeMethod · 0.45
generate_promptMethod · 0.45

Tested by

no test coverage detected