[PR #300] [MERGED] Match DeepSeek-V4.1 reference prompts and preserve agent tool histories #457

Closed
opened 2026-10-29 21:25:38 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/300
Author: @Gazelle221B
Created: 10/20/2026
Status: ✅ Merged
Merged: 10/20/2026
Merged by: @mw00

Base: main ← Head: ds41-reference-prompt-format


📝 Commits (1)

  • 5372cdd Match DeepSeek-V4.1 reference prompt rendering and tool parsing

📊 Changes

19 files changed (+2214 additions, -36 deletions)

View changed files

➕ LICENSE.deepseek-v41 (+21 -0)
📝 README.md (+3 -0)
➕ docs/deepseek-prompt-format.md (+40 -0)
➕ serve/ds41_encoding.py (+995 -0)
➕ serve/fixtures/deepseek_v41/README.md (+21 -0)
➕ serve/fixtures/deepseek_v41/test_input_1.json (+82 -0)
➕ serve/fixtures/deepseek_v41/test_input_2.json (+24 -0)
➕ serve/fixtures/deepseek_v41/test_input_3.json (+93 -0)
➕ serve/fixtures/deepseek_v41/test_input_4.json (+28 -0)
➕ serve/fixtures/deepseek_v41/test_input_5.json (+39 -0)
➕ serve/fixtures/deepseek_v41/test_output_1.txt (+38 -0)
➕ serve/fixtures/deepseek_v41/test_output_2.txt (+1 -0)
➕ serve/fixtures/deepseek_v41/test_output_3.txt (+35 -0)
➕ serve/fixtures/deepseek_v41/test_output_4.txt (+2 -0)
➕ serve/fixtures/deepseek_v41/test_output_5.txt (+11 -0)
📝 serve/frontend.py (+190 -15)
📝 serve/server.py (+24 -20)
➕ serve/test_ds41_conformance.py (+563 -0)
📝 tools/ds4/chat_template.jinja (+4 -1)

📄 Description

DeepSeek's official fixtures expose lost reminders, later system instructions and message-local tools. Parallel tool results can also lose their call association, and DSML string flags can change argument types. Use the pinned reference encoder for DeepSeek rendering and preserve the metadata needed by it. Other model families keep their existing rendering path.

Based on main at 8d9a9b5d705049a87da853717cdc59440252de30 (v1.1.17). The encoder and unchanged fixtures come from DeepSeek-V4.1-Flash-1017, revision 1bae0e1088993b9b82de08f59de5be5a34e42170, with MIT attribution and license retained.

The change preserves later system/developer messages, tool IDs and parallel result order; accepts integer reasoning budgets 1–100 and retains medium → 75 / xhigh → 100; respects DSML string flags; and preserves leading newlines and text-block boundaries.

Offline check Before After
Official fixture prompts, exact UTF-8 bytes 3/5 5/5
Later system/developer role converted to user system role retained
Parallel results supplied B/A for calls A/B B/A A/B, matched by ID
Integer reasoning budgets 1–100 rejected accepted and range-checked
DSML string/number flag overridden by heuristics determines argument type
Leading content newlines stripped preserved
Namespaced tools incomplete declarations, responses and history round-trip
Anthropic tool-use/result IDs dropped preserved and associated

Measured on 2 × Radeon AI PRO R9700 (32 GB each, PCIe 5.0 ×16), Ryzen 9 7950X, 122.7 GiB RAM, HIP, DeepSeek-V4.1-Flash-1017 Maya-S (six shards). Both servers used the same engine binary and config, DSpark on, vision off, context 131072, and separate workspace expert-profile copies initialized identically. Requests were greedy with thinking off: a 16-token warm-up, then three 256-token chat completions, plus 2048/8192-token prefill requests with zero cached tokens.

Measurement Before After
Decode, total generated tokens / total decode time 21.95 tok/s 22.30 tok/s
Prefill, 2048 tokens 211.3 tok/s 180.5 tok/s
Prefill, 8192 tokens 245.3 tok/s 240.3 tok/s

All three ordinary chat prompts had identical rendered bytes before/after; all three generated answers differed. Both runs generated two parallel weather calls and, after city-labelled results arrived in reverse order, reported Tokyo 18 and Osaka 23, with different wording. The round-trip response took 2.30 s before and 16.85 s after; its 44-token prefill took 1.48 s and 15.96 s. The tool-result prompt deliberately changes order with this fix, so that round trip is excluded from the throughput comparison.

This single sequential A/B includes slower 2048-token prefill and a large agent-round-trip latency outlier (a 44-token prefill taking 16 s, which looks like cold expert reads rather than rendering cost, but I have not confirmed it). It does not establish a causal speed change or generation-quality improvement. Opened as a draft until that outlier is re-measured.

Validation: 29 new conformance tests pass, including all five official prompt pairs, numeric budgets, late system/developer messages, parallel results in both adapters, DSML argument types and literal nested DSML tags, chunk boundaries/newlines and namespaced response/history round trips. All CI-equivalent checks passed under Python 3.12.14 with the workflow's dependencies and pinned gguf-py (3cf03257f219afbe7334045ff7c6a06ac68c627d): HIP mapping check; 244 server tests (6 skipped); 331 installer/tools tests (28 skipped). Exact commands, requests, responses and engine logs are available on request.

Scope: prompt rendering and response parsing only. No engine, weights, quantization or GPU kernels change. DSML parameters are buffered until their closing tag before argument JSON is streamed, so the first argument fragment can arrive later. OpenAI responses expose the reference's bare function name and optional call-level namespace; internal routing and Anthropic names stay qualified. Anthropic thinking token budgets keep their existing low/high/max conversion.

tools/ds4/chat_template.jinja remains a partial compatibility export for ordinary chat/tools. It does not implement the full reference's reminders/tasks, message-local tools, later system roles, ID sorting or namespaces; duplicating the encoder's preprocessing in sandboxed Jinja would introduce a second implementation. The server uses the pinned Python encoder. This boundary is documented in docs/deepseek-prompt-format.md; existing model packs are not rewritten. Image placeholders pass the fixtures, but vision inference, model quality and exhaustive malformed/truncated completion equivalence are not established by these tests.

Refs #299

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/300 **Author:** [@Gazelle221B](https://github.com/Gazelle221B) **Created:** 10/20/2026 **Status:** ✅ Merged **Merged:** 10/20/2026 **Merged by:** [@mw00](https://github.com/mw00) **Base:** `main` ← **Head:** `ds41-reference-prompt-format` --- ### 📝 Commits (1) - [`5372cdd`](https://github.com/mw00/project-maya/commit/5372cdde7e5669033f0b831fb4ced4d69014e1fc) Match DeepSeek-V4.1 reference prompt rendering and tool parsing ### 📊 Changes **19 files changed** (+2214 additions, -36 deletions) <details> <summary>View changed files</summary> ➕ `LICENSE.deepseek-v41` (+21 -0) 📝 `README.md` (+3 -0) ➕ `docs/deepseek-prompt-format.md` (+40 -0) ➕ `serve/ds41_encoding.py` (+995 -0) ➕ `serve/fixtures/deepseek_v41/README.md` (+21 -0) ➕ `serve/fixtures/deepseek_v41/test_input_1.json` (+82 -0) ➕ `serve/fixtures/deepseek_v41/test_input_2.json` (+24 -0) ➕ `serve/fixtures/deepseek_v41/test_input_3.json` (+93 -0) ➕ `serve/fixtures/deepseek_v41/test_input_4.json` (+28 -0) ➕ `serve/fixtures/deepseek_v41/test_input_5.json` (+39 -0) ➕ `serve/fixtures/deepseek_v41/test_output_1.txt` (+38 -0) ➕ `serve/fixtures/deepseek_v41/test_output_2.txt` (+1 -0) ➕ `serve/fixtures/deepseek_v41/test_output_3.txt` (+35 -0) ➕ `serve/fixtures/deepseek_v41/test_output_4.txt` (+2 -0) ➕ `serve/fixtures/deepseek_v41/test_output_5.txt` (+11 -0) 📝 `serve/frontend.py` (+190 -15) 📝 `serve/server.py` (+24 -20) ➕ `serve/test_ds41_conformance.py` (+563 -0) 📝 `tools/ds4/chat_template.jinja` (+4 -1) </details> ### 📄 Description DeepSeek's official fixtures expose lost reminders, later system instructions and message-local tools. Parallel tool results can also lose their call association, and DSML string flags can change argument types. Use the pinned reference encoder for DeepSeek rendering and preserve the metadata needed by it. Other model families keep their existing rendering path. Based on `main` at `8d9a9b5d705049a87da853717cdc59440252de30` (v1.1.17). The encoder and unchanged fixtures come from [DeepSeek-V4.1-Flash-1017](https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash-1017/tree/1bae0e1088993b9b82de08f59de5be5a34e42170/encoding), revision `1bae0e1088993b9b82de08f59de5be5a34e42170`, with MIT attribution and license retained. The change preserves later system/developer messages, tool IDs and parallel result order; accepts integer reasoning budgets 1–100 and retains `medium` → 75 / `xhigh` → 100; respects DSML string flags; and preserves leading newlines and text-block boundaries. | Offline check | Before | After | | --- | --- | --- | | Official fixture prompts, exact UTF-8 bytes | 3/5 | 5/5 | | Later system/developer role | converted to user | system role retained | | Parallel results supplied B/A for calls A/B | B/A | A/B, matched by ID | | Integer reasoning budgets 1–100 | rejected | accepted and range-checked | | DSML string/number flag | overridden by heuristics | determines argument type | | Leading content newlines | stripped | preserved | | Namespaced tools | incomplete | declarations, responses and history round-trip | | Anthropic tool-use/result IDs | dropped | preserved and associated | Measured on 2 × Radeon AI PRO R9700 (32 GB each, PCIe 5.0 ×16), Ryzen 9 7950X, 122.7 GiB RAM, HIP, DeepSeek-V4.1-Flash-1017 Maya-S (six shards). Both servers used the same engine binary and config, DSpark on, vision off, context 131072, and separate workspace expert-profile copies initialized identically. Requests were greedy with thinking off: a 16-token warm-up, then three 256-token chat completions, plus 2048/8192-token prefill requests with zero cached tokens. | Measurement | Before | After | | --- | ---: | ---: | | Decode, total generated tokens / total decode time | 21.95 tok/s | 22.30 tok/s | | Prefill, 2048 tokens | 211.3 tok/s | 180.5 tok/s | | Prefill, 8192 tokens | 245.3 tok/s | 240.3 tok/s | All three ordinary chat prompts had identical rendered bytes before/after; all three generated answers differed. Both runs generated two parallel weather calls and, after city-labelled results arrived in reverse order, reported Tokyo 18 and Osaka 23, with different wording. The round-trip response took 2.30 s before and 16.85 s after; its 44-token prefill took 1.48 s and 15.96 s. The tool-result prompt deliberately changes order with this fix, so that round trip is excluded from the throughput comparison. This single sequential A/B includes slower 2048-token prefill and a large agent-round-trip latency outlier (a 44-token prefill taking 16 s, which looks like cold expert reads rather than rendering cost, but I have not confirmed it). It does not establish a causal speed change or generation-quality improvement. Opened as a draft until that outlier is re-measured. Validation: 29 new conformance tests pass, including all five official prompt pairs, numeric budgets, late system/developer messages, parallel results in both adapters, DSML argument types and literal nested DSML tags, chunk boundaries/newlines and namespaced response/history round trips. All CI-equivalent checks passed under Python 3.12.14 with the workflow's dependencies and pinned gguf-py (`3cf03257f219afbe7334045ff7c6a06ac68c627d`): HIP mapping check; 244 server tests (6 skipped); 331 installer/tools tests (28 skipped). Exact commands, requests, responses and engine logs are available on request. Scope: prompt rendering and response parsing only. No engine, weights, quantization or GPU kernels change. DSML parameters are buffered until their closing tag before argument JSON is streamed, so the first argument fragment can arrive later. OpenAI responses expose the reference's bare function name and optional call-level namespace; internal routing and Anthropic names stay qualified. Anthropic thinking token budgets keep their existing low/high/max conversion. `tools/ds4/chat_template.jinja` remains a partial compatibility export for ordinary chat/tools. It does not implement the full reference's reminders/tasks, message-local tools, later system roles, ID sorting or namespaces; duplicating the encoder's preprocessing in sandboxed Jinja would introduce a second implementation. The server uses the pinned Python encoder. This boundary is documented in `docs/deepseek-prompt-format.md`; existing model packs are not rewritten. Image placeholders pass the fixtures, but vision inference, model quality and exhaustive malformed/truncated completion equivalence are not established by these tests. Refs #299 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:25:38 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#457
No description provided.