nemo_automodel.components.datasets.llm.agent_chat#
Multi-turn agent SFT dataset adapter.
Loads function-calling chat datasets where each example contains tool
definitions and a multi-turn message list that includes tool calls and
tool responses, then renders them through the tokenizer’s chat template
with answer_only_loss_mask=True so that user and tool tokens
are excluded from the loss.
Two input schemas are accepted:
Swift / chatml
messagesschema (used byAI-ModelScope/function-calling-chatml)::{ "tools": "[{...openai tool schema...}]", "messages": [ {"role": "user", "content": "..."}, {"role": "tool_call", "content": "{\"name\": ..., \"arguments\": ...}"}, {"role": "tool_response", "content": "..."}, {"role": "assistant", "content": "..."} ] }ShareGPT
conversationsschema (used byllamafactory/glaive_toolcall_enand similar)::{ "tools": "[...]", "conversations": [ {"from": "human", "value": "..."}, {"from": "function_call", "value": "..."}, {"from": "observation", "value": "..."}, {"from": "gpt", "value": "..."} ] }
Consecutive tool_call entries are merged into a single assistant
message with parallel tool_calls; the following tool_response
entries are paired with those calls in order.
Module Contents#
Functions#
Parse |
|
Convert ShareGPT |
|
Convert chatml-style agent messages to OpenAI chat-completions format. |
|
Restrict the loss to the final assistant turn ( |
|
Render one agent example into tokenized |
|
Load a multi-turn function-calling SFT dataset. |
Data#
API#
- nemo_automodel.components.datasets.llm.agent_chat.logger#
‘getLogger(…)’
- nemo_automodel.components.datasets.llm.agent_chat._VALID_MESSAGE_ROLES#
None
- nemo_automodel.components.datasets.llm.agent_chat._SHAREGPT_ROLE_MAP#
None
- nemo_automodel.components.datasets.llm.agent_chat._json_load_if_str(value: Any) Any#
Parse
valueas JSON if it is a string, otherwise return as-is.
- conversations: List[Dict[str, Any]],
Convert ShareGPT
{from, value}turns to chatml{role, content}.
- nemo_automodel.components.datasets.llm.agent_chat._convert_messages(
- messages: List[Dict[str, Any]],
- example_id: Optional[Union[int, str]] = None,
Convert chatml-style agent messages to OpenAI chat-completions format.
Consecutive
tool_callentries collapse into one assistant message with paralleltool_calls. When the preceding emitted turn is an assistant message without tool_calls (e.g. an assistantcontentthat reasons before calling tools), the tool_calls attach to that message instead of creating a second consecutive assistant turn — this preserves the natural single-turn shape the model produces at inference.tool_response(ortool) entries that follow are paired with those tool_call ids in order.- Parameters:
messages – chatml-style turns with roles in
_VALID_MESSAGE_ROLES.example_id – Optional identifier used to derive unique tool_call ids.
- Returns:
A list of OpenAI-format messages suitable for
apply_chat_template.
- nemo_automodel.components.datasets.llm.agent_chat._mask_labels_to_last_turn(
- labels: List[int],
- ignore_index: int = -100,
Restrict the loss to the final assistant turn (
mask_history).labelscome from :func:format_chat_templatewith every assistant turn supervised; non-assistant tokens are alreadyignore_index. Because the chat template renders each assistant message as a single contiguous span, supervised tokens form one maximal run per assistant turn separated byignore_indexruns. This keeps only the last such run and masks every earlier supervised token in place.- Parameters:
labels – per-token labels (
ignore_indexmarks unsupervised tokens).ignore_index – the value marking unsupervised tokens.
- Returns:
The same list, mutated so only the final supervised run is kept.
- nemo_automodel.components.datasets.llm.agent_chat._format_example(
- example: Dict[str, Any],
- tokenizer,
- eos_token_id: int,
- pad_token_id: int,
- seq_length: Optional[int] = None,
- padding: Union[str, bool] = False,
- truncation: Union[str, bool] = False,
- mask_reasoning_content: bool = False,
- train_on_last_turn_only: bool = False,
Render one agent example into tokenized
input_ids/labels.
- nemo_automodel.components.datasets.llm.agent_chat.make_agent_chat_dataset(
- tokenizer,
- *,
- dataset_name: Optional[str] = None,
- path: Optional[Union[str, List[str]]] = None,
- split: str = 'train',
- seq_length: Optional[int] = None,
- limit_dataset_samples: Optional[int] = None,
- padding: Union[str, bool] = False,
- truncation: Union[str, bool] = False,
- mask_reasoning_content: bool = False,
- train_on_last_turn_only: bool = False,
Load a multi-turn function-calling SFT dataset.
Exactly one of
dataset_name(HuggingFace Hub id) orpath(local JSON/JSONL file or list of files) must be provided. The loaded examples are lazily rendered through the tokenizer’s chat template; tool/user tokens are excluded from the loss viaanswer_only_loss_mask=True.- Parameters:
tokenizer – HuggingFace tokenizer with a chat template.
dataset_name – HF Hub dataset id, e.g.
llamafactory/glaive_toolcall_en.path – Local JSON/JSONL file path or list of paths.
split – Dataset split (only used with
dataset_name).seq_length – Optional max sequence length for the tokenizer.
limit_dataset_samples – If set, keep only the first N examples.
padding – Padding strategy forwarded to the tokenizer.
truncation – Truncation strategy forwarded to the tokenizer.
mask_reasoning_content – If True, exclude assistant
reasoning_content(thinking) tokens from the loss while still rendering them into the prompt. Requires a chat template that emitsreasoning_content. Defaults to False, which trains on reasoning tokens like any other assistant content.train_on_last_turn_only – If True, supervise only the final assistant turn of each dialogue (
mask_history); all earlier assistant turns are excluded from the loss. Defaults to False, which supervises every assistant turn.
- Returns:
A
LazyMappedDatasetyielding dicts withinput_ids,labelsandattention_maskready for the trainer collator.