Shortcuts

JSONToMessages

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

Convert a single chat sample with identical json structure to torchtune’s Message structure. This transform simply creates Message dataclasses from the provided jsons.

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

For example:

{
    "messages": [
        {
            "role": <system|user|assistant>,
            "content": <message>,
        },
        ...
    ]
}

Message follows:

[
    {
        "role": <system|user|assistant>,
        "content": <message>,
    },
    ...
]
Parameters:
  • train_on_input (bool) – whether the prompt should remain unmasked. Default: False

  • column_map (Optional[Dict[str, str]]) – a mapping from the expected columns (“messages”) to the new column names in the dataset. Key should be “messages” and value should be the new column name. If None, keep the default “messages”. Default is None.

  • 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 messages 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