Shortcuts

OpenAIToMessages

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

Convert a single chat sample adhering to the OpenAI chat completion JSON structure to torchtune’s Message structure. This supports both text and image messages.

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": [
                {
                    "type": "text",
                    "text": "What'''s in this image?",
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": <url>,
                    },
                },
        },
        ...
    ]
}

Message follows:

[
    {
        "role": <system|user|assistant>,
        "content": [
            {
                "type": "text",
                "content": "What'''s in this image?",
            },
            {
                "type": "image",
                "content": <PIL.Image.Image>,
            },
        ],
    },
    ...
]
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