Skip to content

register_chat_format

Register a chat-format factory for a model family, so :func:chat_format_for_tokenizer can pick it.

Use it for a family whose tokenizer has no usable chat_template, or to replace the template rendering.

Parameters:

Name Type Description Default
factory ChatFormatFactory

factory(tokenizer) returning a :class:ChatFormat, or None for a tokenizer it does not handle.

required
first bool

Ask this factory before the ones registered earlier (default), so it can override a built-in.

True

Returns:

Type Description
ChatFormatFactory

factory, so this can be used as a decorator.

Source code in gradiend/trainer/text/common/chat_format.py
def register_chat_format(factory: ChatFormatFactory, *, first: bool = True) -> ChatFormatFactory:
    """Register a chat-format factory for a model family, so :func:`chat_format_for_tokenizer` can pick it.

    Use it for a family whose tokenizer has no usable ``chat_template``, or to replace the template rendering.

    Args:
        factory: ``factory(tokenizer)`` returning a :class:`ChatFormat`, or ``None`` for a tokenizer it does not handle.
        first: Ask this factory before the ones registered earlier (default), so it can override a built-in.

    Returns:
        ``factory``, so this can be used as a decorator.
    """
    if first:
        _CHAT_FORMAT_FACTORIES.insert(0, factory)
    else:
        _CHAT_FORMAT_FACTORIES.append(factory)
    return factory