nat.finetuning.utils.parsers.base_parser#

Attributes#

Functions#

parse_to_openai_messages(→ list[dict])

Convert IntermediateStep objects to OpenAI-compatible messages.

_validate_message_sequence(→ list[dict])

Validate and fix the message sequence to follow OpenAI's expected format.

Module Contents#

logger#
parse_to_openai_messages(
steps: list[nat.data_models.intermediate_step.IntermediateStep],
) list[dict]#

Convert IntermediateStep objects to OpenAI-compatible messages.

Args:

steps: List of IntermediateStep objects representing the conversation.

Returns:

List of dictionaries formatted for OpenAI API consumption.

Raises:

ValueError: If unsupported type or invalid sequence.

_validate_message_sequence(messages: list[dict]) list[dict]#

Validate and fix the message sequence to follow OpenAI’s expected format.

Rules:

  • System messages can only appear at the beginning

  • After system messages, must alternate between user/tool and assistant

  • Cannot have consecutive user messages or consecutive assistant messages

  • If first non-system messages are not user messages, they will be concatenated into a single user message (with a warning)

Args:

messages: List of parsed OpenAI messages

Returns:

list[dict]: The validated (and potentially fixed) message list

Raises:

ValueError: If the message sequence is invalid.