Skip to navigation

nemo_automodel.components.datasets.llm.chat_dataset

View as MarkdownOpen in Claude

Module Contents

Classes

NameDescription
ChatDatasetDataset for OpenAI-format tool-calling chat transcripts.
ChatDatasetConfigConstruction-time configuration for ChatDataset (tokenizer is a build arg).

Functions

NameDescription
_as_iter-
_conversations_to_messagesConvert a ShareGPT conversations list to OpenAI messages.
_is_hf_repo_id-
_load_openai_messagesLoad OpenAI chat messages datasets from HF or local JSON/JSONL files.
_normalize_messagesEnsure messages list is valid and content fields are strings for system/user/assistant.
_parse_split_sliceParse a split string like "train[1024:]" into (base_split, bounds | None).
_split_slice_indicesRow indices selected by the slice in split out of num_rows rows.

Data

_SHAREGPT_ROLE_MAP

_SPLIT_SLICE_RE

API

class nemo_automodel.components.datasets.llm.chat_dataset.ChatDataset(
path_or_dataset_id: typing.Union[str, typing.Sequence[str]],
tokenizer,
split: str | None = None,
name: str | None = None,
seq_length: int | None = None,
padding: typing.Union[str, bool] = 'do_not_pad',
truncation: typing.Union[str, bool] = 'do_not_truncate',
start_of_turn_token: str | None = None,
chat_template: str | None = None,
shuffle_seed: int | None = None,
mask_reasoning_content: bool = False,
mask_history: bool = False,
unshifted: bool = False,
skip_invalid_samples: bool = False,
mask_generation_prompt: bool = False,
chat_template_kwargs: dict[str, typing.Any] | None = None
)

Bases: Dataset

Dataset for OpenAI-format tool-calling chat transcripts.

Each row should contain a messages list in OpenAI chat format (role / content), potentially including tool calls and tool responses. Rows that instead carry a ShareGPT conversations list (from / value, as used by PerfectBlend and similar) are auto-converted, so no manual column rename is needed. The conversation is formatted via the tokenizer’s chat template to produce input_ids, labels, and attention_mask suitable for SFT.

dataset
pad_token_id
= _add_pad_token(self.tokenizer)
nemo_automodel.components.datasets.llm.chat_dataset.ChatDataset.__getitem__(
idx: int
) -> typing.Dict[str, typing.List[int]]
nemo_automodel.components.datasets.llm.chat_dataset.ChatDataset.__len__() -> int
class nemo_automodel.components.datasets.llm.chat_dataset.ChatDatasetConfig(
path_or_dataset_id: str | typing.Sequence[str],
split: str | None = None,
name: str | None = None,
seq_length: int | None = None,
padding: str | bool = 'do_not_pad',
truncation: str | bool = 'do_not_truncate',
start_of_turn_token: str | None = None,
chat_template: str | None = None,
shuffle_seed: int | None = None,
mask_reasoning_content: bool = False,
mask_history: bool = False,
unshifted: bool = False,
skip_invalid_samples: bool = False,
mask_generation_prompt: bool = False,
chat_template_kwargs: dict[str, typing.Any] | None = None
)
Dataclass

Construction-time configuration for ChatDataset (tokenizer is a build arg).

accepts_tokenizer
bool = True
chat_template
str | None = None

Optional Jinja template string overriding tokenizer.chat_template.

chat_template_kwargs
dict[str, Any] | None = None

Extra chat template variables (e.g. {enable_thinking: false}) for every render.

mask_generation_prompt
bool = False

If True, exclude the template-supplied prefix of each assistant turn (role header and any empty reasoning block such as <think></think>) from the loss mask.

mask_history
bool = False

If True, supervise only the final assistant turn.

mask_reasoning_content
bool = False

If True, exclude rendered reasoning traces from the loss mask.

name
str | None = None

Optional Hub subset / config name.

padding
str | bool = 'do_not_pad'

Padding mode for format_chat_template.

path_or_dataset_id
str | Sequence[str]

HF dataset id, local JSON/JSONL path(s), Parquet file, or Parquet directory.

seq_length
int | None = None

Maximum sequence length for padding and truncation in formatting.

shuffle_seed
int | None = None

If set, shuffles Hub/Parquet data before applying a split slice.

skip_invalid_samples
bool = False

If True, skip malformed JSONL lines when reading local files.

split
str | None = None

Dataset split or slice (e.g. train, train[1024:]).

start_of_turn_token
str | None = None

Optional token marking assistant turns for answer-only loss.

truncation
str | bool = 'do_not_truncate'

Truncation mode for format_chat_template.

unshifted
bool = False

Passed through to format_chat_template.

nemo_automodel.components.datasets.llm.chat_dataset.ChatDatasetConfig.build(
tokenizer: 'PreTrainedTokenizerBase | None'
) -> 'ChatDataset'

Build a ChatDataset from this ChatDatasetConfig and a runtime tokenizer.

nemo_automodel.components.datasets.llm.chat_dataset._as_iter(
val: typing.Union[str, typing.Sequence[str]]
) -> typing.Iterator[str]
nemo_automodel.components.datasets.llm.chat_dataset._conversations_to_messages(
conversations: typing.Any
) -> typing.List[typing.Dict[str, typing.Any]]

Convert a ShareGPT conversations list to OpenAI messages.

ShareGPT-style rows store turns as {"from": <role>, "value": <text>} under a conversations column instead of OpenAI {"role", "content"} under messages. Map the common plain-chat roles so such datasets load without a manual rename. Raises on an unsupported role rather than guessing.

nemo_automodel.components.datasets.llm.chat_dataset._is_hf_repo_id(
val: str
) -> bool
nemo_automodel.components.datasets.llm.chat_dataset._load_openai_messages(
path_or_dataset_id: typing.Union[str, typing.Sequence[str]],
split: str | None = None,
name: str | None = None,
shuffle_seed: int | None = None,
skip_invalid_samples: bool = False
)

Load OpenAI chat messages datasets from HF or local JSON/JSONL files.

For HF repo IDs, we delegate to datasets.load_dataset. When split is provided, the full base split is loaded and shuffled before any slice (e.g. [1024:]) is applied so that train/val splits sample from a consistent random order. When split is None it is passed through to load_dataset as-is (no default override).

For local files, we manually parse JSONL/JSON to avoid pyarrow type inference issues (e.g., heterogeneous field types under tools). A slice in split is applied to the parsed rows in file order.

Parameters:

path_or_dataset_id
Union[str, Sequence[str]]

HF dataset ID or local file path(s).

split
str | NoneDefaults to None

Dataset split to load (e.g., “train”, “train[1024:]”).

name
str | NoneDefaults to None

Dataset configuration/subset name

shuffle_seed
int | NoneDefaults to None

Random seed for shuffling HF datasets before slicing. Set to None to disable shuffling.

skip_invalid_samples
boolDefaults to False

If True, skip malformed JSONL lines for local files instead of failing fast.

nemo_automodel.components.datasets.llm.chat_dataset._normalize_messages(
messages: typing.List[typing.Dict[str, typing.Any]]
) -> typing.List[typing.Dict[str, typing.Any]]

Ensure messages list is valid and content fields are strings for system/user/assistant.

  • Keeps tool_calling fields if present (e.g., tool calls in assistant messages, tool role messages).
  • If content is a list of parts, only keep text parts.
nemo_automodel.components.datasets.llm.chat_dataset._parse_split_slice(
split: str | None
)

Parse a split string like "train[1024:]" into (base_split, bounds | None).

bounds is a (start, stop) pair of the raw bound strings (None when a bound is omitted). Resolve the split against a row count with _split_slice_indices.

nemo_automodel.components.datasets.llm.chat_dataset._split_slice_indices(
split: str,
base_split: str,
num_rows: int
) -> range

Row indices selected by the slice in split out of num_rows rows.

Bounds are resolved by datasets.ReadInstruction so units, rounding and range checks match load_dataset(..., split=split).

nemo_automodel.components.datasets.llm.chat_dataset._SHAREGPT_ROLE_MAP = {'system': 'system', 'human': 'user', 'user': 'user', 'gpt': 'assistant', 'assis...
nemo_automodel.components.datasets.llm.chat_dataset._SPLIT_SLICE_RE = re.compile('^(\\w+)\\[(-?\\d+%?)?:(-?\\d+%?)?\\]$')