Shortcuts

ChosenRejectedToMessages

class torchtune.data.ChosenRejectedToMessages(train_on_input: bool = False, column_map: Optional[Dict[str, str]] = None, new_system_prompt: Optional[str] = None)[source]

Transform for converting a single sample from datasets with “chosen” and “rejected” columns containing conversations to a list of chosen and rejected messages. For example:

|  chosen                                |  rejected                              |
|----------------------------------------|----------------------------------------|
| [{"role": "user", "content": Q1},      | [{"role": "user", "content": Q1},      |
|  {"role": "assistant", "content": A1}] |  {"role": "assistant", "content": A2}] |

will be converted to:

chosen = [
    Message(role="user", content="Q1"),
    Message(role="assistant", content="A1"),
]
rejected = [
    Message(role="user", content="Q1"),
    Message(role="assistant", content="A2"),
]

A single sample typically consists of a single optional system prompt and one or multiple turns of user and assistant messages.

Parameters:
  • train_on_input (bool) – Whether the model is trained on the user prompt or not. Default is False.

  • column_map (Optional[Dict[str, str]]) – a mapping to change the expected “chosen” and “rejected” column names to the actual column names in the dataset. Keys should be “chosen” and “rejected” and values should be the actual column names. Default is None, keeping the default column names.

  • new_system_prompt (Optional[str]) – if specified, prepend a system message. This can serve as instructions to guide the model response. Setting this will OVERRIDE any system messages already present in the dataset. Default is None.

Raises:

ValueError – If column_map is provided and chosen not in column_map, or rejected not in column_map.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources