nemo_automodel.components.models.kimi_k3.encoding
nemo_automodel.components.models.kimi_k3.encoding
Kimi K3 XTML encoding helpers.
This module keeps chat rendering in Python.
Callers that need token IDs should consume EncodeSegment objects directly:
structural markers may be encoded as tiktoken special tokens, while user/tool
text and attribute values are encoded as ordinary text.
Module Contents
Classes
Functions
Data
API
Map assistant tool_calls[].id to (1-based position, function name).
The position mirrors the chat template’s enumeration over tool_calls
(every entry advances the position, even an id-less one). Duplicate ids keep
their first occurrence.
Re-sort K3 XTML tool results into assistant tool_calls order.
Serving frameworks generally deliver tool results already in call order. A
direct Transformers caller, however, may pass OpenAI-style tool messages in any
order, so each run of consecutive tool messages is matched against the most
recent preceding assistant tool_calls by opaque tool_call_id ==
tool_calls[].id (K3 drops the func:index format requirement) and
sorted by the matched 1-based position. The matched call is authoritative,
so each matched message’s tool is set to that call’s function name —
this keeps an explicit (and possibly stale) tool/name from drifting
out of sync with the reordered position. index is still derived from the
rendered position by the chat template. A run that cannot be fully matched is
left untouched. Re-running is idempotent.
This function is side-effect free: matched tool messages are shallow-copied
before their tool/name is rewritten, and every other message is
appended to the output as-is. The input list and its message objects are
never mutated.