Skip to content

feat(deepseek_v4): tool calling (DSML parser + chat template + grammar-constrained decoding) - #7

Open
nh13 wants to merge 1 commit into
dsv4/01-core-modelfrom
feat/dsv4-tool-calling
Open

nh13 wants to merge 1 commit into
dsv4/01-core-modelfrom
feat/dsv4-tool-calling

Conversation

@nh13

@nh13 nh13 commented Aug 30, 2026

Copy link
Copy Markdown
Owner

DeepSeek-V4 tool calling for mlx_lm.server, stacked on the base model (ml-explore#1797). Preview so maintainers can see it coming; not proposed for merge yet.

Three parts:

  • DSML tool-call parser + Python chat template (chat_templates/deepseek_v4.py, tool_parsers/deepseek_v4.py) — handles the model's truncated </|DSML|inv> close tag and the <-vs-</ token boundary.
  • DeepSeek standard-format parser (V3/V3.1/R1/V4) in tool_parsers/deepseek_v3.py.
  • Grammar-constrained decoding (tool_grammar.py + server.py) that forces the DSML structure so a quantization-degraded model cannot corrupt the keywords, with tool_choice auto/required/named and think-block awareness.

Tests cover the parser, chat template, and grammar (25 pass). The grammar tests drive the phase machine with a synthetic byte-level tokenizer reproducing the DeepSeek DSML token boundaries, so they run without the model files.

… decoding

Adds DeepSeek-V4 tool-calling to mlx_lm.server:

- DSML tool-call parser and Python chat template (chat_templates/deepseek_v4.py,
  tool_parsers/deepseek_v4.py), handling the model's truncated `</|DSML|inv>`
  close tag and the `<`-vs-`</` token boundary.
- A DeepSeek standard-format tool-call parser (V3/V3.1/R1/V4) in
  tool_parsers/deepseek_v3.py.
- Grammar-constrained decoding (tool_grammar.py + server.py) that forces the DSML
  tool-call structure so a quantization-degraded model cannot corrupt the
  structural keywords, with tool_choice auto/required/named and think-block
  awareness.

Tests cover the parser, chat template, and grammar. The grammar tests drive the
phase machine with a synthetic byte-level tokenizer that reproduces the DeepSeek
DSML token boundaries (`</` as a single token; `|DSML|`, `<think>` and `</think>`
as special tokens), so they run without the model files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant