Shortcuts

get_openai_messages

torchtune.data.get_openai_messages(sample: Mapping[str, Any], train_on_input: bool = False) List[Message][source]

Warning

This class is deprecated and will be removed in a future release. Please use OpenAIToMessages instead. The following are equivalent:

# Deprecated
transformed_sample = get_openai_messages(sample, train_on_input=True)

# New
transformed_sample = OpenAIToMessages(train_on_input=True)(sample)

Convert a chat sample adhering to the OpenAI API json structure to torchtune’s Message structure.

OpenAI API standard chat format follows:

{
    # key could be "messages" OR "conversations"
    "messages": [
        {
            "role": <system|user|assistant>,
            "content": <message>,
        },
        ...
    ]
}

Message follows:

[
    {
        "role": <system|user|assistant>,
        "content": <message>,
    },
    ...
]
Parameters:
  • sample (Mapping[str, Any]) – a single data sample with “conversations” field pointing to a list of dict messages.

  • train_on_input (bool) – whether the prompt should remain unmasked. Default: False

Raises:

ValueError – If the sample does not contain “messages” or “conversations” key.

Returns:

A list of messages with “role” and “content” fields.

Return type:

List[Message]

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