nemo_rl.data.deepseek_v4_encoding#
DeepSeek-V4 Encoding
A self-contained implementation for encoding/decoding DeepSeek-V4 chat messages with tool calling, thinking mode, and quick instruction task support.
Module Contents#
Functions#
Serialize a value to JSON string. |
|
Extract function definitions from OpenAI-format tool list. |
|
Convert OpenAI-format tool calls to internal format. |
|
Convert internal tool calls to OpenAI format. |
|
Encode tool call arguments into DSML parameter format. |
|
Decode DSML parameters back to a tool call dict. |
|
Render tool schemas into the system prompt format. |
|
Find the index of the last user/developer message. |
|
Render a single message at the given index into its encoded string form. |
|
Merge tool messages into the preceding user message using content_blocks format. |
|
Sort tool_result blocks within user messages by the order of tool_calls in the preceding assistant message. |
|
Encode a list of messages into the DeepSeek-V4 prompt format. |
|
Drop reasoning and non-essential messages before the last user message. |
|
Read text from index until one of the stop strings is found. |
|
Parse DSML tool calls from text starting at the given index. |
|
Parse a model completion text into a structured assistant message. |
Data#
API#
- nemo_rl.data.deepseek_v4_encoding.bos_token: str#
‘<|begin▁of▁sentence|>’
- nemo_rl.data.deepseek_v4_encoding.eos_token: str#
‘<|end▁of▁sentence|>’
- nemo_rl.data.deepseek_v4_encoding.thinking_start_token: str#
‘
’
- nemo_rl.data.deepseek_v4_encoding.thinking_end_token: str#
‘’
- nemo_rl.data.deepseek_v4_encoding.dsml_token: str#
‘|DSML|’
- nemo_rl.data.deepseek_v4_encoding.USER_SP_TOKEN#
‘<|User|>’
- nemo_rl.data.deepseek_v4_encoding.ASSISTANT_SP_TOKEN#
‘<|Assistant|>’
- nemo_rl.data.deepseek_v4_encoding.LATEST_REMINDER_SP_TOKEN#
‘<|latest_reminder|>’
- nemo_rl.data.deepseek_v4_encoding.DS_TASK_SP_TOKENS#
None
- nemo_rl.data.deepseek_v4_encoding.VALID_TASKS#
‘set(…)’
- nemo_rl.data.deepseek_v4_encoding.system_msg_template: str#
‘{content}’
- nemo_rl.data.deepseek_v4_encoding.user_msg_template: str#
‘{content}’
- nemo_rl.data.deepseek_v4_encoding.latest_reminder_msg_template: str#
‘{content}’
- nemo_rl.data.deepseek_v4_encoding.assistant_msg_template: str#
None
- nemo_rl.data.deepseek_v4_encoding.assistant_msg_wo_eos_template: str#
‘{reasoning}{content}{tool_calls}’
- nemo_rl.data.deepseek_v4_encoding.thinking_template: str#
‘{reasoning}’
- nemo_rl.data.deepseek_v4_encoding.response_format_template: str = <Multiline-String>#
- nemo_rl.data.deepseek_v4_encoding.tool_call_template: str = <Multiline-String>#
- nemo_rl.data.deepseek_v4_encoding.tool_calls_template = <Multiline-String>#
- nemo_rl.data.deepseek_v4_encoding.tool_calls_block_name: str#
‘tool_calls’
- nemo_rl.data.deepseek_v4_encoding.tool_output_template: str#
‘<tool_result>{content}</tool_result>’
- nemo_rl.data.deepseek_v4_encoding.REASONING_EFFORT_MAX = <Multiline-String>#
- nemo_rl.data.deepseek_v4_encoding.TOOLS_TEMPLATE = <Multiline-String>#
- nemo_rl.data.deepseek_v4_encoding.to_json(value: Any) str#
Serialize a value to JSON string.
- nemo_rl.data.deepseek_v4_encoding.tools_from_openai_format(tools)#
Extract function definitions from OpenAI-format tool list.
- nemo_rl.data.deepseek_v4_encoding.tool_calls_from_openai_format(tool_calls)#
Convert OpenAI-format tool calls to internal format.
- nemo_rl.data.deepseek_v4_encoding.tool_calls_to_openai_format(tool_calls)#
Convert internal tool calls to OpenAI format.
- nemo_rl.data.deepseek_v4_encoding.encode_arguments_to_dsml(
- tool_call: Dict[str, Any],
Encode tool call arguments into DSML parameter format.
- Parameters:
tool_call – Dict with “name” and “arguments” keys.
- Returns:
DSML-formatted parameter string.
- nemo_rl.data.deepseek_v4_encoding.decode_dsml_to_arguments(
- tool_name: str,
- tool_args: Dict[str, Tuple[str, str]],
Decode DSML parameters back to a tool call dict.
- Parameters:
tool_name – Name of the tool.
tool_args – Dict mapping param_name -> (value, is_string_flag).
- Returns:
Dict with “name” and “arguments” (JSON string) keys.
- nemo_rl.data.deepseek_v4_encoding.render_tools(
- tools: List[Dict[str, Union[str, Dict[str, Any]]]],
Render tool schemas into the system prompt format.
- Parameters:
tools – List of tool schema dicts (each with name, description, parameters).
- Returns:
Formatted tools section string.
- nemo_rl.data.deepseek_v4_encoding.find_last_user_index(
- messages: List[Dict[str, Any]],
Find the index of the last user/developer message.
- nemo_rl.data.deepseek_v4_encoding.render_message(
- index: int,
- messages: List[Dict[str, Any]],
- thinking_mode: str,
- drop_thinking: bool = True,
- reasoning_effort: Optional[str] = None,
Render a single message at the given index into its encoded string form.
This is the core function that converts each message in the conversation into the DeepSeek-V4 format.
- Parameters:
index – Index of the message to render.
messages – Full list of messages in the conversation.
thinking_mode – Either “chat” or “thinking”.
drop_thinking – Whether to drop reasoning content from earlier turns.
reasoning_effort – Optional reasoning effort level (“max”, “high”, or None).
- Returns:
Encoded string for this message.
- nemo_rl.data.deepseek_v4_encoding.merge_tool_messages(
- messages: List[Dict[str, Any]],
Merge tool messages into the preceding user message using content_blocks format.
DeepSeek-V4 does not have a standalone “tool” role; instead, tool results are encoded as <tool_result> blocks within user messages.
This function converts a standard OpenAI-format conversation (with separate “tool” role messages) into V4 format where tool results are merged into user messages.
- Parameters:
messages – List of message dicts in OpenAI format.
- Returns:
Processed message list with tool messages merged into user messages.
- nemo_rl.data.deepseek_v4_encoding.sort_tool_results_by_call_order(
- messages: List[Dict[str, Any]],
Sort tool_result blocks within user messages by the order of tool_calls in the preceding assistant message.
- Parameters:
messages – Preprocessed message list (after merge_tool_messages).
- Returns:
Message list with sorted tool result blocks.
- nemo_rl.data.deepseek_v4_encoding.encode_messages(
- messages: List[Dict[str, Any]],
- thinking_mode: str,
- context: Optional[List[Dict[str, Any]]] = None,
- drop_thinking: bool = True,
- add_default_bos_token: bool = True,
- reasoning_effort: Optional[str] = None,
Encode a list of messages into the DeepSeek-V4 prompt format.
This is the main entry point for encoding conversations. It handles:
BOS token insertion
Thinking mode with optional reasoning content dropping
Tool message merging into user messages
Multi-turn conversation context
- Parameters:
messages – List of message dicts to encode.
thinking_mode – Either “chat” or “thinking”.
context – Optional preceding context messages (already encoded prefix).
drop_thinking – If True, drop reasoning from earlier assistant turns (only keep reasoning for messages after the last user message).
add_default_bos_token – Whether to prepend BOS token at conversation start.
reasoning_effort – Optional reasoning effort level (“max”, “high”, or None).
- Returns:
The encoded prompt string.
- nemo_rl.data.deepseek_v4_encoding._drop_thinking_messages(
- messages: List[Dict[str, Any]],
Drop reasoning and non-essential messages before the last user message.
Behavior:
Messages with role in [“user”, “system”, “tool”, “latest_reminder”] are always kept.
Messages at or after the last user index are always kept.
Assistant messages before the last user get reasoning removed.
Developer messages before the last user are dropped entirely.
- nemo_rl.data.deepseek_v4_encoding._read_until_stop(
- index: int,
- text: str,
- stop: List[str],
Read text from index until one of the stop strings is found.
- Returns:
Tuple of (new_index, content_before_stop, matched_stop_string_or_None).
- nemo_rl.data.deepseek_v4_encoding.parse_tool_calls(
- index: int,
- text: str,
Parse DSML tool calls from text starting at the given index.
- Parameters:
index – Starting position in text.
text – The full text to parse.
- Returns:
Tuple of (new_index, last_stop_token, list_of_tool_call_dicts). Each tool call dict has “name” and “arguments” keys.
- nemo_rl.data.deepseek_v4_encoding.parse_message_from_completion_text(
- text: str,
- thinking_mode: str,
Parse a model completion text into a structured assistant message.
This function takes the raw text output from the model (a single assistant turn) and extracts:
reasoning (thinking block)
content (summary/response)
tool_calls (if any)
NOTE: This function is designed to parse only correctly formatted strings and will raise ValueError for malformed output.
- Parameters:
text – The raw completion text (including EOS token).
thinking_mode – Either “chat” or “thinking”.
- Returns:
“role”, “content”, “reasoning”, “tool_calls”. tool_calls are in OpenAI format.
- Return type:
Dict with keys